[jboss-svn-commits] JBoss Common SVN: r2931 - in common-core/trunk/src: test/java/org/jboss/test/util/test and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Sat Nov 1 07:58:22 EDT 2008


Author: alesj
Date: 2008-11-01 07:58:22 -0400 (Sat, 01 Nov 2008)
New Revision: 2931

Added:
   common-core/trunk/src/main/java/org/jboss/util/collection/ReferenceValueHashMap.java
   common-core/trunk/src/main/java/org/jboss/util/collection/ValueRef.java
   common-core/trunk/src/test/java/org/jboss/test/util/test/collection/AbstractMapUnitTest.java
   common-core/trunk/src/test/java/org/jboss/test/util/test/collection/SoftValueMapUnitTestCase.java
   common-core/trunk/src/test/java/org/jboss/test/util/test/collection/WeakValueMapUnitTestCase.java
Modified:
   common-core/trunk/src/main/java/org/jboss/util/collection/SoftValueHashMap.java
   common-core/trunk/src/main/java/org/jboss/util/collection/WeakValueHashMap.java
   common-core/trunk/src/test/java/org/jboss/test/util/test/StringPropertyReplacerUnitTestCase.java
Log:
[JBCOMMON-71]; initial SVHM fix.

Copied: common-core/trunk/src/main/java/org/jboss/util/collection/ReferenceValueHashMap.java (from rev 2930, common-core/trunk/src/main/java/org/jboss/util/collection/WeakValueHashMap.java)
===================================================================
--- common-core/trunk/src/main/java/org/jboss/util/collection/ReferenceValueHashMap.java	                        (rev 0)
+++ common-core/trunk/src/main/java/org/jboss/util/collection/ReferenceValueHashMap.java	2008-11-01 11:58:22 UTC (rev 2931)
@@ -0,0 +1,213 @@
+/*
+  * JBoss, Home of Professional Open Source
+  * Copyright 2005, JBoss Inc., and individual contributors as indicated
+  * by the @authors tag. See the copyright.txt in the distribution for a
+  * full listing of individual contributors.
+  *
+  * This is free software; you can redistribute it and/or modify it
+  * under the terms of the GNU Lesser General Public License as
+  * published by the Free Software Foundation; either version 2.1 of
+  * the License, or (at your option) any later version.
+  *
+  * This software is distributed in the hope that it will be useful,
+  * but WITHOUT ANY WARRANTY; without even the implied warranty of
+  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  * Lesser General Public License for more details.
+  *
+  * You should have received a copy of the GNU Lesser General Public
+  * License along with this software; if not, write to the Free
+  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+  */
+package org.jboss.util.collection;
+
+import java.lang.ref.ReferenceQueue;
+import java.util.AbstractMap;
+import java.util.AbstractSet;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+import java.io.Serializable;
+
+
+/**
+ * This Map will remove entries when the value in the map has been
+ * cleaned from garbage collection
+ *
+ * ReferenceMap is serializable if K and V are serializable.
+ *
+ * @param <K> the key type
+ * @param <V> the value type
+ * @author  <a href="mailto:bill at jboss.org">Bill Burke</a>
+ * @author  <a href="mailto:adrian at jboss.org">Adrian Brock</a>
+ * @author  <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+public abstract class ReferenceValueHashMap<K, V> extends AbstractMap<K, V>
+{
+   /** Hash table mapping keys to ref values */
+   private Map<K, ValueRef<K, V>> hash;
+
+   /** Reference queue for cleared WeakKeys */
+   private ReferenceQueue<V> queue = new ReferenceQueue<V>();
+
+   protected ReferenceValueHashMap(int initialCapacity, float loadFactor)
+   {
+      hash = new HashMap<K, ValueRef<K, V>>(initialCapacity, loadFactor);
+   }
+
+   protected ReferenceValueHashMap(int initialCapacity)
+   {
+      hash = new HashMap<K, ValueRef<K, V>>(initialCapacity);
+   }
+
+   protected ReferenceValueHashMap()
+   {
+      hash = new HashMap<K, ValueRef<K, V>>();
+   }
+
+   protected ReferenceValueHashMap(Map<K, V> t)
+   {
+      this(Math.max(2*t.size(), 11), 0.75f);
+      putAll(t);
+   }
+
+   @Override
+   public int size()
+   {
+      processQueue();
+      return hash.size();
+   }
+
+   @Override
+   public boolean containsKey(Object key)
+   {
+      processQueue();
+      return hash.containsKey(key);
+   }
+
+   @Override
+   public V get(Object key)
+   {
+      processQueue();
+      ValueRef<K, V> ref = hash.get(key);
+      if (ref != null)
+         return ref.get();
+      return null;
+   }
+
+   @Override
+   public V put(K key, V value)
+   {
+      processQueue();
+      ValueRef<K, V> ref = create(key, value, queue);
+      ValueRef<K, V> result = hash.put(key, ref);
+      if (result != null)
+         return result.get();
+      return null;
+   }
+
+   @Override
+   public V remove(Object key)
+   {
+      processQueue();
+      ValueRef<K, V> result = hash.remove(key);
+      if (result != null)
+         return result.get();
+      return null;
+   }
+
+   @Override
+   public Set<Entry<K,V>> entrySet()
+   {
+      processQueue();
+      return new EntrySet();
+   }
+
+   @Override
+   public void clear()
+   {
+      processQueue();
+      hash.clear();
+   }
+
+   /**
+    * Remove all entries whose values have been discarded.
+    */
+   @SuppressWarnings("unchecked")
+   private void processQueue()
+   {
+      ValueRef<K, V> ref = (ValueRef<K, V>) queue.poll();
+      while (ref != null)
+      {
+         // only remove if it is the *exact* same WeakValueRef
+         if (ref == hash.get(ref.getKey()))
+            hash.remove(ref.getKey());
+
+         ref = (ValueRef<K, V>) queue.poll();
+      }
+   }
+
+   /**
+    * EntrySet.
+    */
+   private class EntrySet extends AbstractSet<Entry<K, V>>
+   {
+      @Override
+      public Iterator<Entry<K, V>> iterator()
+      {
+         return new EntrySetIterator(hash.entrySet().iterator());
+      }
+
+      @Override
+      public int size()
+      {
+         return ReferenceValueHashMap.this.size();
+      }
+   }
+
+   /**
+    * EntrySet iterator
+    */
+   private class EntrySetIterator implements Iterator<Entry<K, V>>
+   {
+      /** The delegate */
+      private Iterator<Entry<K, ValueRef<K, V>>> delegate;
+
+      /**
+       * Create a new EntrySetIterator.
+       *
+       * @param delegate the delegate
+       */
+      public EntrySetIterator(Iterator<Entry<K, ValueRef<K, V>>> delegate)
+      {
+         this.delegate = delegate;
+      }
+
+      public boolean hasNext()
+      {
+         return delegate.hasNext();
+      }
+
+      public Entry<K, V> next()
+      {
+         Entry<K, ValueRef<K, V>> next = delegate.next();
+         return next.getValue();
+      }
+
+      public void remove()
+      {
+         throw new UnsupportedOperationException("remove");
+      }
+   }
+
+   /**
+    * Create new value ref instance.
+    *
+    * @param key the key
+    * @param value the value
+    * @param q the ref queue
+    * @return new value ref instance
+    */
+   protected abstract ValueRef<K, V> create(K key, V value, ReferenceQueue<V> q);
+}
\ No newline at end of file

