Search in sources :

Example 61 with WeakHashMap

use of java.util.WeakHashMap in project mockito by mockito.

the class TypeCachingMockBytecodeGeneratorTest method validate_simple_code_idea_where_weakhashmap_with_classloader_as_key_get_GCed_when_no_more_references.

@Test
public void validate_simple_code_idea_where_weakhashmap_with_classloader_as_key_get_GCed_when_no_more_references() throws Exception {
    // given
    WeakHashMap<ClassLoader, Object> cache = new WeakHashMap<ClassLoader, Object>();
    ClassLoader short_lived_classloader = inMemoryClassLoader().withClassDefinition("foo.Bar", makeMarkerInterface("foo.Bar")).build();
    cache.put(short_lived_classloader, new HoldingAReference(new WeakReference<Class<?>>(short_lived_classloader.loadClass("foo.Bar"))));
    assertThat(cache).hasSize(1);
    // when
    short_lived_classloader = is_no_more_referenced();
    System.gc();
    ensure_gc_happened();
    // then
    assertThat(cache).isEmpty();
}
Also used : WeakReference(java.lang.ref.WeakReference) ClassLoaders.inMemoryClassLoader(org.mockitoutil.ClassLoaders.inMemoryClassLoader) WeakHashMap(java.util.WeakHashMap) Test(org.junit.Test)

Example 62 with WeakHashMap

use of java.util.WeakHashMap in project gerrit by GerritCodeReview.

the class DynamicOptions method getMergedClassLoader.

protected ClassLoader getMergedClassLoader(ClassLoader beanCl, ClassLoader dynamicBeanCl) {
    Map<ClassLoader, WeakReference<ClassLoader>> mergedClByCl = mergedClByCls.get(beanCl);
    if (mergedClByCl == null) {
        mergedClByCl = Collections.synchronizedMap(new WeakHashMap<>());
        mergedClByCls.put(beanCl, mergedClByCl);
    }
    WeakReference<ClassLoader> mergedClRef = mergedClByCl.get(dynamicBeanCl);
    ClassLoader mergedCl = null;
    if (mergedClRef != null) {
        mergedCl = mergedClRef.get();
    }
    if (mergedCl == null) {
        mergedCl = new DelegatingClassLoader(beanCl, dynamicBeanCl);
        mergedClByCl.put(dynamicBeanCl, new WeakReference<>(mergedCl));
    }
    return mergedCl;
}
Also used : WeakReference(java.lang.ref.WeakReference) DelegatingClassLoader(com.google.gerrit.server.plugins.DelegatingClassLoader) DelegatingClassLoader(com.google.gerrit.server.plugins.DelegatingClassLoader) WeakHashMap(java.util.WeakHashMap)

Example 63 with WeakHashMap

use of java.util.WeakHashMap in project guava by google.

the class GcFinalizationTest method testAwaitDone_FinalizationPredicate.

public void testAwaitDone_FinalizationPredicate() {
    final WeakHashMap<Object, Object> map = new WeakHashMap<>();
    map.put(new Object(), Boolean.TRUE);
    GcFinalization.awaitDone(new FinalizationPredicate() {

        @Override
        public boolean isDone() {
            return map.isEmpty();
        }
    });
    assertTrue(map.isEmpty());
}
Also used : FinalizationPredicate(com.google.common.testing.GcFinalization.FinalizationPredicate) WeakHashMap(java.util.WeakHashMap)

Example 64 with WeakHashMap

use of java.util.WeakHashMap in project j2objc by google.

the class AbstractMapTest method test_values.

/**
 * java.util.AbstractMap#values()
 */
public void test_values() {
    AbstractMap map1 = new HashMap(0);
    assertSame("HashMap(0)", map1.values(), map1.values());
    AbstractMap map2 = new HashMap(10);
    assertSame("HashMap(10)", map2.values(), map2.values());
    Map map3 = Collections.EMPTY_MAP;
    assertSame("EMPTY_MAP", map3.values(), map3.values());
    AbstractMap map4 = new IdentityHashMap(1);
    assertSame("IdentityHashMap", map4.values(), map4.values());
    AbstractMap map5 = new LinkedHashMap(122);
    assertSame("IdentityHashMap", map5.values(), map5.values());
    AbstractMap map6 = new TreeMap();
    assertSame("TreeMap", map6.values(), map6.values());
    AbstractMap map7 = new WeakHashMap();
    assertSame("WeakHashMap", map7.values(), map7.values());
}
Also used : AbstractMap(java.util.AbstractMap) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) WeakHashMap(java.util.WeakHashMap) IdentityHashMap(java.util.IdentityHashMap) TreeMap(java.util.TreeMap) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AbstractMap(java.util.AbstractMap) TreeMap(java.util.TreeMap) Map(java.util.Map) WeakHashMap(java.util.WeakHashMap) LinkedHashMap(java.util.LinkedHashMap) WeakHashMap(java.util.WeakHashMap)

Example 65 with WeakHashMap

use of java.util.WeakHashMap in project j2objc by google.

the class AbstractMapTest method test_removeLjava_lang_Object.

/**
 * java.util.AbstractMap#remove(java.lang.Object)
 */
public void test_removeLjava_lang_Object() {
    Object key = new Object();
    Object value = new Object();
    AbstractMap map1 = new HashMap(0);
    map1.put("key", value);
    assertSame("HashMap(0)", map1.remove("key"), value);
    AbstractMap map4 = new IdentityHashMap(1);
    map4.put(key, value);
    assertSame("IdentityHashMap", map4.remove(key), value);
    AbstractMap map5 = new LinkedHashMap(122);
    map5.put(key, value);
    assertSame("LinkedHashMap", map5.remove(key), value);
    AbstractMap map6 = new TreeMap(new Comparator() {

        // Bogus comparator
        public int compare(Object object1, Object object2) {
            return 0;
        }
    });
    map6.put(key, value);
    assertSame("TreeMap", map6.remove(key), value);
    AbstractMap map7 = new WeakHashMap();
    map7.put(key, value);
    assertSame("WeakHashMap", map7.remove(key), value);
    AbstractMap aSpecialMap = new MyMap();
    aSpecialMap.put(specialKey, specialValue);
    Object valueOut = aSpecialMap.remove(specialKey);
    assertSame("MyMap", valueOut, specialValue);
}
Also used : AbstractMap(java.util.AbstractMap) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) WeakHashMap(java.util.WeakHashMap) IdentityHashMap(java.util.IdentityHashMap) TreeMap(java.util.TreeMap) LinkedHashMap(java.util.LinkedHashMap) Comparator(java.util.Comparator) WeakHashMap(java.util.WeakHashMap)

Aggregations

WeakHashMap (java.util.WeakHashMap)65 Map (java.util.Map)19 HashMap (java.util.HashMap)18 TreeMap (java.util.TreeMap)15 IdentityHashMap (java.util.IdentityHashMap)13 AbstractMap (java.util.AbstractMap)12 LinkedHashMap (java.util.LinkedHashMap)12 ICC_ColorSpace (java.awt.color.ICC_ColorSpace)8 ColorTransform (sun.java2d.cmm.ColorTransform)8 PCMM (sun.java2d.cmm.PCMM)8 Hashtable (java.util.Hashtable)6 AttributedString (java.text.AttributedString)5 Properties (java.util.Properties)5 ArrayList (java.util.ArrayList)4 Comparator (java.util.Comparator)4 List (java.util.List)4 TreeSet (java.util.TreeSet)4 WeakReference (java.lang.ref.WeakReference)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 AttributedCharacterIterator (java.text.AttributedCharacterIterator)3