commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rdon...@apache.org
Subject cvs commit: jakarta-commons/collections/src/test/org/apache/commons/collections TestReferenceMap.java
Date Wed, 07 May 2003 09:18:58 GMT
rdonkin     2003/05/07 02:18:58

  Modified:    collections/src/java/org/apache/commons/collections
                        ReferenceMap.java
               collections/src/test/org/apache/commons/collections
                        TestReferenceMap.java
  Log:
  Added ability to make referenced value purgable. This allows an instance to be constructed
with weak referenced keys whose values are purged when the keys are garbage collected.
  
  Revision  Changes    Path
  1.11      +50 -3     jakarta-commons/collections/src/java/org/apache/commons/collections/ReferenceMap.java
  
  Index: ReferenceMap.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/ReferenceMap.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- ReferenceMap.java	6 May 2003 11:19:26 -0000	1.10
  +++ ReferenceMap.java	7 May 2003 09:18:57 -0000	1.11
  @@ -175,6 +175,11 @@
        *  @serial
        */
       private float loadFactor;
  +    
  +    /**
  +     * Should the value be automatically purged when the associated key has been collected?
  +     */
  +    private boolean purgeValues = false;
   
   
       // -- Non-serialized instance variables
  @@ -237,6 +242,21 @@
           this(HARD, SOFT);
       }
   
  +    /**
  +     *  Constructs a new <Code>ReferenceMap</Code> that will
  +     *  use the specified types of references.
  +     *
  +     *  @param keyType  the type of reference to use for keys;
  +     *   must be {@link #HARD}, {@link #SOFT}, {@link #WEAK}
  +     *  @param valueType  the type of reference to use for values;
  +     *   must be {@link #HARD}, {@link #SOFT}, {@link #WEAK}
  +     *  @param purgeValues should the value be automatically purged when the 
  +     *   key is garbage collected 
  +     */
  +    public ReferenceMap(int keyType, int valueType, boolean purgeValues) {
  +        this(keyType, valueType);
  +        this.purgeValues = purgeValues;
  +    }
   
       /**
        *  Constructs a new <Code>ReferenceMap</Code> that will
  @@ -251,6 +271,29 @@
           this(keyType, valueType, 16, 0.75f);
       }
   
  +    /**
  +     *  Constructs a new <Code>ReferenceMap</Code> with the
  +     *  specified reference types, load factor and initial
  +     *  capacity.
  +     *
  +     *  @param keyType  the type of reference to use for keys;
  +     *   must be {@link #HARD}, {@link #SOFT}, {@link #WEAK}
  +     *  @param valueType  the type of reference to use for values;
  +     *   must be {@link #HARD}, {@link #SOFT}, {@link #WEAK}
  +     *  @param capacity  the initial capacity for the map
  +     *  @param loadFactor  the load factor for the map
  +     *  @param purgeValues should the value be automatically purged when the 
  +     *   key is garbage collected 
  +     */
  +    public ReferenceMap(
  +                        int keyType, 
  +                        int valueType, 
  +                        int capacity, 
  +                        float loadFactor, 
  +                        boolean purgeValues) {
  +        this(keyType, valueType, capacity, loadFactor);
  +        this.purgeValues = purgeValues;
  +    }
   
       /**
        *  Constructs a new <Code>ReferenceMap</Code> with the
  @@ -778,7 +821,11 @@
               r = r || ((valueType > HARD) && (value == ref));
               if (r) {
                   if (keyType > HARD) ((Reference)key).clear();
  -                if (valueType > HARD) ((Reference)value).clear();
  +                if (valueType > HARD) {
  +                    ((Reference)value).clear();
  +                } else if (purgeValues) {
  +                    value = null;
  +                }
               }
               return r;
           }
  
  
  
  1.8       +46 -5     jakarta-commons/collections/src/test/org/apache/commons/collections/TestReferenceMap.java
  
  Index: TestReferenceMap.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestReferenceMap.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- TestReferenceMap.java	6 May 2003 11:19:26 -0000	1.7
  +++ TestReferenceMap.java	7 May 2003 09:18:58 -0000	1.8
  @@ -61,6 +61,8 @@
   
   package org.apache.commons.collections;
   
  +import java.lang.ref.WeakReference;
  +
   import java.util.Map;
   
   import junit.framework.Test;
  @@ -186,5 +188,44 @@
           return "2.1";
       }
   
  -
  +    /** Tests whether purge values setting works */
  +    public void testPurgeValues() throws Exception {
  +        // many thanks to Juozas Baliuka for suggesting this method
  +        Object key = new Object();
  +        Object value = new Object();
  +        
  +        WeakReference keyReference = new WeakReference(key);
  +        WeakReference valueReference = new WeakReference(value);
  +        
  +        Map testMap = new ReferenceMap(ReferenceMap.WEAK, ReferenceMap.HARD, true);
  +        testMap.put(key, value);
  +        
  +        assertEquals("In map", value, testMap.get(key));
  +        assertNotNull("Weak reference released early (1)", keyReference.get());
  +        assertNotNull("Weak reference released early (2)", valueReference.get());
  +        
  +        // dereference strong references
  +        key = null;
  +        value = null;
  +        
  +        int iterations = 0;
  +        int bytz = 2;
  +        while(true) {
  +            System.gc();
  +            if(iterations++ > 50){
  +                fail("Max iterations reached before resource released.");
  +            }
  +            testMap.isEmpty();
  +            if( 
  +                keyReference.get() == null &&
  +                valueReference.get() == null) {
  +                break;
  +                
  +            } else {
  +                // create garbage:
  +                byte[] b =  new byte[bytz];
  +                bytz = bytz * 2;
  +            }
  +        }
  +    }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message