Modified: common-core/trunk/src/main/java/org/jboss/util/collection/SoftValueHashMap.java
===================================================================
--- common-core/trunk/src/main/java/org/jboss/util/collection/SoftValueHashMap.java	2008-10-21 13:24:28 UTC (rev 2930)
+++ common-core/trunk/src/main/java/org/jboss/util/collection/SoftValueHashMap.java	2008-11-01 11:58:22 UTC (rev 2931)
@@ -23,79 +23,25 @@
 
 import java.lang.ref.ReferenceQueue;
 import java.lang.ref.SoftReference;
-import java.util.AbstractMap;
-import java.util.HashMap;
 import java.util.Map;
-import java.util.Set;
 
-
 /**
  * This Map will remove entries when the value in the map has been
  * cleaned from garbage collection
  *
- * @version <tt>$Revision$</tt>
  * @author  <a href="mailto:bill at jboss.org">Bill Burke</a>
+ * @author  <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
  */
- at SuppressWarnings("unchecked")
-public class SoftValueHashMap
-   extends AbstractMap
-   implements Map 
+public class SoftValueHashMap<K, V> extends ReferenceValueHashMap<K, V>
 {
-   private static class SoftValueRef extends SoftReference
-   {
-      public Object key;
-
-      private SoftValueRef(Object key, Object val, ReferenceQueue q)
-      {
-         super(val, q);
-         this.key = key;
-      }
-      
-      private static SoftValueRef create(Object key, Object val, ReferenceQueue q)
-      {
-         if (val == null) return null;
-         else return new SoftValueRef(key, val, q);
-      }
-      
-   }
-   public Set entrySet() 
-   { 
-      processQueue();
-      return hash.entrySet();
-   }
-
-   /* Hash table mapping WeakKeys to values */
-   private Map hash;
-
-   /* Reference queue for cleared WeakKeys */
-   private ReferenceQueue queue = new ReferenceQueue();
-   
-   /* Remove all invalidated entries from the map, that is, remove all entries
-      whose values have been discarded.  
-    */
-   private void processQueue()
-   {
-      SoftValueRef ref;
-      while ((ref = (SoftValueRef)queue.poll()) != null) {
-         if (ref == (SoftValueRef) hash.get(ref.key)) {
-            // only remove if it is the *exact* same WeakValueRef
-            //
-            hash.remove(ref.key);
-         }
-      }
-   }
-
-
-   /* -- Constructors -- */
-
    /**
-    * Constructs a new, empty <code>WeakHashMap</code> with the given
+    * Constructs a new, empty <code>SoftValueHashMap</code> with the given
     * initial capacity and the given load factor.
     *
     * @param  initialCapacity  The initial capacity of the
-    *                          <code>WeakHashMap</code>
+    *                          <code>SoftValueHashMap</code>
     *
-    * @param  loadFactor       The load factor of the <code>WeakHashMap</code>
+    * @param  loadFactor       The load factor of the <code>SoftValueHashMap</code>
     *
     * @throws IllegalArgumentException  If the initial capacity is less than
     *                                   zero, or if the load factor is
@@ -103,146 +49,106 @@
     */
    public SoftValueHashMap(int initialCapacity, float loadFactor)
    {
-      hash = new HashMap(initialCapacity, loadFactor);
+      super(initialCapacity, loadFactor);
    }
 
    /**
-    * Constructs a new, empty <code>WeakHashMap</code> with the given
+    * Constructs a new, empty <code>SoftValueHashMap</code> with the given
     * initial capacity and the default load factor, which is
     * <code>0.75</code>.
     *
     * @param  initialCapacity  The initial capacity of the
-    *                          <code>WeakHashMap</code>
+    *                          <code>SoftValueHashMap</code>
     *
     * @throws IllegalArgumentException  If the initial capacity is less than
     *                                   zero
     */
    public SoftValueHashMap(int initialCapacity)
    {
-      hash = new HashMap(initialCapacity);
+      super(initialCapacity);
    }
 
    /**
-    * Constructs a new, empty <code>WeakHashMap</code> with the default
+    * Constructs a new, empty <code>SoftValueHashMap</code> with the default
     * initial capacity and the default load factor, which is
     * <code>0.75</code>.
     */
    public SoftValueHashMap()
    {
-      hash = new HashMap();
    }
 
    /**
-    * Constructs a new <code>WeakHashMap</code> with the same mappings as the
-    * specified <tt>Map</tt>.  The <code>WeakHashMap</code> is created with an
+    * Constructs a new <code>SoftValueHashMap</code> with the same mappings as the
+    * specified <tt>Map</tt>.  The <code>SoftValueHashMap</code> is created with an
     * initial capacity of twice the number of mappings in the specified map
     * or 11 (whichever is greater), and a default load factor, which is
     * <tt>0.75</tt>.
     *
     * @param   t the map whose mappings are to be placed in this map.
-    * @since	1.3
+    * @since    1.3
     */
-   public SoftValueHashMap(Map t)
+   public SoftValueHashMap(Map<K, V> t)
    {
-      this(Math.max(2*t.size(), 11), 0.75f);
-      putAll(t);
+      super(t);
    }
 
-   /* -- Simple queries -- */
-
-   /**
-    * Returns the number of key-value mappings in this map.
-    * <strong>Note:</strong> <em>In contrast with most implementations of the
-    * <code>Map</code> interface, the time required by this operation is
-    * linear in the size of the map.</em>
-    */
-   public int size()
+   protected ValueRef<K, V> create(K key, V value, ReferenceQueue<V> q)
    {
-      processQueue();
-      return hash.size();
+      return SoftValueRef.create(key, value, q);
    }
 
    /**
-    * Returns <code>true</code> if this map contains no key-value mappings.
+    * Soft value ref impl
     */
-   public boolean isEmpty()
+   private static class SoftValueRef<K, V> extends SoftReference<V> implements ValueRef<K, V>
    {
-      processQueue();
-      return hash.isEmpty();
-   }
+      /** The key */
+      public K key;
 
-   /**
-    * Returns <code>true</code> if this map contains a mapping for the
-    * specified key.
-    *
-    * @param   key   The key whose presence in this map is to be tested
-    */
-   public boolean containsKey(Object key)
-   {
-      processQueue();
-      return hash.containsKey(key);
-   }
+      /**
+       * Safely create a new WeakValueRef
+       *
+       * @param <K> the key type
+       * @param <V> the value type
+       * @param key the key
+       * @param val the value
+       * @param q the reference queue
+       * @return the reference or null if the value is null
+       */
+      private static <K, V> SoftValueRef<K, V> create(K key, V val, ReferenceQueue<V> q)
+      {
+         if (val == null)
+            return null;
+         else
+            return new SoftValueRef<K, V>(key, val, q);
+      }
 
-   /* -- Lookup and modification operations -- */
+      /**
+       * Create a new WeakValueRef.
+       *
+       * @param key the key
+       * @param val the value
+       * @param q the reference queue
+       */
+      private SoftValueRef(K key, V val, ReferenceQueue<V> q)
+      {
+         super(val, q);
+         this.key = key;
+      }
 
-   /**
-    * Returns the value to which this map maps the specified <code>key</code>.
-    * If this map does not contain a value for this key, then return
-    * <code>null</code>.
-    *
-    * @param  key  The key whose associated value, if any, is to be returned
-    */
-   public Object get(Object key)
-   {
-      processQueue();
-      SoftReference ref = (SoftReference)hash.get(key);
-      if (ref != null) return ref.get();
-      return null;
-   }
+      public K getKey()
+      {
+         return key;
+      }
 
-   /**
-    * Updates this map so that the given <code>key</code> maps to the given
-    * <code>value</code>.  If the map previously contained a mapping for
-    * <code>key</code> then that mapping is replaced and the previous value is
-    * returned.
-    *
-    * @param  key    The key that is to be mapped to the given
-    *                <code>value</code> 
-    * @param  value  The value to which the given <code>key</code> is to be
-    *                mapped
-    *
-    * @return  The previous value to which this key was mapped, or
-    *          <code>null</code> if if there was no mapping for the key
-    */
-   public Object put(Object key, Object value) 
-   {
-      processQueue();
-      Object rtn = hash.put(key, SoftValueRef.create(key, value, queue));
-      if (rtn != null) rtn = ((SoftReference)rtn).get();
-      return rtn;
-   }
+      public V getValue()
+      {
+         return get();
+      }
 
-   /**
-    * Removes the mapping for the given <code>key</code> from this map, if
-    * present.
-    *
-    * @param  key  The key whose mapping is to be removed
-    *
-    * @return  The value to which this key was mapped, or <code>null</code> if
-    *          there was no mapping for the key
-    */
-   public Object remove(Object key) 
-   {
-      processQueue();
-      return hash.remove(key);
+      public V setValue(V value)
+      {
+         throw new UnsupportedOperationException("setValue");
+      }
    }
-
-   /**
-    * Removes all mappings from this map.
-    */
-   public void clear()
-   {
-      processQueue();
-      hash.clear();
-   }
 }

