[jboss-svn-commits] JBoss Common SVN: r2516 - common-core/trunk/src/main/java/org/jboss/util/collection.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Aug 28 06:43:35 EDT 2007


Author: adrian at jboss.org
Date: 2007-08-28 06:43:34 -0400 (Tue, 28 Aug 2007)
New Revision: 2516

Modified:
   common-core/trunk/src/main/java/org/jboss/util/collection/CollectionsFactory.java
   common-core/trunk/src/main/java/org/jboss/util/collection/LazyList.java
   common-core/trunk/src/main/java/org/jboss/util/collection/LazyMap.java
   common-core/trunk/src/main/java/org/jboss/util/collection/LazySet.java
Log:
[JBCOMMON-30] - Generification of lazy collections

Modified: common-core/trunk/src/main/java/org/jboss/util/collection/CollectionsFactory.java
===================================================================
--- common-core/trunk/src/main/java/org/jboss/util/collection/CollectionsFactory.java	2007-08-28 09:16:20 UTC (rev 2515)
+++ common-core/trunk/src/main/java/org/jboss/util/collection/CollectionsFactory.java	2007-08-28 10:43:34 UTC (rev 2516)
@@ -42,49 +42,69 @@
 {
    /**
     * Defines the map implementation
+    * 
+    * @param <K> the key type
+    * @param <V> the value type
+    * @return the map
     */
-   public static final Map createLazyMap()
+   public static final <K, V> Map<K, V> createLazyMap()
    {
-      return new LazyMap();
+      return new LazyMap<K, V>();
    }
 
    /**
     * Defines the list implementation
+    * 
+    * @param <T> the type
+    * @return the list
     */
-   public static final List createLazyList()
+   public static final <T> List<T> createLazyList()
    {
-      return new LazyList();
+      return new LazyList<T>();
    }
 
    /**
     * Defines the set implementation
+    * 
+    * @param <T> the type
+    * @return the set
     */
-   public static final Set createLazySet()
+   public static final <T> Set<T> createLazySet()
    {
-      return new LazySet();
+      return new LazySet<T>();
    }
 
    /**
     * Defines the concurrent map implementation
+    * 
+    * @param <K> the key type
+    * @param <V> the value type
+    * @return the map
     */
-   public static final Map createConcurrentReaderMap()
+   public static final <K, V> Map<K, V> createConcurrentReaderMap()
    {
-      return new ConcurrentHashMap();
+      return new ConcurrentHashMap<K, V>();
    }
 
    /**
     * Defines the concurrent list implementation
+    * 
+    * @param <T> the type
+    * @return the list
     */
-   public static final List createCopyOnWriteList()
+   public static final <T> List<T> createCopyOnWriteList()
    {
-      return new CopyOnWriteArrayList();
+      return new CopyOnWriteArrayList<T>();
    }
 
    /**
     * Defines the concurrent set implementation
+    * 
+    * @param <T> the type
+    * @return the set
     */
-   public static final Set createCopyOnWriteSet()
+   public static final <T> Set<T> createCopyOnWriteSet()
    {
-      return new CopyOnWriteArraySet();
+      return new CopyOnWriteArraySet<T>();
    }
 }

Modified: common-core/trunk/src/main/java/org/jboss/util/collection/LazyList.java
===================================================================
--- common-core/trunk/src/main/java/org/jboss/util/collection/LazyList.java	2007-08-28 09:16:20 UTC (rev 2515)
+++ common-core/trunk/src/main/java/org/jboss/util/collection/LazyList.java	2007-08-28 10:43:34 UTC (rev 2516)
@@ -31,53 +31,62 @@
 /**
  * LazyList.
  * 
+ * @param <T> the collection type
  * @author <a href="adrian at jboss.com">Adrian Brock</a>
  * @version $Revision$
  */
-public class LazyList implements List
+public class LazyList<T> implements List<T>
 {
    /** The delegate list */
-   private List delegate = Collections.EMPTY_LIST; 
+   private List<T> delegate = Collections.emptyList(); 
 
-   public void add(int index, Object element)
+   /**
+    * Create the list implementation
+    * 
+    * @return the list
+    */
+   private List<T> createImplementation()
    {
       if (delegate instanceof ArrayList == false)
-         delegate = new ArrayList(delegate);
+         return new ArrayList<T>(delegate);
+      return delegate;
+   }
+   
+   public void add(int index, T element)
+   {
+      delegate = createImplementation();
       delegate.add(index, element);
    }
 
-   public boolean add(Object o)
+   public boolean add(T o)
    {
-      if (delegate == Collections.EMPTY_LIST)
+      if (delegate.isEmpty())
       {
          delegate = Collections.singletonList(o);
          return true;
       }
       else
       {
-         if (delegate instanceof ArrayList == false)
-            delegate = new ArrayList(delegate);
+         delegate = createImplementation();
          return delegate.add(o);
       }
    }
 
-   public boolean addAll(Collection c)
+   public boolean addAll(Collection<? extends T> c)
    {
-      if (delegate instanceof ArrayList == false)
-         delegate = new ArrayList(delegate);
+      delegate = createImplementation();
       return delegate.addAll(c);
    }
 
-   public boolean addAll(int index, Collection c)
+   public boolean addAll(int index, Collection<? extends T> c)
    {
-      if (delegate instanceof ArrayList == false)
-         delegate = new ArrayList(delegate);
+      delegate = createImplementation();
       return delegate.addAll(index, c);
    }
 
    public void clear()
    {
-      delegate = Collections.EMPTY_LIST;
+      delegate = Collections.emptyList();
    }
 
    public boolean contains(Object o)
@@ -85,12 +94,12 @@
       return delegate.contains(o);
    }
 
