river-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From peter_firmst...@apache.org
Subject svn commit: r1175669 - in /river/jtsk/skunk/peterConcurrentPolicy: src/org/apache/river/impl/util/ test/src/org/apache/river/impl/util/
Date Mon, 26 Sep 2011 04:57:18 GMT
Author: peter_firmstone
Date: Mon Sep 26 04:57:17 2011
New Revision: 1175669

URL: http://svn.apache.org/viewvc?rev=1175669&view=rev
Log:
Reference Collection unit tests and refactor

Modified:
    river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/EntryConversionFactory.java
    river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/EntryFacadeConverter.java
    river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/EntrySetFacade.java
    river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/RC.java
    river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceCollection.java
    river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceComparator.java
    river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceEntryFacade.java
    river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceList.java
    river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceMap.java
    river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceSet.java
    river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceSortedMap.java
    river/jtsk/skunk/peterConcurrentPolicy/test/src/org/apache/river/impl/util/ReferenceListTest.java
    river/jtsk/skunk/peterConcurrentPolicy/test/src/org/apache/river/impl/util/ReferenceMapTest.java

Modified: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/EntryConversionFactory.java
URL: http://svn.apache.org/viewvc/river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/EntryConversionFactory.java?rev=1175669&r1=1175668&r2=1175669&view=diff
==============================================================================
--- river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/EntryConversionFactory.java
(original)
+++ river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/EntryConversionFactory.java
Mon Sep 26 04:57:17 2011
@@ -27,7 +27,7 @@ interface EntryConversionFactory<O, R> {
 
     O asFacade(R u);
 
-    R asReference(O w);
+    R asReference(O w, boolean enque);
 
     void processQueue();
     

Modified: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/EntryFacadeConverter.java
URL: http://svn.apache.org/viewvc/river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/EntryFacadeConverter.java?rev=1175669&r1=1175668&r2=1175669&view=diff
==============================================================================
--- river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/EntryFacadeConverter.java
(original)
+++ river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/EntryFacadeConverter.java
Mon Sep 26 04:57:17 2011
@@ -20,6 +20,7 @@ package org.apache.river.impl.util;
 
 import java.lang.ref.Reference;
 import java.lang.ref.ReferenceQueue;
+import java.util.AbstractMap.SimpleEntry;
 import java.util.Map.Entry;
 
 /**
@@ -27,32 +28,24 @@ import java.util.Map.Entry;
  * @author Peter Firmstone.
  */
 class EntryFacadeConverter<K,V> implements EntryConversionFactory<Entry<K,V>,
Entry<Reference<K>, Reference<V>>> {
-    private final Ref valRef;
-    private final ReferenceQueueProcessor rqp;
-    private final ReferenceQueue<V> queue;
-
-    EntryFacadeConverter(ReferenceQueue<V> queue, Ref valRef, 
-            ReferenceQueueProcessor rqp) {
-        this.queue = queue;
-        this.valRef = valRef;
-        this.rqp = rqp;
+    ReferenceMap<K,V> map;
+
+    EntryFacadeConverter(ReferenceMap<K,V> map) {
+        this.map = map;
     }
 
     public Entry<K,V> asFacade(Entry<Reference<K>, Reference<V>>
u) {
-        return new ReferenceEntryFacade<K, V>(u, valRef, queue);
+        return new ReferenceEntryFacade<K, V>(u, map.getValRef(), map.getValQueue());
     }
 
-    public Entry<Reference<K>, Reference<V>> asReference(Entry<K,V>
w) {
-        
-        // This is where we check the cast.
-        if (w instanceof ReferenceEntryFacade) {
-            return ((ReferenceEntryFacade<K,V>)w).reveal();
+    public Entry<Reference<K>, Reference<V>> asReference(Entry<K,V>
w, boolean enque) {
+        return new SimpleEntry<Reference<K>, Reference<V>>(
+            map.wrapKey(w.getKey(), enque), map.wrapVal(w.getValue(), enque)
+        );
         }
-        return null;
-    }
 
     public void processQueue() {
-        rqp.processQueue();
+        map.processQueue();
     }
     
 }

Modified: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/EntrySetFacade.java
URL: http://svn.apache.org/viewvc/river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/EntrySetFacade.java?rev=1175669&r1=1175668&r2=1175669&view=diff
==============================================================================
--- river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/EntrySetFacade.java
(original)
+++ river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/EntrySetFacade.java
Mon Sep 26 04:57:17 2011
@@ -18,16 +18,12 @@
 
 package org.apache.river.impl.util;
 
-import java.util.ArrayList;
-import java.util.Collection;
+import java.util.AbstractSet;
 import java.util.Iterator;
 import java.util.Set;
 
 /**
- * This doesn't support creation of, nor addition of Entry's
- * however it does support removal and retention.
- *
- * It simply disguises the contained set's reference from the
+ * EntrySetFacade disguises the contained set's reference from the
  * caller with a Facade, or exposes the underlying references to the set, when
  * returned by the caller.
  *
@@ -38,7 +34,7 @@ import java.util.Set;
  *
  * @author peter
  */
-class EntrySetFacade<O, R> implements Set<O> {
+class EntrySetFacade<O, R> extends AbstractSet<O> implements Set<O> {
     private Set<R> set;
     private EntryConversionFactory<O, R> factory;
 
@@ -57,92 +53,18 @@ class EntrySetFacade<O, R> implements Se
         return set.isEmpty();
     }
 
-    public boolean contains(Object o) {
-        factory.processQueue();
-        // cast is checked by factory and returns null if other.
-        R entryRef = factory.asReference((O) o);
-        // entryRef is null if wrong type.
-        if (entryRef == null) {
-            return false;
-        }
-        return set.contains(entryRef);
-    }
-
     public Iterator<O> iterator() {
         factory.processQueue();
         return new EntryIteratorFacade<O, R>(set.iterator(), factory);
     }
 
-    public Object[] toArray() {
-        factory.processQueue();
-        Collection<O> result = new ArrayList<O>(set.size());
-        Iterator<O> i = iterator();
-        while (i.hasNext()) {
-            result.add(i.next());
-        }
-        return result.toArray();
-    }
-
-    public boolean remove(Object o) {
-        factory.processQueue();
-        // cast is checked by factory.
-        R entryRef = factory.asReference((O) o);
-        if (entryRef == null) {
-            return false;
-        }
-        return set.remove(entryRef);
-    }
-
-    private Collection<R> asReferenceCollection(Collection<?> c) {
-        int l = c == null ? 0 : c.size();
-        Collection<R> refcoll = new ArrayList<R>(l);
-        if (l > 0) {
-            Iterator<?> i = c.iterator();
-            while (i.hasNext()) {
-                // Factory checks instanceof, we don't have the knowledge here.
-                @SuppressWarnings(value = "unchecked")
-                R ref = factory.asReference((O) i.next());
-                if (ref != null) {
-                    refcoll.add(ref);
-                }
-            }
-        }
-        return refcoll;
-    }
-
-    public boolean containsAll(Collection<?> c) {
-        return set.containsAll(asReferenceCollection(c));
-    }
-
-    public boolean retainAll(Collection<?> c) {
-        return set.retainAll(asReferenceCollection(c));
-    }
-
-    public boolean removeAll(Collection<?> c) {
-        return set.removeAll(asReferenceCollection(c));
-    }
-
     public void clear() {
         set.clear();
     }
 
-    @SuppressWarnings(value = "unchecked")
-    public <O> O[] toArray(O[] a) {
-        a = set.toArray(a);
-        int l = a.length;
-        for (int i = 0; i < l; i++) {
-            a[i] = (O) factory.asFacade((R) a[i]);
-        }
-        return a;
-    }
-
     public boolean add(O e) {
-        throw new UnsupportedOperationException("Unsupported by most concurrent collection
implementations.");
+        factory.processQueue();
+        if ( e == null ) return false;
+        return set.add(factory.asReference(e, true));
     }
-
-    @Override
-    public boolean addAll(Collection<? extends O> c) {
-        throw new UnsupportedOperationException("Unsupported by most concurrent collection
implementations.");
     }
-    
-}

Modified: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/RC.java
URL: http://svn.apache.org/viewvc/river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/RC.java?rev=1175669&r1=1175668&r2=1175669&view=diff
==============================================================================
--- river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/RC.java (original)
+++ river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/RC.java Mon Sep
26 04:57:17 2011
@@ -91,10 +91,15 @@ import java.util.concurrent.ConcurrentNa
  * Sub Sets or Sub Maps are views only.
  * </p><p>
  * {@link Map#entrySet() } view instances returned preserve your chosen reference
- * behaviour, they do not support {@link Set#add(java.lang.Object)} or 
- * {@link Set#addAll(java.util.Collection)} methods, which throw
- * an {@link UnsupportedOperationException}.  All other methods are fully implemented
- * and supported.
+ * behaviour, they even support {@link Set#add(java.lang.Object)} or 
+ * {@link Set#addAll(java.util.Collection)} methods, although you'll be hard
+ * pressed to find a standard java implementation that does.  If you have a
+ * Map with a Set of Entry's implementing add, the implementation will need a 
+ * Comparator, that compares Entry's only by their keys, to avoid duplicating
+ * keys, primarily because an Entry hashCode includes the both key and value in its 
+ * calculation. {@link Entry#hashCode() }
+ * </p><p>
+ * All other {@link Map#entrySet() } methods are fully implemented and supported.
  * </p><p>
  * {@link Entry} view instances returned by these methods preserve reference
  * behaviour, all methods are fully implemented and supported.
@@ -137,7 +142,7 @@ public class RC {
      * @param comparator
      * @return
      */
-    public static <T> Comparator<Reference<T>> comparator(Comparator<T>
comparator){
+    public static <T> Comparator<Reference<T>> comparator(Comparator<?
super T> comparator){
         return new ReferenceComparator<T>(comparator);
     }
     

Modified: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceCollection.java
URL: http://svn.apache.org/viewvc/river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceCollection.java?rev=1175669&r1=1175668&r2=1175669&view=diff
==============================================================================
--- river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceCollection.java
(original)
+++ river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceCollection.java
Mon Sep 26 04:57:17 2011
@@ -23,6 +23,8 @@ import java.lang.ref.ReferenceQueue;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
 
 /**
  * A Collection of Reference Objects, the developer may chose any Collection
@@ -185,22 +187,54 @@ class ReferenceCollection<T> implements 
 
     @Override
     public int hashCode() {
+        if ( col instanceof List || col instanceof Set ){
+            return col.hashCode();
+        }
         int hash = 7;
-        hash = 73 * hash + (this.col != null ? this.col.hashCode() : 0);
-        hash = 73 * hash + (this.type != null ? this.type.hashCode() : 0);
-        hash = 73 * hash + (this.getClass().hashCode());
+        hash = 67 * hash + (this.col != null ? this.col.hashCode() : 0);
+        hash = 67 * hash + (this.type != null ? this.type.hashCode() : 0);
         return hash;
     }
     
+    /**
+     * Because equals and hashCode are not defined for collections, we 
+     * cannot guarantee consistent behaviour by implementing equals and
+     * hashCode.  A collection could be a list, set, queue or deque.
+     * So a List != Queue and a Set != list. therefore equals for collections is
+     * not defined.
+     * 
+     * However since two collections may both also be Lists, while abstracted
+     * from the client two lists may still be equal.
+     * @see Collection#equals(java.lang.Object) 
+     */
+    
     @Override
     public boolean equals(Object o){
         if ( o == this ) return true;
-        if ( hashCode() != o.hashCode()) return false;
-        if (!(this.getClass().equals(o.getClass()))) return false;
-        if (!( o instanceof ReferenceCollection)) return false;
-        ReferenceCollection that = (ReferenceCollection) o;
-        if ( type.equals(that.type) && col.equals(that.col)) return true;
+        if ( col instanceof List || col instanceof Set ){
+            return col.equals(o);
+        }
         return false;
     }
     
+    
+//    @Override
+//    public int hashCode() {
+//        int hash = 7;
+//        hash = 73 * hash + (this.col != null ? this.col.hashCode() : 0);
+//        hash = 73 * hash + (this.type != null ? this.type.hashCode() : 0);
+//        hash = 73 * hash + (this.getClass().hashCode());
+//        return hash;
+//    }
+//    
+//    @Override
+//    public boolean equals(Object o){
+//        if ( o == this ) return true;
+//        if ( hashCode() != o.hashCode()) return false;
+//        if (!( o instanceof Collection)) return false;
+//        ReferenceCollection that = (ReferenceCollection) o;
+//        if ( type.equals(that.type) && col.equals(that.col)) return true;
+//        return false;
+//    }
+    
 }

Modified: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceComparator.java
URL: http://svn.apache.org/viewvc/river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceComparator.java?rev=1175669&r1=1175668&r2=1175669&view=diff
==============================================================================
--- river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceComparator.java
(original)
+++ river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceComparator.java
Mon Sep 26 04:57:17 2011
@@ -27,8 +27,8 @@ import java.util.Comparator;
  * @author Peter Firmstone.
  */
 class ReferenceComparator<T> implements Comparator<Reference<T>> {
-    private final Comparator<T> comparator;
-    ReferenceComparator(Comparator<T> comparator){
+    private final Comparator<? super T> comparator;
+    ReferenceComparator(Comparator<? super T> comparator){
         this.comparator = comparator;
     }
 

Modified: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceEntryFacade.java
URL: http://svn.apache.org/viewvc/river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceEntryFacade.java?rev=1175669&r1=1175668&r2=1175669&view=diff
==============================================================================
--- river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceEntryFacade.java
(original)
+++ river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceEntryFacade.java
Mon Sep 26 04:57:17 2011
@@ -22,13 +22,12 @@ import java.lang.ref.Reference;
 import java.lang.ref.ReferenceQueue;
 import java.util.Map;
 import java.util.Map.Entry;
-import org.apache.river.api.util.Facade;
 
 /**
  * 
  * @author Peter Firmstone.
  */
-class ReferenceEntryFacade<K, V> implements Map.Entry<K, V>, Facade<Entry<Reference<K>,Reference<V>>>
{
+class ReferenceEntryFacade<K, V> implements Map.Entry<K, V> {
     private final Map.Entry<Reference<K>, Reference<V>> entry;
     private final ReferenceQueue<V> queue;
     private final Ref valRef;
@@ -52,12 +51,34 @@ class ReferenceEntryFacade<K, V> impleme
         return entry.setValue(wrapVal(value, true)).get();
     }
 
+    /**
+     * Implementation copied directly from Entry javadoc.
+     * @see Entry#hashCode() 
+     * @return 
+     */
+    @Override
+    public int hashCode() {
+        return (getKey()==null   ? 0 : getKey().hashCode()) ^
+             (getValue()==null ? 0 : getValue().hashCode());
+    }
+
+    /**
+     * Implementation copied directly from Entry javadoc.
+     * @see Entry#equals(java.lang.Object) 
+     */
+    public boolean equals(Object o){
+        if ( o == this ) return true;
+        if ( !(o instanceof Entry) ) return false;
+        Entry e1 = this;
+        Entry e2 = (Entry) o;   
+        return ( (e1.getKey()==null ? e2.getKey()==null : 
+                e1.getKey().equals(e2.getKey()))  &&
+                (e1.getValue()==null ? e2.getValue()==null : 
+                e1.getValue().equals(e2.getValue())));
+    }
+    
     private Reference<V> wrapVal(V val, boolean enque) {
         return ReferenceFactory.create(val, enque == true ? queue : null, valRef);
-    }
+}
 
-    public Entry<Reference<K>, Reference<V>> reveal() {
-        return entry;
-    }
-    
 }

Modified: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceList.java
URL: http://svn.apache.org/viewvc/river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceList.java?rev=1175669&r1=1175668&r2=1175669&view=diff
==============================================================================
--- river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceList.java
(original)
+++ river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceList.java
Mon Sep 26 04:57:17 2011
@@ -21,6 +21,7 @@ package org.apache.river.impl.util;
 import java.lang.ref.Reference;
 import java.lang.ref.ReferenceQueue;
 import java.util.Collection;
+import java.util.Iterator;
 import java.util.List;
 import java.util.ListIterator;
 
@@ -56,6 +57,48 @@ class ReferenceList<T> extends Reference
         this.list = list;
     }
 
+    /**
+     * Implemented as per the List interface definition of equals.
+     * @see List#equals(java.lang.Object) 
+     * @param o
+     * @return 
+     */
+    public boolean equals(Object o){
+        if ( o == null ) return false;
+        if (!( o instanceof List)) return false;
+        List l = (List) o;
+        if ( l.size() != size()) return false;
+        Iterator<T> li = iterator();
+        int i = 0;
+        while(li.hasNext()){
+            T t = li.next();
+            if ( t != null ){
+                if ( !(t.equals(l.get(i))) ) return false;
+            } else {
+                if ( l.get(i) != null ) return false; // both must be null
+            }
+            i++;
+        }
+        return true;
+    }
+    
+    /**
+     * Implemented as per List interface definition.
+     * 
+     * @see List#hashCode() 
+     * @return 
+     */
+    public int hashCode() {
+        // hash code calculation copied directly from List interface contract.
+        int hashCode = 1;
+        Iterator<T> i = iterator();
+        while (i.hasNext()) {
+            T obj = i.next();
+            hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
+        }
+        return hashCode;
+    }
+
     public boolean addAll(int index, Collection<? extends T> c) {
         processQueue();
         return list.addAll(index, wrapColl(c, true));

Modified: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceMap.java
URL: http://svn.apache.org/viewvc/river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceMap.java?rev=1175669&r1=1175668&r2=1175669&view=diff
==============================================================================
--- river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceMap.java
(original)
+++ river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceMap.java
Mon Sep 26 04:57:17 2011
@@ -20,6 +20,7 @@ package org.apache.river.impl.util;
 
 import java.lang.ref.Reference;
 import java.lang.ref.ReferenceQueue;
+import java.util.AbstractMap;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -45,11 +46,15 @@ import java.util.Set;
  * Collection of Key's or Values.  Defensive copies cannot be made by the
  * underlying encapsulated collections.
  * 
+ * Abstract map is extended to take advantage of it's equals, hashCode and 
+ * toString methods, which rely on calling entrySet() which this class
+ * overrides.
+ * 
  * @param <K> 
  * @param <V> 
  * @author Peter Firmstone.
  */
-class ReferenceMap<K, V> implements Map<K, V>, ReferenceQueueProcessor {
+class ReferenceMap<K, V> extends AbstractMap<K, V> implements Map<K, V>,
ReferenceQueueProcessor {
 
     // ConcurrentHashMap must be protected from null values;
     private final Ref keyRef;
@@ -59,6 +64,7 @@ class ReferenceMap<K, V> implements Map<
     private final ReferenceQueue<V> valQueue;
     
     ReferenceMap(Map<Reference<K>,Reference<V>> map, Ref key, Ref val){
+        super();
         if (map == null || key == null || val == null ) throw new IllegalArgumentException("Null
not allowed");
         this.map = map;
         keyQueue = new ReferenceQueue<K>();
@@ -67,6 +73,7 @@ class ReferenceMap<K, V> implements Map<
         valRef = val;
     }
     ReferenceMap(Map<Reference<K>,Reference<V>> map, Ref key, Ref val,
ReferenceQueue<K> keyQueue, ReferenceQueue<V> valQueue){
+        super();
         if (map == null || key == null || val == null ) throw new IllegalArgumentException("Null
not allowed");
         this.map = map;
         this.keyQueue = keyQueue;
@@ -74,45 +81,22 @@ class ReferenceMap<K, V> implements Map<
         keyRef = key;
         valRef = val;
     }
-     protected ReferenceQueue<V> getValQueue(){
+     ReferenceQueue<V> getValQueue(){
          return valQueue;
      }
      
-     protected ReferenceQueue<K> getKeyQueue(){
+     ReferenceQueue<K> getKeyQueue(){
          return keyQueue;
      }
      
-     /*
-      * The following three methods are suitable implementations for subclasses also.
-      */
-    @Override
-    public int hashCode() {
-        int hash = 3;
-        hash = 47 * hash + (this.keyRef != null ? this.keyRef.hashCode() : 0);
-        hash = 47 * hash + (this.valRef != null ? this.valRef.hashCode() : 0);
-        hash = 47 * hash + (this.map != null ? this.map.hashCode() : 0);
-        hash = 47 * hash + (this.keyQueue != null ? this.keyQueue.hashCode() : 0);
-        hash = 47 * hash + (this.valQueue != null ? this.valQueue.hashCode() : 0);
-        hash = 47 * hash + (this.getClass().hashCode());
-        return hash;
+     Ref getKeyRef(){
+         return keyRef;
     }
     
-    public boolean equals(Object o){
-        if (o == this) return true;
-        if ( !(this.getClass().equals(o.getClass())) ) return false;
-        if (!( o instanceof ReferenceMap)) return false;
-        ReferenceMap that = (ReferenceMap) o;
-        if ( !this.keyRef.equals(that.keyRef) ) return false;
-        if ( !this.valRef.equals(that.valRef) ) return false;
-        if ( !this.keyQueue.equals(that.keyQueue) ) return false;
-        if ( !this.valQueue.equals(that.valQueue) ) return false;
-        return ( map.equals(that.map));
+     Ref getValRef(){
+         return valRef;
     }
     
-    public String toString(){
-        return map.toString();
-    }
-
     /**
      * Removes all associations from this map.
      */
@@ -157,7 +141,7 @@ class ReferenceMap<K, V> implements Map<
     public Set<Entry<K,V>> entrySet() {
         return new EntrySetFacade<Entry<K,V>, Entry<Reference<K>,Reference<V>>>(
                 map.entrySet(), 
-                new EntryFacadeConverter<K,V>(valQueue, valRef, this)
+                new EntryFacadeConverter<K,V>(this)
                 );
     }
 

Modified: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceSet.java
URL: http://svn.apache.org/viewvc/river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceSet.java?rev=1175669&r1=1175668&r2=1175669&view=diff
==============================================================================
--- river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceSet.java
(original)
+++ river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceSet.java
Mon Sep 26 04:57:17 2011
@@ -20,6 +20,8 @@ package org.apache.river.impl.util;
 
 import java.lang.ref.Reference;
 import java.lang.ref.ReferenceQueue;
+import java.util.Collection;
+import java.util.Iterator;
 import java.util.Set;
 
 /**
@@ -38,4 +40,33 @@ class ReferenceSet<T> extends ReferenceC
     ReferenceSet(Set<Reference<T>> col, Ref type, ReferenceQueue<T> queue){
         super(col, type, queue);
     }
+    
+    public boolean equals(Object o) {
+	if (o == this) return true;
+	if (!(o instanceof Set)) return false;
+        @SuppressWarnings("unchecked")
+	Set<T> s = (Set<T>) o;
+	if (s.size() != size())
+	    return false;
+        try {
+            return containsAll(s);
+        } catch (ClassCastException e)   {
+            return false;
+        } catch (NullPointerException e) {
+            return false;
+}
+    }
+
+    @Override
+    public int hashCode() {
+        int hash = 0;
+        Iterator<T> i = iterator();
+        while (i.hasNext()){
+            T next = i.next();
+            if ( next != null) {
+                hash = hash + next.hashCode();
+            }
+        }
+        return hash;
+    }
 }

Modified: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceSortedMap.java
URL: http://svn.apache.org/viewvc/river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceSortedMap.java?rev=1175669&r1=1175668&r2=1175669&view=diff
==============================================================================
--- river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceSortedMap.java
(original)
+++ river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceSortedMap.java
Mon Sep 26 04:57:17 2011
@@ -62,7 +62,7 @@ class ReferenceSortedMap<K,V> extends Re
     public SortedMap<K, V> subMap(K fromKey, K toKey) {
         processQueue();
         return new ReferenceSortedMap<K,V>(
-                map.subMap(wrapKey(fromKey, false), wrapKey(fromKey, false)),
+                map.subMap(wrapKey(fromKey, false), wrapKey(toKey, false)),
                 keyRef,
                 valRef,
                 getKeyQueue(),

Modified: river/jtsk/skunk/peterConcurrentPolicy/test/src/org/apache/river/impl/util/ReferenceListTest.java
URL: http://svn.apache.org/viewvc/river/jtsk/skunk/peterConcurrentPolicy/test/src/org/apache/river/impl/util/ReferenceListTest.java?rev=1175669&r1=1175668&r2=1175669&view=diff
==============================================================================
--- river/jtsk/skunk/peterConcurrentPolicy/test/src/org/apache/river/impl/util/ReferenceListTest.java
(original)
+++ river/jtsk/skunk/peterConcurrentPolicy/test/src/org/apache/river/impl/util/ReferenceListTest.java
Mon Sep 26 04:57:17 2011
@@ -18,6 +18,7 @@
 
 package org.apache.river.impl.util;
 
+import java.util.LinkedList;
 import java.lang.ref.Reference;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -69,6 +70,26 @@ public class ReferenceListTest {
     }
 
     /**
+     * Test of equals method, of class ReferenceList.
+     */
+     @Test
+    public void testEquals() {
+        System.out.println("testEquals");
+        List<String> set1 = new ArrayList<String>(3);
+        List<String> set2 = RC.list(new ArrayList<Reference<String>>(),
Ref.SOFT);
+        String s1 = "1", s2 = "2", s3 = "3";
+        set1.add(s1);
+        set1.add(s2);
+        set1.add(s3);
+        set2.add(s1);
+        set2.add(s2);
+        set2.add(s3);
+        assertTrue(set1.equals(set2));
+        assertTrue(set2.equals(set1));
+        assertTrue(set1.hashCode() == set2.hashCode());
+    }
+
+    /**
      * Test of addAll method, of class ReferenceList.
      */
     @Test

Modified: river/jtsk/skunk/peterConcurrentPolicy/test/src/org/apache/river/impl/util/ReferenceMapTest.java
URL: http://svn.apache.org/viewvc/river/jtsk/skunk/peterConcurrentPolicy/test/src/org/apache/river/impl/util/ReferenceMapTest.java?rev=1175669&r1=1175668&r2=1175669&view=diff
==============================================================================
--- river/jtsk/skunk/peterConcurrentPolicy/test/src/org/apache/river/impl/util/ReferenceMapTest.java
(original)
+++ river/jtsk/skunk/peterConcurrentPolicy/test/src/org/apache/river/impl/util/ReferenceMapTest.java
Mon Sep 26 04:57:17 2011
@@ -18,6 +18,8 @@
 
 package org.apache.river.impl.util;
 
+import tests.support.MutableMap;
+import java.util.TreeMap;
 import java.util.Iterator;
 import java.util.Map.Entry;
 import java.util.List;
@@ -71,6 +73,151 @@ public class ReferenceMapTest {
     public void tearDown() {
     }
 
+    @Test
+    public void testEquals(){
+        Map<Integer, String> m1 = new HashMap<Integer, String>();
+        m1.put(i1, s1);
+        m1.put(i2, s2);
+        m1.put(i3, s3);
+        Map<Integer, String> m2 = RC.map(
+            new TreeMap<Reference<Integer>, Reference<String>>(),
+            Ref.SOFT, Ref.SOFT
+        );
+        m2.put(i1, s1);
+        m2.put(i2, s2);
+        m2.put(i3, s3);
+        assertTrue(m1.equals(m2));
+        assertTrue(m2.equals(m1));
+        assertEquals( m1.hashCode(), m2.hashCode());
+    }
+    
+    @Test
+    public void testEntrySetEquals(){
+        Map<Integer, String> m1 = new HashMap<Integer, String>();
+        m1.put(i1, s1);
+        m1.put(i2, s2);
+        m1.put(i3, s3);
+        Map<Integer, String> m2 = RC.map(
+            new TreeMap<Reference<Integer>, Reference<String>>(),
+            Ref.SOFT, Ref.SOFT
+        );
+        m2.put(i1, s1);
+        m2.put(i2, s2);
+        m2.put(i3, s3);
+        Set<Entry<Integer, String>> set1 = m1.entrySet();
+        Set<Entry<Integer, String>> set2 = m2.entrySet();
+        assertTrue(set1.equals(set2));
+        assertTrue(set2.equals(set1));
+        assertEquals(set1.hashCode(), set2.hashCode());
+    }
+    
+    @Test 
+    public void testEntrySetRemoveAll(){
+        Map<Integer, String> m1 = new HashMap<Integer, String>();
+        m1.put(i1, s1);
+        m1.put(i2, s2);
+        m1.put(i3, s3);
+        Map<Integer, String> m2 = RC.map(
+            new TreeMap<Reference<Integer>, Reference<String>>(),
+            Ref.SOFT, Ref.SOFT
+        );
+        m2.put(i1, s1);
+        m2.put(i2, s2);
+        m2.put(i3, s3);
+        Set<Entry<Integer, String>> set1 = m1.entrySet();
+        Set<Entry<Integer, String>> set2 = m2.entrySet();
+        set2.removeAll(set1);
+        assertTrue(set2.isEmpty());
+    }
+    
+    @Test
+    public void testEntrySetRetainAll(){
+        Map<Integer, String> m1 = new HashMap<Integer, String>();
+        m1.put(i1, s1);
+        m1.put(i2, s2);
+        m1.put(i3, s3);
+        Map<Integer, String> m2 = RC.map(
+            new TreeMap<Reference<Integer>, Reference<String>>(),
+            Ref.SOFT, Ref.SOFT
+        );
+        m2.put(i1, s1);
+        m2.put(i2, s2);
+        m2.put(i3, s3);
+        Set<Entry<Integer, String>> set1 = m1.entrySet();
+        Set<Entry<Integer, String>> set2 = m2.entrySet();
+        set2.retainAll(set1);
+        assertFalse(set2.isEmpty());
+    }
+    
+    @Test
+    public void testEntryContainsAll(){
+        Map<Integer, String> m1 = new HashMap<Integer, String>();
+        m1.put(i1, s1);
+        m1.put(i2, s2);
+        m1.put(i3, s3);
+        Map<Integer, String> m2 = RC.map(
+            new TreeMap<Reference<Integer>, Reference<String>>(),
+            Ref.SOFT, Ref.SOFT
+        );
+        m2.put(i1, s1);
+        m2.put(i2, s2);
+        m2.put(i3, s3);
+        Map<Integer, String> m3 = new HashMap<Integer, String>();
+        m3.put(i1, s1);
+        m3.put(10, "Ten");
+        Set<Entry<Integer, String>> set1 = m1.entrySet();
+        Set<Entry<Integer, String>> set2 = m2.entrySet();
+        Set<Entry<Integer, String>> set3 = m3.entrySet();
+        assertTrue(set2.containsAll(set1));
+        assertFalse(set2.containsAll(set3));
+    }
+
+   @Test
+    public void testEntrySetRemoveContains(){
+        Map<Integer, String> m1 = new HashMap<Integer, String>();
+        m1.put(i1, s1);
+        m1.put(i2, s2);
+        m1.put(i3, s3);
+        Map<Integer, String> m2 = RC.map(
+            new TreeMap<Reference<Integer>, Reference<String>>(),
+            Ref.SOFT, Ref.SOFT
+        );
+        m2.put(i1, s1);
+        m2.put(i2, s2);
+        m2.put(i3, s3);
+        Set<Entry<Integer, String>> set1 = m1.entrySet();
+        Set<Entry<Integer, String>> set2 = m2.entrySet();
+        assertTrue(set2.containsAll(set1));
+        Iterator<Entry<Integer, String>> it1 = set1.iterator();
+        while (it1.hasNext()){
+            Entry<Integer, String> e = it1.next();
+            set2.remove(e);
+            assertFalse(set2.contains(e));
+        }
+        assertTrue(set2.isEmpty());
+    }
+    
+   @Test
+   public void testEntrySetAdd(){
+       Map<Integer, String> m1 = new HashMap<Integer, String>();
+        m1.put(i1, s1);
+        m1.put(i2, s2);
+        m1.put(i3, s3);
+        Map<Integer, String> m2 = RC.map(
+            new MutableMap<Reference<Integer>, Reference<String>>(),
+            Ref.SOFT, Ref.SOFT
+        );
+        Set<Entry<Integer, String>> set1 = m1.entrySet();
+        Set<Entry<Integer, String>> set2 = m2.entrySet();
+        assertTrue(set2.isEmpty());
+        Iterator<Entry<Integer, String>> it1 = set1.iterator();
+        while (it1.hasNext()){
+            Entry<Integer, String> e = it1.next();
+            set2.add(e);
+        }
+        assertTrue(set2.containsAll(set1));
+   }
+   
     /**
      * Test of containsKey method, of class ReferenceMap.
      */



Mime
View raw message