Added: common-core/trunk/src/main/java/org/jboss/util/collection/ValueRef.java
===================================================================
--- common-core/trunk/src/main/java/org/jboss/util/collection/ValueRef.java	                        (rev 0)
+++ common-core/trunk/src/main/java/org/jboss/util/collection/ValueRef.java	2008-11-01 11:58:22 UTC (rev 2931)
@@ -0,0 +1,43 @@
+/*
+  * JBoss, Home of Professional Open Source
+  * Copyright 2005, JBoss Inc., and individual contributors as indicated
+  * by the @authors tag. See the copyright.txt in the distribution for a
+  * full listing of individual contributors.
+  *
+  * This is free software; you can redistribute it and/or modify it
+  * under the terms of the GNU Lesser General Public License as
+  * published by the Free Software Foundation; either version 2.1 of
+  * the License, or (at your option) any later version.
+  *
+  * This software is distributed in the hope that it will be useful,
+  * but WITHOUT ANY WARRANTY; without even the implied warranty of
+  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  * Lesser General Public License for more details.
+  *
+  * You should have received a copy of the GNU Lesser General Public
+  * License along with this software; if not, write to the Free
+  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+  */
+package org.jboss.util.collection;
+
+import java.io.Serializable;
+import java.util.Map;
+
+/**
+ * ValueRef.
+ *
+ * ValueRef instance is serializable if K and V are serializable.
+ *
+ * @param <K> the key type
+ * @param <V> the value type
+ */
+public interface ValueRef<K, V> extends Map.Entry<K, V>, Serializable
+{
+   /**
+    * Get underlying value.
+    *
+    * @return the value
+    */
+   V get();
+}