-   public boolean containsAll(Collection c)
+   public boolean containsAll(Collection<?> c)
    {
       return delegate.containsAll(c);
    }
 
-   public Object get(int index)
+   public T get(int index)
    {
       return delegate.get(index);
    }
@@ -105,7 +114,7 @@
       return delegate.isEmpty();
    }
 
-   public Iterator iterator()
+   public Iterator<T> iterator()
    {
       return delegate.iterator();
    }
@@ -115,48 +124,43 @@
       return delegate.lastIndexOf(o);
    }
 
-   public ListIterator listIterator()
+   public ListIterator<T> listIterator()
    {
       return delegate.listIterator();
    }
 
-   public ListIterator listIterator(int index)
+   public ListIterator<T> listIterator(int index)
    {
       return delegate.listIterator(index);
    }
 
-   public Object remove(int index)
+   public T remove(int index)
    {
-      if (delegate instanceof ArrayList == false)
-         delegate = new ArrayList(delegate);
+      delegate = createImplementation();
       return delegate.remove(index);
    }
 
    public boolean remove(Object o)
    {
-      if (delegate instanceof ArrayList == false)
-         delegate = new ArrayList(delegate);
+      delegate = createImplementation();
       return delegate.remove(o);
    }
 
-   public boolean removeAll(Collection c)
+   public boolean removeAll(Collection<?> c)
    {
-      if (delegate instanceof ArrayList == false)
-         delegate = new ArrayList(delegate);
-      return delegate.remove(c);
+      delegate = createImplementation();
+      return delegate.removeAll(c);
    }
 
-   public boolean retainAll(Collection c)
+   public boolean retainAll(Collection<?> c)
    {
-      if (delegate instanceof ArrayList == false)
-         delegate = new ArrayList(delegate);
+      delegate = createImplementation();
       return delegate.retainAll(c);
    }
 
-   public Object set(int index, Object element)
+   public T set(int index, T element)
    {
-      if (delegate instanceof ArrayList == false)
-         delegate = new ArrayList(delegate);
+      delegate = createImplementation();
       return delegate.set(index, element);
    }
 
@@ -165,7 +169,7 @@
       return delegate.size();
    }
 
-   public List subList(int fromIndex, int toIndex)
+   public List<T> subList(int fromIndex, int toIndex)
    {
       return delegate.subList(fromIndex, toIndex);
    }
@@ -175,7 +179,7 @@
       return delegate.toArray();
    }
 
-   public Object[] toArray(Object[] a)
+   public <U> U[] toArray(U[] a)
    {
       return delegate.toArray(a);
    }

Modified: common-core/trunk/src/main/java/org/jboss/util/collection/LazyMap.java
===================================================================
--- common-core/trunk/src/main/java/org/jboss/util/collection/LazyMap.java	2007-08-28 09:16:20 UTC (rev 2515)
+++ common-core/trunk/src/main/java/org/jboss/util/collection/LazyMap.java	2007-08-28 10:43:34 UTC (rev 2516)
@@ -30,17 +30,31 @@
 /**
  * LazyMap.
  * 
+ * @param <K> the key type
+ * @param <V> the value type
  * @author <a href="adrian at jboss.com">Adrian Brock</a>
  * @version $Revision$
  */
-public class LazyMap implements Map
+public class LazyMap<K, V> implements Map<K, V>
 {
    /** The delegate map */
-   private Map delegate = Collections.EMPTY_MAP;
-   
+   private Map<K, V> delegate = Collections.emptyMap();
+
+   /**
+    * Create the map implementation
+    * 
+    * @return the map
+    */
+   private Map<K, V> createImplementation()
+   {
+      if (delegate instanceof HashMap == false)
+         return new HashMap<K, V>(delegate);
+      return delegate;
+   }
+
    public void clear()
    {
-      delegate = Collections.EMPTY_MAP;
+      delegate = Collections.emptyMap();
    }
 
    public boolean containsKey(Object key)
@@ -53,12 +67,12 @@
       return delegate.containsValue(value);
    }
 
