Example usage for java.lang.ref WeakReference WeakReference

List of usage examples for java.lang.ref WeakReference WeakReference

Introduction

In this page you can find the example usage for java.lang.ref WeakReference WeakReference.

Prototype

public WeakReference(T referent) 

Source Link

Document

Creates a new weak reference that refers to the given object.

Usage

From source file:co.paralleluniverse.common.monitoring.PeriodicMonitor.java

public void setMonitoredObject(Object obj) {
    this.monitored = new WeakReference<Object>(obj);
}

From source file:com.actionbarsherlock.internal.nineoldandroids.view.animation.AnimatorProxy.java

private AnimatorProxy(View view) {
    setDuration(0); //perform transformation immediately
    setFillAfter(true); //persist transformation beyond duration
    view.setAnimation(this);
    mView = new WeakReference<View>(view);
}

From source file:org.Cherry.Modules.Web.Engine.JSONRequestHandler.java

@Override
public void handle(final RequestCommand command) throws HttpException, IOException {
    final CallDef call = scanRESTBeanURI(command.getUri());

    final JSONAgentDefinition jsonAgent = getController(call.getControllerURI());

    assert null != jsonAgent : "Undefined controller for URI [" + command.getUri() + "]";

    final RESTMethodDefinition restMethodDef = jsonAgent.get(call.getMethodURI());

    assert null != restMethodDef : "Undefined controller method for URI [" + command.getUri() + "/"
            + call.getMethodURI() + "]";

    WeakReference<Object> wkRef = null;

    if (null != restMethodDef.getBeanParameterType())
        wkRef = new WeakReference<Object>(
                restMethodDef.invoke(getBeanParameter(command, restMethodDef.getBeanParameterType())));
    else//from  ww w.  ja va 2s.com
        wkRef = new WeakReference<Object>(restMethodDef.invoke((Object[]) null));

    AbstractHttpEntity entity = null;

    if (null != wkRef && null != wkRef.get())
        if (getObjectMapperService().canSerialize(wkRef.get().getClass())) {
            debug("Handled URI [{}] with controller [{}] and result [{}]", command.getUri(), jsonAgent,
                    wkRef.get());

            final WeakReference<ByteArrayOutputStream> os = new WeakReference<ByteArrayOutputStream>(
                    new ByteArrayOutputStream());

            getObjectMapperService().writeValue(os.get(), wkRef.get());

            entity = new InputStreamEntity(new ByteArrayInputStream(os.get().toByteArray()),
                    APPLICATION_JSON_CONTENT_TYPE);
        } else
            warn("Object [{}] not JSON serializable!", wkRef.get());
    else
        warn("JSON Controller returned null for expected result of type [{}]",
                restMethodDef.getBeanParameterType());

    command.getResponse().setStatusCode(HttpStatus.SC_OK);

    if (null == entity)
        entity = new StringEntity(EMPTY_JSON_OBJECT, APPLICATION_JSON_CONTENT_TYPE);

    command.getResponse().setEntity(entity);
}

From source file:com.dgsd.android.ShiftTracker.Adapter.MonthPagerAdapter.java

@Override
public Fragment getItem(int pos) {
    final YearAndMonth ym = getYearAndMonthForPosition(pos);
    final MonthFragment frag = MonthFragment.newInstance(ym.month, ym.year);

    mPosToFragRef.put(pos, new WeakReference<MonthFragment>(frag));
    return frag;/*from w  w w. j av a  2  s  .com*/
}

From source file:py.una.pol.karaku.util.I18nHelper.java

/**
 * Retorna el {@link I18nHelper} que actualmente esta siendo utilizado por
 * la aplicacin./*from w w  w .  j  a  va 2 s  . c  o m*/
 * 
 * @return
 */
public static I18nHelper getSingleton() {

    if (weakSingleton != null) {
        return weakSingleton.get();
    }

    synchronized (I18nHelper.class) {
        if (weakSingleton == null) {
            weakSingleton = new WeakReference<I18nHelper>(context.getBean(I18nHelper.class));
        }
    }
    return weakSingleton.get();
}