Modified: common-core/trunk/src/main/java/org/jboss/util/collection/WeakValueHashMap.java
===================================================================
--- common-core/trunk/src/main/java/org/jboss/util/collection/WeakValueHashMap.java	2008-10-21 13:24:28 UTC (rev 2930)
+++ common-core/trunk/src/main/java/org/jboss/util/collection/WeakValueHashMap.java	2008-11-01 11:58:22 UTC (rev 2931)
@@ -23,12 +23,7 @@
 
 import java.lang.ref.ReferenceQueue;
 import java.lang.ref.WeakReference;
-import java.util.AbstractMap;
-import java.util.AbstractSet;
-import java.util.HashMap;
-import java.util.Iterator;
 import java.util.Map;
-import java.util.Set;
 
 
 /**
@@ -39,24 +34,18 @@
  * @param <V> the value type
  * @author  <a href="mailto:bill at jboss.org">Bill Burke</a>
  * @author  <a href="mailto:adrian at jboss.org">Adrian Brock</a>
- * @version <tt>$Revision$</tt>
+ * @author  <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
  */
-public class WeakValueHashMap<K, V> extends AbstractMap<K, V> 
+public class WeakValueHashMap<K, V> extends ReferenceValueHashMap<K, V>
 {
-   /** Hash table mapping keys to weak values */
-   private Map<K, WeakValueRef<K, V>> hash;
-
-   /** Reference queue for cleared WeakKeys */
-   private ReferenceQueue<V> queue = new ReferenceQueue<V>();
-
    /**
-    * Constructs a new, empty <code>WeakHashMap</code> with the given
+    * Constructs a new, empty <code>WeakValueHashMap</code> with the given
     * initial capacity and the given load factor.
     *
     * @param  initialCapacity  The initial capacity of the
-    *                          <code>WeakHashMap</code>
+    *                          <code>WeakValueHashMap</code>
     *
-    * @param  loadFactor       The load factor of the <code>WeakHashMap</code>
+    * @param  loadFactor       The load factor of the <code>WeakValueHashMap</code>
     *
     * @throws IllegalArgumentException  If the initial capacity is less than
     *                                   zero, or if the load factor is
@@ -64,38 +53,37 @@
     */
    public WeakValueHashMap(int initialCapacity, float loadFactor)
    {
-      hash = new HashMap<K, WeakValueRef<K, V>>(initialCapacity, loadFactor);
+      super(initialCapacity, loadFactor);
    }
 
    /**
-    * Constructs a new, empty <code>WeakHashMap</code> with the given
+    * Constructs a new, empty <code>WeakValueHashMap</code> with the given
     * initial capacity and the default load factor, which is
     * <code>0.75</code>.
     *
     * @param  initialCapacity  The initial capacity of the
-    *                          <code>WeakHashMap</code>
+    *                          <code>WeakValueHashMap</code>
     *
     * @throws IllegalArgumentException  If the initial capacity is less than
     *                                   zero
     */
    public WeakValueHashMap(int initialCapacity)
    {
-      hash = new HashMap<K, WeakValueRef<K, V>>(initialCapacity);
+      super(initialCapacity);
    }
 
    /**
-    * Constructs a new, empty <code>WeakHashMap</code> with the default
+    * Constructs a new, empty <code>WeakValueHashMap</code> with the default
     * initial capacity and the default load factor, which is
     * <code>0.75</code>.
     */
    public WeakValueHashMap()
    {
-      hash = new HashMap<K, WeakValueRef<K, V>>();
    }
 
    /**
-    * Constructs a new <code>WeakHashMap</code> with the same mappings as the
-    * specified <tt>Map</tt>.  The <code>WeakHashMap</code> is created with an
+    * Constructs a new <code>WeakValueHashMap</code> with the same mappings as the
+    * specified <tt>Map</tt>.  The <code>WeakValueHashMap</code> is created with an
     * initial capacity of twice the number of mappings in the specified map
     * or 11 (whichever is greater), and a default load factor, which is
     * <tt>0.75</tt>.
@@ -105,153 +93,25 @@
     */
    public WeakValueHashMap(Map<K, V> t)
    {
-      this(Math.max(2*t.size(), 11), 0.75f);
-      putAll(t);
+      super(t);
    }
 
-   @Override
-   public int size()
+   protected ValueRef<K, V> create(K key, V value, ReferenceQueue<V> q)
    {
-      processQueue();
-      return hash.size();
+      return WeakValueRef.create(key, value, q);
    }
-
-   @Override
-   public boolean containsKey(Object key)
-   {
-      processQueue();
-      return hash.containsKey(key);
-   }
-
-   @Override
-   public V get(Object key)
-   {
-      processQueue();
-      WeakValueRef<K, V> ref = hash.get(key);
-      if (ref != null)
-         return ref.get();
-      return null;
-   }
-
-   @Override
-   public V put(K key, V value) 
-   {
-      processQueue();
-      WeakValueRef<K, V> ref = WeakValueRef.create(key, value, queue);
-      WeakValueRef<K, V> result = hash.put(key, ref);
-      if (result != null)
-         return result.get();
-      return null;
-   }
-
-   @Override
-   public V remove(Object key) 
-   {
-      processQueue();
-      WeakValueRef<K, V> result = hash.remove(key);
-      if (result != null)
-         return result.get();
-      return null;
-   }
    
-   @Override
-   public Set<Entry<K,V>> entrySet() 
-   { 
-      processQueue();
-      return new EntrySet();
-   }
-
-   @Override
-   public void clear()
-   {
-      processQueue();
-      hash.clear();
-   }
-   
    /**
-    * Remove all entries whose values have been discarded.  
+    * Weak value ref impl
     */
-   @SuppressWarnings("unchecked")
-   private void processQueue()
+   private static class WeakValueRef<K, V> extends WeakReference<V> implements ValueRef<K, V>
    {
-      WeakValueRef<K, V> ref = (WeakValueRef<K, V>) queue.poll();
-      while (ref != null)
-      {
-         // only remove if it is the *exact* same WeakValueRef
-         if (ref == hash.get(ref.key))
-            hash.remove(ref.key);
-         
-         ref = (WeakValueRef<K, V>) queue.poll();
-      }
-   }
-   
-   /**
-    * EntrySet.
-    */
-   private class EntrySet extends AbstractSet<Entry<K, V>>
-   {
-      @Override
-      public Iterator<Entry<K, V>> iterator()
-      {
-         return new EntrySetIterator(hash.entrySet().iterator());
-      }
-
-      @Override
-      public int size()
-      {
-         return WeakValueHashMap.this.size();
-      }
-   }
-   
-   /**
-    * EntrySet iterator
-    */
-   private class EntrySetIterator implements Iterator<Entry<K, V>>
-   {
-      /** The delegate */
-      private Iterator<Entry<K, WeakValueRef<K, V>>> delegate;
-      
-      /**
-       * Create a new EntrySetIterator.
-       * 
-       * @param delegate the delegate
-       */
-      public EntrySetIterator(Iterator<Entry<K, WeakValueRef<K, V>>> delegate)
-      {
-         this.delegate = delegate;
-      }
-
-      public boolean hasNext()
-      {
-         return delegate.hasNext();
-      }
-
-      public Entry<K, V> next()
-      {
-         Entry<K, WeakValueRef<K, V>> next = delegate.next();
-         return next.getValue();
-      }
-
-      public void remove()
-      {
-         throw new UnsupportedOperationException("remove");
-      }
-   }
-   
-   /**
-    * WeakValueRef.
-    * 
-    * @param <K> the key type
-    * @param <V> the value type
-    */
-   private static class WeakValueRef<K, V> extends WeakReference<V> implements Map.Entry<K, V>
-   {
       /** The key */
       public K key;
 
       /**
        * Safely create a new WeakValueRef
-       * 
+       *
        * @param <K> the key type
        * @param <V> the value type
        * @param key the key
@@ -263,13 +123,13 @@
       {
          if (val == null)
             return null;
-         else 
+         else
             return new WeakValueRef<K, V>(key, val, q);
       }
 
       /**
        * Create a new WeakValueRef.
-       * 
+       *
        * @param key the key
        * @param val the value
        * @param q the reference queue

Modified: common-core/trunk/src/test/java/org/jboss/test/util/test/StringPropertyReplacerUnitTestCase.java
===================================================================
--- common-core/trunk/src/test/java/org/jboss/test/util/test/StringPropertyReplacerUnitTestCase.java	2008-10-21 13:24:28 UTC (rev 2930)
+++ common-core/trunk/src/test/java/org/jboss/test/util/test/StringPropertyReplacerUnitTestCase.java	2008-11-01 11:58:22 UTC (rev 2931)
@@ -22,16 +22,11 @@
 
 package org.jboss.test.util.test;
 
-import static org.jboss.util.StringPropertyReplacer.*;
-import static org.junit.Assert.*;
-
 import java.util.Properties;
 
 import junit.framework.TestCase;
+import static org.jboss.util.StringPropertyReplacer.replaceProperties;
 
-import org.jboss.util.StringPropertyReplacer;
-import org.junit.Test;
-
 /**
  * A StringPropertyReplacerUnitTestCase.
  * 

Copied: common-core/trunk/src/test/java/org/jboss/test/util/test/collection/AbstractMapUnitTest.java (from rev 2930, common-core/trunk/src/test/java/org/jboss/test/util/test/collection/AbstractSetUnitTest.java)
===================================================================
--- common-core/trunk/src/test/java/org/jboss/test/util/test/collection/AbstractMapUnitTest.java	                        (rev 0)
+++ common-core/trunk/src/test/java/org/jboss/test/util/test/collection/AbstractMapUnitTest.java	2008-11-01 11:58:22 UTC (rev 2931)
@@ -0,0 +1,66 @@
+package org.jboss.test.util.test.collection;
+
+import java.util.Collections;
+import java.util.Date;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+/**
+ * Unit tests for custom maps.
+ *
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+public abstract class AbstractMapUnitTest extends TestCase
+{
+   protected abstract Map createEmptyMap();
+
+   @SuppressWarnings("unchecked")
+   public void testBasicOperations() throws Exception
+   {
+      Map map = createEmptyMap();
+      assertTrue(map.isEmpty());
+
+      String key = "date1";
+      Date value = new Date();
+      map.put(key, value);
+
+      assertTrue(map.containsKey(key));
+      assertTrue(map.containsValue(value));
+      assertEquals(1, map.size());
+
+      map.clear();
+      assertTrue(map.isEmpty());
+
+      key = "date1";
+      value = new Date();
+      map.put(key, value);
+
+      map.remove(key);
+      assertTrue(map.isEmpty());
+
+      map.putAll(Collections.singletonMap(key, value));
+
+      assertEquals(value, map.get(key));
+      assertEquals(map, Collections.singletonMap(key, value));
+      
+      // iterables
+      Iterable<String> keys = map.keySet();
+      assertIterable(keys, String.class);
+      Iterable<Date> values = map.values();
+      assertIterable(values, Date.class);
+      Iterable<Map.Entry> entries = map.entrySet();
+      Map.Entry entry = assertIterable(entries, Map.Entry.class);
+      assertEquals(key, entry.getKey());
+      assertEquals(value, entry.getValue());
+   }
+
+   protected <T> T assertIterable(Iterable<T> iter, Class<T> clazz)
+   {
+      assertTrue(iter.iterator().hasNext());
+      T next = iter.iterator().next();
+      assertTrue("Next " + next + " is not instance of " + clazz.getName(), clazz.isInstance(next));
+      assertNotNull(next);
+      return next;
+   }
+}
\ No newline at end of file

Added: common-core/trunk/src/test/java/org/jboss/test/util/test/collection/SoftValueMapUnitTestCase.java
===================================================================
--- common-core/trunk/src/test/java/org/jboss/test/util/test/collection/SoftValueMapUnitTestCase.java	                        (rev 0)
+++ common-core/trunk/src/test/java/org/jboss/test/util/test/collection/SoftValueMapUnitTestCase.java	2008-11-01 11:58:22 UTC (rev 2931)
@@ -0,0 +1,39 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.util.test.collection;
+
+import java.util.Map;
+
+import org.jboss.util.collection.SoftValueHashMap;
+
+/**
+ * SoftValueHashMap test.
+ * 
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class SoftValueMapUnitTestCase extends AbstractMapUnitTest
+{
+   protected Map createEmptyMap()
+   {
+      return new SoftValueHashMap();
+   }
+}

Added: common-core/trunk/src/test/java/org/jboss/test/util/test/collection/WeakValueMapUnitTestCase.java
===================================================================
--- common-core/trunk/src/test/java/org/jboss/test/util/test/collection/WeakValueMapUnitTestCase.java	                        (rev 0)
+++ common-core/trunk/src/test/java/org/jboss/test/util/test/collection/WeakValueMapUnitTestCase.java	2008-11-01 11:58:22 UTC (rev 2931)
@@ -0,0 +1,39 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.util.test.collection;
+
+import java.util.Map;
+
+import org.jboss.util.collection.WeakValueHashMap;
+
+/**
+ * WeakValueHashMap test.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class WeakValueMapUnitTestCase extends AbstractMapUnitTest
+{
+   protected Map createEmptyMap()
+   {
+      return new WeakValueHashMap();
+   }
+}
\ No newline at end of file




More information about the jboss-svn-commits mailing list