-   public Set entrySet()
+   public Set<Entry<K, V>> entrySet()
    {
       return delegate.entrySet();
    }
 
-   public Object get(Object key)
+   public V get(Object key)
    {
       return delegate.get(key);
    }
@@ -68,37 +82,34 @@
       return delegate.isEmpty();
    }
 
-   public Set keySet()
+   public Set<K> keySet()
    {
       return delegate.keySet();
    }
 
-   public Object put(Object key, Object value)
+   public V put(K key, V value)
    {
-      if (delegate == Collections.EMPTY_MAP)
+      if (delegate.isEmpty())
       {
          delegate = Collections.singletonMap(key, value);
          return null;
       }
       else
       {
-         if (delegate instanceof HashMap == false)
-            delegate = new HashMap(delegate);
+         delegate = createImplementation();
          return delegate.put(key, value);
       }
    }
 
-   public void putAll(Map t)
+   public void putAll(Map<? extends K, ? extends V> t)
    {
-      if (delegate instanceof HashMap == false)
-         delegate = new HashMap(delegate);
+      delegate = createImplementation();
       delegate.putAll(t);
    }
 
-   public Object remove(Object key)
+   public V remove(Object key)
    {
-      if (delegate instanceof HashMap == false)
-         delegate = new HashMap(delegate);
+      delegate = createImplementation();
       return delegate.remove(key);
    }
 
@@ -107,7 +118,7 @@
       return delegate.size();
    }
 
-   public Collection values()
+   public Collection<V> values()
    {
       return delegate.values();
    }

Modified: common-core/trunk/src/main/java/org/jboss/util/collection/LazySet.java
===================================================================
--- common-core/trunk/src/main/java/org/jboss/util/collection/LazySet.java	2007-08-28 09:16:20 UTC (rev 2515)
+++ common-core/trunk/src/main/java/org/jboss/util/collection/LazySet.java	2007-08-28 10:43:34 UTC (rev 2516)
@@ -30,39 +30,50 @@
 /**
  * LazySet.
  * 
+ * @param <T> the element type
  * @author <a href="adrian at jboss.com">Adrian Brock</a>
  * @version $Revision$
  */
-public class LazySet implements Set
+public class LazySet<T> implements Set<T>
 {
    /** The delegate set */
-   private Set delegate = Collections.EMPTY_SET;
-   
-   public boolean add(Object o)
+   private Set<T> delegate = Collections.emptySet();
+
+   /**
+    * Create the set implementation
+    * 
+    * @return the set
+    */
+   private Set<T> createImplementation()
    {
-      if (delegate == Collections.EMPTY_SET)
+      if (delegate instanceof HashSet == false)
+         return new HashSet<T>(delegate);
+      return delegate;
+   }
+
+   public boolean add(T o)
+   {
+      if (delegate.isEmpty())
       {
          delegate = Collections.singleton(o);
          return true;
       }
       else
       {
-         if (delegate instanceof HashSet == false)
-            delegate = new HashSet(delegate);
+         delegate = createImplementation();
          return delegate.add(o);
       }
    }
 
-   public boolean addAll(Collection c)
+   public boolean addAll(Collection<? extends T> c)
    {
-      if (delegate instanceof HashSet == false)
-         delegate = new HashSet(delegate);
+      delegate = createImplementation();
       return delegate.addAll(c);
    }
 
    public void clear()
    {
-      delegate = Collections.EMPTY_SET;
+      delegate = Collections.emptySet();
    }
 
    public boolean contains(Object o)
@@ -70,7 +81,7 @@
       return delegate.contains(o);
    }
 
-   public boolean containsAll(Collection c)
+   public boolean containsAll(Collection<?> c)
    {
       return delegate.containsAll(c);
    }
@@ -80,29 +91,26 @@
       return delegate.isEmpty();
    }
 
-   public Iterator iterator()
+   public Iterator<T> iterator()
    {
       return delegate.iterator();
    }
 
    public boolean remove(Object o)
    {
-      if (delegate instanceof HashSet == false)
-         delegate = new HashSet(delegate);
+      delegate = createImplementation();
       return delegate.remove(o);
    }
 
-   public boolean removeAll(Collection c)
+   public boolean removeAll(Collection<?> c)
    {
-      if (delegate instanceof HashSet == false)
-         delegate = new HashSet(delegate);
+      delegate = createImplementation();
       return delegate.removeAll(c);
    }
 
-   public boolean retainAll(Collection c)
+   public boolean retainAll(Collection<?> c)
    {
-      if (delegate instanceof HashSet == false)
-         delegate = new HashSet(delegate);
+      delegate = createImplementation();
       return delegate.retainAll(c);
    }
 
@@ -116,7 +124,7 @@
       return delegate.toArray();
    }
 
-   public Object[] toArray(Object[] a)
+   public <U> U[] toArray(U[] a)
    {
       return delegate.toArray(a);
    }




More information about the jboss-svn-commits mailing list