You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by gn...@apache.org on 2009/05/26 20:52:32 UTC

svn commit: r778844 - in /geronimo/sandbox/blueprint/blueprint-core/src: main/java/org/apache/geronimo/blueprint/container/ main/java/org/apache/geronimo/blueprint/utils/ test/java/org/apache/geronimo/blueprint/utils/

Author: gnodet
Date: Tue May 26 18:52:31 2009
New Revision: 778844

URL: http://svn.apache.org/viewvc?rev=778844&view=rev
Log:
Simplify dynamic collections

Removed:
    geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/utils/DynamicList.java
    geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/utils/DynamicSet.java
    geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/utils/DynamicSortedList.java
    geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/utils/DynamicSortedSet.java
Modified:
    geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/container/RefCollectionRecipe.java
    geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/utils/DynamicCollection.java
    geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/utils/DynamicCollectionTest.java

Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/container/RefCollectionRecipe.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/container/RefCollectionRecipe.java?rev=778844&r1=778843&r2=778844&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/container/RefCollectionRecipe.java (original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/container/RefCollectionRecipe.java Tue May 26 18:52:31 2009
@@ -30,17 +30,12 @@
 import java.util.ListIterator;
 import java.util.RandomAccess;
 import java.util.Set;
-import java.util.SortedSet;
 
 import net.sf.cglib.proxy.Dispatcher;
 import org.apache.geronimo.blueprint.ExtendedBlueprintContainer;
 import org.apache.geronimo.blueprint.di.Recipe;
 import org.apache.geronimo.blueprint.utils.ConversionUtils;
 import org.apache.geronimo.blueprint.utils.DynamicCollection;
-import org.apache.geronimo.blueprint.utils.DynamicList;
-import org.apache.geronimo.blueprint.utils.DynamicSet;
-import org.apache.geronimo.blueprint.utils.DynamicSortedList;
-import org.apache.geronimo.blueprint.utils.DynamicSortedSet;
 import org.apache.geronimo.blueprint.utils.TypeUtils;
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceReference;
@@ -94,13 +89,13 @@
             }
             if (metadata.getCollectionType() == List.class) {
                 if (comparator != null) {
-                    collection = new ManagedSortedList(memberReferences, orderReferences, comparator);
+                    collection = new ManagedList(memberReferences, orderReferences, comparator);
                 } else {
                     collection = new ManagedList(memberReferences);
                 }
             } else if (metadata.getCollectionType() == Set.class) {
                 if (comparator != null) {
-                    collection = new ManagedSortedSet(memberReferences, orderReferences, comparator);
+                    collection = new ManagedSet(memberReferences, orderReferences, comparator);
                 } else {
                     collection = new ManagedSet(memberReferences);
                 }
@@ -212,7 +207,20 @@
             }
             return service;
         }
-        
+
+        @Override
+        public boolean equals(Object o) {
+            if (this == o) return true;
+            if (o == null || getClass() != o.getClass()) return false;
+            ServiceDispatcher that = (ServiceDispatcher) o;
+            if (this.getMember() != null ? !this.getMember().equals(that.getMember()) : that.getMember() != null) return false;
+            return true;
+        }
+
+        @Override
+        public int hashCode() {
+            return getMember() != null ? getMember().hashCode() : 0;
+        }
     }
 
     public static class DispatcherComparator implements Comparator<ServiceDispatcher> {
@@ -283,7 +291,7 @@
                 references = false;
             }
             LOGGER.debug("Retrieving member in ManagedCollection references={}", references);
