Example usage for org.apache.commons.collections.map ReferenceMap size

List of usage examples for org.apache.commons.collections.map ReferenceMap size

Introduction

In this page you can find the example usage for org.apache.commons.collections.map ReferenceMap size.

Prototype

public int size() 

Source Link

Document

Gets the size of the map.

Usage

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.  j av  a  2s  .com
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));
}