Search in sources :

Example 46 with WeakReference

use of java.lang.ref.WeakReference in project FBReaderJ by geometer.

the class CoverUtil method getCover.

public static ZLImage getCover(ZLFile file, IFormatPluginCollection collection) {
    WeakReference<ZLImage> cover = ourCovers.get(file);
    if (cover == NULL_IMAGE) {
        return null;
    } else if (cover != null) {
        final ZLImage image = cover.get();
        if (image != null) {
            return image;
        }
    }
    ZLImage image = null;
    try {
        image = collection.getPlugin(file).readCover(file);
    } catch (Exception e) {
    // ignore
    }
    ourCovers.put(file, image != null ? new WeakReference<ZLImage>(image) : NULL_IMAGE);
    return image;
}
Also used : ZLImage(org.geometerplus.zlibrary.core.image.ZLImage) WeakReference(java.lang.ref.WeakReference)

Example 47 with WeakReference

use of java.lang.ref.WeakReference in project stetho by facebook.

the class ApplicationDescriptor method onGetChildren.

@Override
protected void onGetChildren(Application element, Accumulator<Object> children) {
    ElementContext context = getContext(element);
    List<WeakReference<Activity>> activities = context.getActivitiesList();
    // We report these in reverse order so that the newer ones show up on top
    for (int i = activities.size() - 1; i >= 0; --i) {
        Activity activity = activities.get(i).get();
        if (activity != null) {
            children.store(activity);
        }
    }
}
Also used : WeakReference(java.lang.ref.WeakReference) Activity(android.app.Activity)

Example 48 with WeakReference

use of java.lang.ref.WeakReference in project gocd by gocd.

the class DefaultPluginJarLocationMonitor method removePluginJarChangeListener.

@Override
public void removePluginJarChangeListener(final PluginJarChangeListener listener) {
    Object referenceOfListenerToBeRemoved = CollectionUtils.find(pluginJarChangeListener, new Predicate() {

        @Override
        public boolean evaluate(Object object) {
            WeakReference<PluginJarChangeListener> listenerWeakReference = (WeakReference<PluginJarChangeListener>) object;
            PluginJarChangeListener registeredListener = listenerWeakReference.get();
            return registeredListener != null && registeredListener == listener;
        }
    });
    pluginJarChangeListener.remove(referenceOfListenerToBeRemoved);
    removeClearedWeakReferences();
}
Also used : WeakReference(java.lang.ref.WeakReference) Predicate(org.apache.commons.collections.Predicate)

Example 49 with WeakReference

use of java.lang.ref.WeakReference in project guice by google.

the class Asserts method awaitFullGc.

public static void awaitFullGc() {
    // GcFinalization *should* do it, but doesn't work well in practice...
    // so we put a second latch and wait for a ReferenceQueue to tell us.
    ReferenceQueue<Object> queue = new ReferenceQueue<Object>();
    WeakReference ref = new WeakReference<Object>(new Object(), queue);
    GcFinalization.awaitFullGc();
    try {
        assertSame("queue didn't return ref in time", ref, queue.remove(5000));
    } catch (IllegalArgumentException e) {
        throw new RuntimeException(e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue) WeakReference(java.lang.ref.WeakReference)

Example 50 with WeakReference

use of java.lang.ref.WeakReference in project guice by google.

the class Asserts method awaitClear.

public static void awaitClear(WeakReference<?> ref) {
    // GcFinalization *should* do it, but doesn't work well in practice...
    // so we put a second latch and wait for a ReferenceQueue to tell us.
    Object data = ref.get();
    ReferenceQueue<Object> queue = null;
    WeakReference extraRef = null;
    if (data != null) {
        queue = new ReferenceQueue<Object>();
        extraRef = new WeakReference<Object>(data, queue);
        data = null;
    }
    GcFinalization.awaitClear(ref);
    if (queue != null) {
        try {
            assertSame("queue didn't return ref in time", extraRef, queue.remove(5000));
        } catch (IllegalArgumentException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : WeakReference(java.lang.ref.WeakReference)

Aggregations

WeakReference (java.lang.ref.WeakReference)277 ArrayList (java.util.ArrayList)31 HashMap (java.util.HashMap)19 Map (java.util.Map)19 Field (java.lang.reflect.Field)18 Activity (android.app.Activity)17 Method (java.lang.reflect.Method)15 File (java.io.File)14 IOException (java.io.IOException)13 URLClassLoader (java.net.URLClassLoader)13 Test (org.junit.Test)13 Reference (java.lang.ref.Reference)12 ReferenceQueue (java.lang.ref.ReferenceQueue)12 Iterator (java.util.Iterator)12 Resources (android.content.res.Resources)10 Handler (android.os.Handler)10 AbstractModule (com.google.inject.AbstractModule)10 Injector (com.google.inject.Injector)10 ArrayMap (android.util.ArrayMap)9 URL (java.net.URL)8