List of usage examples for org.apache.commons.collections.map ReferenceMap get
public Object get(Object key)
From source file:org.lockss.test.FuncCommons.java
/** A {Hard,Weak} ref map should automatically remove entries whose * values have no other references. Add lots of otherwise unreferenced * objects and make sure the map doesn't grow monotonically. Check that * an object that did have other pointers is still in the map after * several collections, and that others aren't. *///from w w w . jav a 2 s . c o m public void testRefMapHW() { ReferenceMap map = new ReferenceMap(ReferenceMap.HARD, ReferenceMap.WEAK); int drop = 5; /* 20 too big - PJG */// number of drops in map size needed int loop = 10000; // inner loop repetitions Integer aKey = null; // one key that we will hold on to Object aVal = null; // one value that we will hold on to int lastSize = 0; // last map size // Repeat until we have seen enough drops in size for (int ii = 0; drop > 0; ii++) { for (int jj = 0; jj < loop; jj++) { Integer key = new Integer(ii * loop + jj); // make a unique key Integer val = new Integer(jj); map.put(key, val); if (key.intValue() == 143) { // hold on to key 143 aKey = key; } if (key.intValue() == 243) { // hold on to value for key 243 aVal = val; } } // System.out.println(""+ map.size()); if (map.size() < lastSize) { // Map is smaller than last time. Some entries have been removed. drop--; } else if ((ii % 100) == 0) { System.gc(); } lastSize = map.size(); } // Because the value for key 243 still exists, that entry should still // be in the map, and should be the same as the one we put in assertTrue(map.containsKey(new Integer(243))); assertSame(aVal, (Integer) map.get(new Integer(243))); // This key's value has been collected by now (we hope), so it shouldn't // be in the map assertFalse(map.containsKey(new Integer(221))); // Holding on to a key shouldn't have any effect. This one shouldn't // be in the map either. assertFalse(map.containsKey(new Integer(143))); assertFalse(map.containsKey(aKey)); }
From source file:org.openengsb.core.common.WeakMapUT.java
@Test public void testReferenceMap_shouldWork() throws Exception { ReferenceMap map = new ReferenceMap(AbstractReferenceMap.HARD, AbstractReferenceMap.WEAK); Object referent = new Object(); map.put("42", referent); assertThat(map.containsKey("42"), is(true)); assertThat(map.get("42"), sameInstance(referent)); referent = null;// w ww. j a va 2 s . co m System.gc(); assertThat(map.containsKey("42"), is(false)); assertThat(map.get("42"), nullValue()); }