-            return references.booleanValue();
+            return references;
         }
         
         public boolean addDispatcher(ServiceDispatcher dispatcher) {
@@ -371,15 +379,12 @@
 
     public static class ManagedList extends ManagedCollection implements List, RandomAccess {
 
-        protected DynamicList<ServiceDispatcher> dispatchers;
-
         public ManagedList(Boolean references) {
-            this(references,  new DynamicList<ServiceDispatcher>());
+            super(references,  new DynamicCollection<ServiceDispatcher>(true, null));
         }
 
-        protected ManagedList(Boolean references, DynamicList<ServiceDispatcher> dispatchers) {
-            super(references, dispatchers);
-            this.dispatchers = dispatchers;
+        public ManagedList(Boolean references, boolean orderingReferences, Comparator comparator) {
+            super(references, new DynamicCollection<ServiceDispatcher>(true, new DispatcherComparator(comparator, orderingReferences)));
         }
 
         @Override
@@ -422,7 +427,7 @@
         }
 
         public ListIterator listIterator(int index) {
-            return new ManagedListIterator(dispatchers.listIterator(index));
+            return new ManagedListIterator(dispatchers.iterator(index));
         }
 
         public List<ServiceDispatcher> subList(int fromIndex, int toIndex) {
@@ -492,73 +497,14 @@
 
     }
 
-    public static class ManagedSortedList extends ManagedList {
-
-        public ManagedSortedList(Boolean references, boolean orderingReferences, Comparator comparator) {
-            super(references, new DynamicSortedList<ServiceDispatcher>(new DispatcherComparator(comparator, orderingReferences)));
-        }
-
-    }
-
     public static class ManagedSet extends ManagedCollection implements Set {
 
         public ManagedSet(Boolean references) {
-            super(references, new DynamicServiceDispatcherSet());
-        }
-        
-        protected ManagedSet(Boolean references, DynamicSet<ServiceDispatcher> dispatchers) {
-            super(references, dispatchers);
+            super(references, new DynamicCollection<ServiceDispatcher>(false, null));
         }
         
-    }
-    
-    private static class DynamicServiceDispatcherSet extends DynamicSet<ServiceDispatcher> {
-        public boolean add(ServiceDispatcher newDispatcher) {
-            synchronized (lock) {
-                for (ServiceDispatcher dispatcher : storage) {
-                    if (dispatcher.getMember().equals(newDispatcher.getMember())) {
-                        return false;
-                    }
-                }
-                storage.add(newDispatcher);
-                return true;
-            }
-        }
-    }
-    
-    public static class ManagedSortedSet extends ManagedSet implements SortedSet {
-
-        protected final DynamicSortedSet<ServiceDispatcher> dispatchers;
-        protected final Comparator comparator;
-
-        public ManagedSortedSet(Boolean references, boolean orderingReferences, Comparator comparator) {
-            super(references, new DynamicSortedSet<ServiceDispatcher>(new DispatcherComparator(comparator, orderingReferences)));
-            this.dispatchers = (DynamicSortedSet<ServiceDispatcher>) super.dispatchers;
-            this.comparator =  comparator;
-        }
-
-        public Comparator comparator() {
-            return comparator;
-        }
-
-        public SortedSet subSet(Object fromElement, Object toElement) {
-            throw new UnsupportedOperationException("Not implemented");
-        }
-
-        public SortedSet headSet(Object toElement) {
-            throw new UnsupportedOperationException("Not implemented");
-        }
-
-        public SortedSet tailSet(Object fromElement) {
-            throw new UnsupportedOperationException("Not implemented");
-        }
-
-        public Object first() {
-            return dispatchers.first().getMember();
-        }
-
-        public Object last() {
-            return dispatchers.last().getMember();
+        public ManagedSet(Boolean references, boolean orderingReferences, Comparator comparator) {
+            super(references, new DynamicCollection<ServiceDispatcher>(false, new DispatcherComparator(comparator, orderingReferences)));
         }
 
     }

Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/utils/DynamicCollection.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/utils/DynamicCollection.java?rev=778844&r1=778843&r2=778844&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/utils/DynamicCollection.java (original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/utils/DynamicCollection.java Tue May 26 18:52:31 2009
@@ -25,6 +25,9 @@
 import java.util.Iterator;
 import java.util.List;
 import java.util.NoSuchElementException;
+import java.util.Comparator;
+import java.util.Collections;
+import java.util.ListIterator;
 
 /**
  * Collection that allows iterators to see addition or removals of elements while iterating.
@@ -37,20 +40,24 @@
  */
 public class DynamicCollection<E> extends AbstractCollection<E> {
 
+    protected final boolean allowDuplicates;
+    protected final Comparator comparator;
     protected final Object lock = new Object();
     protected final List<E> storage;
     protected final List<WeakReference<DynamicIterator>> iterators;
 
-    public DynamicCollection() {
+    public DynamicCollection(boolean allowDuplicates, Comparator comparator) {
+        this.allowDuplicates = allowDuplicates;
+        this.comparator = comparator;
         this.storage = new ArrayList<E>();
         this.iterators = new ArrayList<WeakReference<DynamicIterator>>();
     }
 
-    public Iterator<E> iterator() {
+    public DynamicIterator iterator() {
         return iterator(0);
     }
 
-    public Iterator<E> iterator(int index) {
+    public DynamicIterator iterator(int index) {
         DynamicIterator iterator = createIterator(index);
         synchronized (lock) {
             for (Iterator<WeakReference<DynamicIterator>> it = iterators.iterator(); it.hasNext();) {
@@ -109,7 +116,38 @@
             throw new NullPointerException();
         }
         synchronized (lock) {
-            return storage.add(o);
+            if (comparator != null) {
+                if (allowDuplicates) {
+                    int index = Collections.binarySearch(storage, o, comparator);
+                    if (index < 0) {
+                        index = -index - 1;
+                    } else {
+                        index = index + 1;
+                    }
+                    internalAdd(index, o);
+                    return true;
+                } else {
+                    int index = Collections.binarySearch(storage, o, comparator);
+                    if (index < 0) {
+                        internalAdd(-index - 1, o);
+                        return true;
+                    } else {
+                        return false;
+                    }
+                }
+            } else {
+                if (allowDuplicates) {
+                    internalAdd(storage.size(), o);
+                    return true;
+                } else {
+                    if (!storage.contains(o)) {
+                        internalAdd(storage.size(), o);
+                        return true;
+                    } else {
+                        return false;
+                    }
+                }
+            }
         }
     }
 
@@ -123,7 +161,13 @@
         }
     }
 
-    public void add(int index, E o) {
+    public E get(int index) {
+        synchronized (lock) {
+            return storage.get(index);
+        }
+    }
+
+    private void internalAdd(int index, E o) {
         if (o == null) {
             throw new NullPointerException();
         }
@@ -140,6 +184,13 @@
         }
     }
 
+    @Override
+    public void clear() {
+        synchronized (lock) {
+            storage.clear();
+        }
+    }
+
     public E remove(int index) {
         synchronized (lock) {
             E o = storage.remove(index);
@@ -156,7 +207,27 @@
         }
     }
 
-    public class DynamicIterator implements Iterator<E> {
+    public E first() {
+        synchronized (lock) {
+            if (storage.isEmpty()) {
+                throw new NoSuchElementException();
+            } else {
+                return storage.get(0);
+            }
+        }
+    }
+
+    public E last() {
+        synchronized (lock) {
+            if (storage.isEmpty()) {
+                throw new NoSuchElementException();
+            } else {
+                return storage.get(storage.size() - 1);
+            }
+        }
+    }
+
+    public class DynamicIterator implements ListIterator<E> {
 
         protected int index;
         protected boolean hasNextCalled;
@@ -187,10 +258,10 @@
 
         public synchronized boolean hasNext() {
             synchronized (lock) {
-                hasPreviousCalled = false;
-                hasNextCalled = true;
                 next = index < storage.size() ? storage.get(index) : null;
-                return next != null;
+                hasPreviousCalled = false;
+                hasNextCalled = next != null;
+                return hasNextCalled;
             }
         }
 
@@ -247,17 +318,18 @@
             return index - 1;
         }
 
-        public synchronized void remove() {
-            if (last == null) {
-                throw new IllegalStateException();
-            }
-            synchronized (lock) {
-                if (storage.get(index - 1) == last) {
-                    storage.remove(index - 1);
-                }
-            }
-            last = null;
+        public void set(E o) {
+            throw new UnsupportedOperationException();
+        }
 
+        public void add(E o) {
+            throw new UnsupportedOperationException();
         }
+
+        public synchronized void remove() {
+            throw new UnsupportedOperationException();
+        }
+
     }
+
 }

Modified: geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/utils/DynamicCollectionTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/utils/DynamicCollectionTest.java?rev=778844&r1=778843&r2=778844&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/utils/DynamicCollectionTest.java (original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/utils/DynamicCollectionTest.java Tue May 26 18:52:31 2009
@@ -32,7 +32,7 @@
     protected DynamicCollection<Object> collection;
 
     protected void setUp() {
-        collection = new DynamicCollection<Object>();
+        collection = new DynamicCollection<Object>(true, null);
     }
 
     public void testAddRemove() throws Exception {
@@ -110,15 +110,4 @@
         assertFalse(iterator.hasNext());
     }
 
-    public void testIteratorRemove() throws Exception {
-        collection.add(O0);
-
-        Iterator iterator = collection.iterator();
-        assertTrue(iterator.hasNext());
-        assertEquals(O0, iterator.next());
-        iterator.remove();
-        assertFalse(iterator.hasNext());
-        assertTrue(collection.isEmpty());
-        assertFalse(iterator.hasNext());
-    }
 }