From source file:com.parse.ParseRelation.java

void ensureParentAndKey(ParseObject someParent, String someKey) {
    synchronized (mutex) {
        if (parent == null) {
            parent = new WeakReference<>(someParent);
            parentObjectId = someParent.getObjectId();
            parentClassName = someParent.getClassName();
        }/*from  w  w w .  ja v a 2s .  co m*/
        if (key == null) {
            key = someKey;
        }
        if (parent.get() != someParent) {
            throw new IllegalStateException(
                    "Internal error. One ParseRelation retrieved from two different ParseObjects.");
        }
        if (!key.equals(someKey)) {
            throw new IllegalStateException(
                    "Internal error. One ParseRelation retrieved from two different keys.");
        }
    }
}

From source file:com.gargoylesoftware.htmlunit.javascript.background.JavaScriptExecutor.java

/** Creates an EventLoop for the webClient.
 *
 * @param webClient the provided webClient
 *//*  w  w w .j a va 2s  . c  om*/
public JavaScriptExecutor(final WebClient webClient) {
    jobManagerList_ = new ArrayList<WeakReference<JavaScriptJobManager>>();
    webClient_ = new WeakReference<WebClient>(webClient);
}

From source file:org.Cherry.Modules.Web.Engine.ResponseInterceptor.java

@Override
public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException {
    log.debug("Intercepted response [{}] for context [{}].", response, context);

    if (authenticated(context))
        if (createCookie(context)) {
            final WeakReference<BasicServerCookie> cookie = new WeakReference<BasicServerCookie>(
                    new BasicServerCookie(getSessionCookie(),
                            getServerSessions() ? getSession().toString() : UUID.randomUUID().toString()));

            cookie.get().setDomain(getSessionCookieDomain());
            cookie.get().setPath(getSessionCookiePath());
            cookie.get().setVersion(getSessionCookieVersion());
            cookie.get().setSecure(true);

            final Date expiryDate = new DateTime().plusMinutes(getSessionCookieTimeToLive().intValue())
                    .toDate();// ww  w. j  ava  2s  .  c om

            cookie.get().setExpiryDate(expiryDate);

            response.addHeader(Set_Cookie, cookie.get().toString());
        }
}

From source file:com.microsoft.tfs.core.ws.runtime.transport.IdleHTTPConnectionCloser.java

/**
 * Adds a client to be monitored by this thread.
 *
 * @param client//from   w  w w.j  a v  a  2 s .  c om
 *        the client whose connection manager should be monitored for idle
 *        connections that should be closed (not null).
 */
public void addClient(final HttpClient client) {
    Check.notNull(client, "client"); //$NON-NLS-1$

    synchronized (clientWeakReferences) {
        clientWeakReferences.add(new WeakReference(client));
    }
}

From source file:com.espian.ticktock.oss.CursorPagerAdapter.java

@Override
public Fragment getItem(int position) {
    if (cursor == null || cursor.getCount() == 0) {
        if (emptyFragmentClass != null) {
            try {
                return emptyFragmentClass.newInstance();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }/*from ww  w  .j a  v a2s.c om*/
        }
        return null;
    }

    if (fragmentRefs.get(position) != null && fragmentRefs.get(position).get() != null)
        return fragmentRefs.get(position).get();

    cursor.moveToPosition(position);
    F frag;
    try {
        frag = fragmentClass.newInstance();
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
    Bundle args = new Bundle();
    if (projection == null || projection.length == 0) {
        for (int i = 0; i < cursor.getColumnCount(); i++) {
            args.putString(cursor.getColumnName(i), cursor.getString(i));
        }
    } else {
        for (int i = 0; i < projection.length; ++i) {
            args.putString(projection[i], cursor.getString(i));
        }
    }
    frag.setArguments(args);
    fragmentRefs.put(position, new WeakReference<F>(frag));
    return frag;
}