harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r410690 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util: AbstractSet.java HashSet.java LinkedHashMap.java LinkedHashSet.java LinkedList.java
Date Wed, 31 May 2006 22:24:21 GMT
Author: tellison
Date: Wed May 31 15:24:21 2006
New Revision: 410690

URL: http://svn.apache.org/viewvc?rev=410690&view=rev
Log:
Apply patch HARMONY-525 ([classlib][luni] Misc Collections Generification)

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/AbstractSet.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/HashSet.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/LinkedHashMap.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/LinkedHashSet.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/LinkedList.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/AbstractSet.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/AbstractSet.java?rev=410690&r1=410689&r2=410690&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/AbstractSet.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/AbstractSet.java
Wed May 31 15:24:21 2006
@@ -81,7 +81,7 @@
 	 * @exception UnsupportedOperationException
 	 *                when removing from this Collection is not supported
 	 */
-	public boolean removeAll(Collection collection) {
+	public boolean removeAll(Collection<?> collection) {
 		boolean result = false;
 		if (size() <= collection.size()) {
 			Iterator it = iterator();

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/HashSet.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/HashSet.java?rev=410690&r1=410689&r2=410690&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/HashSet.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/HashSet.java
Wed May 31 15:24:21 2006
@@ -30,13 +30,13 @@
 
 	private static final long serialVersionUID = -5024744406713321676L;
 
-	transient HashMap backingMap;
+	transient HashMap<E, HashSet<E>> backingMap;
 
 	/**
-	 * Contructs a new empty instance of HashSet.
+	 * Constructs a new empty instance of HashSet.
 	 */
 	public HashSet() {
-		this(new HashMap());
+		this(new HashMap<E, HashSet<E>>());
 	}
 
 	/**
@@ -46,7 +46,7 @@
 	 *            the initial capacity of this HashSet
 	 */
 	public HashSet(int capacity) {
-		this(new HashMap(capacity));
+		this(new HashMap<E, HashSet<E>>(capacity));
 	}
 
 	/**
@@ -59,7 +59,7 @@
 	 *            the initial load factor
 	 */
 	public HashSet(int capacity, float loadFactor) {
-		this(new HashMap(capacity, loadFactor));
+		this(new HashMap<E, HashSet<E>>(capacity, loadFactor));
 	}
 
 	/**
@@ -69,14 +69,14 @@
 	 * @param collection
 	 *            the collection of elements to add
 	 */
-	public HashSet(Collection collection) {
-		this(new HashMap(collection.size() < 6 ? 11 : collection.size() * 2));
-		Iterator it = collection.iterator();
-		while (it.hasNext())
-			add(it.next());
+	public HashSet(Collection<? extends E> collection) {
+		this(new HashMap<E, HashSet<E>>(collection.size() < 6 ? 11 : collection.size()
* 2));
+        for (E e : collection) {
+            add(e);
+        }
 	}
 
-	HashSet(HashMap backingMap) {
+	HashSet(HashMap<E, HashSet<E>> backingMap) {
 		this.backingMap = backingMap;
 	}
 
@@ -88,7 +88,7 @@
 	 * @return true when this HashSet did not already contain the object, false
 	 *         otherwise
 	 */
-	public boolean add(Object object) {
+	public boolean add(E object) {
 		return backingMap.put(object, this) == null;
 	}
 
@@ -111,8 +111,8 @@
 	 */
 	public Object clone() {
 		try {
-			HashSet clone = (HashSet) super.clone();
-			clone.backingMap = (HashMap) backingMap.clone();
+			HashSet<E> clone = (HashSet<E>) super.clone();
+			clone.backingMap = (HashMap<E, HashSet<E>>) backingMap.clone();
 			return clone;
 		} catch (CloneNotSupportedException e) {
 			return null;
@@ -149,7 +149,7 @@
 	 * 
 	 * @see Iterator
 	 */
-	public Iterator iterator() {
+	public Iterator<E> iterator() {
 		return backingMap.keySet().iterator();
 	}
 
@@ -179,7 +179,7 @@
 		stream.writeFloat(backingMap.loadFactor);
 		stream.writeInt(backingMap.elementCount);
 		for (int i = backingMap.elementData.length; --i >= 0;) {
-			HashMap.Entry entry = backingMap.elementData[i];
+			HashMap.Entry<E, HashSet<E>> entry = backingMap.elementData[i];
 			while (entry != null) {
 				stream.writeObject(entry.key);
 				entry = entry.next;
@@ -195,12 +195,12 @@
 		backingMap = createBackingMap(length, loadFactor);
 		int elementCount = stream.readInt();
 		for (int i = elementCount; --i >= 0;) {
-			Object key = stream.readObject();
+			E key = (E)stream.readObject();
 			backingMap.put(key, this);
 		}
 	}
 
-	HashMap createBackingMap(int capacity, float loadFactor) {
-		return new HashMap(capacity, loadFactor);
+	HashMap<E, HashSet<E>> createBackingMap(int capacity, float loadFactor) {
+		return new HashMap<E, HashSet<E>>(capacity, loadFactor);
 	}
 }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/LinkedHashMap.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/LinkedHashMap.java?rev=410690&r1=410689&r2=410690&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/LinkedHashMap.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/LinkedHashMap.java
Wed May 31 15:24:21 2006
@@ -20,24 +20,24 @@
  * LinkedHashMap is a variant on HashMap. Its entries are kept in a doubly-linked list.
  * The iteration order is, by default, the order in which keys were inserted.
  * <p> 
- * If the three arg constructor is used, and <code>order</code> is specified
as <code>true</code>, 
+ * If the three argument constructor is used, and <code>order</code> is specified
as <code>true</code>, 
  * the iteration would be in the order that entries were accessed. The access order gets

  * affected by put(), get(), putAll() operations, but not by operations on the collection
views.
  * <p>
  * Null elements are allowed, and all the optional Map operations are supported.
  * <p>
- *
+ * @since 1.4
  */
-public class LinkedHashMap extends HashMap {
+public class LinkedHashMap<K, V> extends HashMap<K, V> {
 
     private static final long serialVersionUID = 3801124242820219131L;
 
     private final boolean accessOrder;
 
-    transient private LinkedHashMapEntry head, tail;
+    transient private LinkedHashMapEntry<K, V> head, tail;
 
     /**
-     * Contructs a new empty instance of LinkedHashMap.
+     * Constructs a new empty instance of LinkedHashMap.
      */
     public LinkedHashMap() {
         super();
@@ -96,15 +96,15 @@
      * @param m
      *            Input map
      */
-    public LinkedHashMap(Map m) {
+    public LinkedHashMap(Map<? extends K, ? extends V> m) {
         accessOrder = false;
         head = null;
         tail = null;
         putAll(m);
     }
 
-    static final class LinkedHashIterator extends HashMapIterator {
-        LinkedHashIterator(MapEntry.Type value, LinkedHashMap hm) {
+    static final class LinkedHashIterator<E, KT, VT> extends HashMapIterator<E,
KT, VT> {
+        LinkedHashIterator(MapEntry.Type<E, KT, VT> value, LinkedHashMap<KT, VT>
hm) {
             super(value, hm);
             entry = hm.head;
         }
@@ -113,13 +113,13 @@
             return (entry != null);
         }
 
-        public Object next() {
+        public E next() {
             checkConcurrentMod();
             if (!hasNext())
                 throw new NoSuchElementException();
-            Object result = type.get(entry);
+            E result = type.get(entry);
             lastEntry = entry;
-            entry = ((LinkedHashMapEntry) entry).chainForward;
+            entry = ((LinkedHashMapEntry<KT, VT>)entry).chainForward;
             canRemove = true;
             return result;
         }
@@ -133,22 +133,22 @@
             associatedMap.modCount++;
 
             int index = associatedMap.getModuloHash(lastEntry.key);
-            LinkedHashMapEntry m = (LinkedHashMapEntry) associatedMap.elementData[index];
+            LinkedHashMapEntry<KT, VT> m = (LinkedHashMapEntry<KT, VT>) associatedMap.elementData[index];
             if (m == lastEntry) {
                 associatedMap.elementData[index] = lastEntry.next;
             } else {
                 while (m.next != null) {
                     if (m.next == lastEntry)
                         break;
-                    m = (LinkedHashMapEntry) m.next;
+                    m = (LinkedHashMapEntry<KT, VT>) m.next;
                 }
                 // assert m.next == entry
                 m.next = lastEntry.next;
             }
-            LinkedHashMapEntry lhme = (LinkedHashMapEntry) lastEntry;
-            LinkedHashMapEntry p = lhme.chainBackward;
-            LinkedHashMapEntry n = lhme.chainForward;
-            LinkedHashMap lhm = (LinkedHashMap) associatedMap;
+            LinkedHashMapEntry<KT, VT> lhme = (LinkedHashMapEntry<KT, VT>) lastEntry;
+            LinkedHashMapEntry<KT, VT> p = lhme.chainBackward;
+            LinkedHashMapEntry<KT, VT> n = lhme.chainForward;
+            LinkedHashMap<KT, VT> lhm = (LinkedHashMap<KT, VT>) associatedMap;
             if (p != null) {
                 p.chainForward = n;
                 if (n != null)
@@ -167,36 +167,36 @@
         }
     }
 
-    static final class LinkedHashMapEntrySet extends HashMapEntrySet {
-        public LinkedHashMapEntrySet(LinkedHashMap lhm) {
+    static final class LinkedHashMapEntrySet<KT, VT> extends HashMapEntrySet<KT,
VT> {
+        public LinkedHashMapEntrySet(LinkedHashMap<KT, VT> lhm) {
             super(lhm);
         }
 
-        public Iterator iterator() {
-            return new LinkedHashIterator(new MapEntry.Type() {
-                public Object get(MapEntry entry) {
+        public Iterator<Map.Entry<KT,VT>> iterator() {
+            return new LinkedHashIterator<Map.Entry<KT,VT>,KT,VT>(new MapEntry.Type<Map.Entry<KT,VT>,
KT, VT>() {
+                public Map.Entry<KT,VT> get(MapEntry<KT,VT> entry) {
                     return entry;
                 }
-            }, (LinkedHashMap) hashMap());
+            }, (LinkedHashMap<KT, VT>) hashMap());
         }
     }
 
-    static final class LinkedHashMapEntry extends Entry {
-        LinkedHashMapEntry chainForward, chainBackward;
+    static final class LinkedHashMapEntry<K, V> extends Entry<K, V> {
+        LinkedHashMapEntry<K, V> chainForward, chainBackward;
 
-        LinkedHashMapEntry(Object theKey, Object theValue) {
+        LinkedHashMapEntry(K theKey, V theValue) {
             super(theKey, theValue);
             chainForward = null;
             chainBackward = null;
         }
 
         public Object clone() {
-            LinkedHashMapEntry entry = (LinkedHashMapEntry) super.clone();
+            LinkedHashMapEntry<K, V> entry = (LinkedHashMapEntry<K, V>) super.clone();
             entry.chainBackward = chainBackward;
             entry.chainForward = chainForward;
-            LinkedHashMapEntry lnext = (LinkedHashMapEntry) entry.next;
+            LinkedHashMapEntry<K, V> lnext = (LinkedHashMapEntry<K, V>) entry.next;
             if (lnext != null)
-                entry.next = (LinkedHashMapEntry) lnext.clone();
+                entry.next = (LinkedHashMapEntry<K, V>) lnext.clone();
             return entry;
         }
     }
@@ -207,7 +207,7 @@
      * @param s
      * @return Reference to the element array
      */
-    Entry[] newElementArray(int s) {
+    Entry<K, V>[] newElementArray(int s) {
         return new LinkedHashMapEntry[s];
     }
 
@@ -218,13 +218,13 @@
      *            Key value
      * @return mapped value or null if the key is not in the map
      */
-    public Object get(Object key) {
-        LinkedHashMapEntry m = (LinkedHashMapEntry) getEntry(key);
+    public V get(Object key) {
+        LinkedHashMapEntry<K, V> m = (LinkedHashMapEntry<K, V>) getEntry(key);
         if (m == null)
             return null;
         if (accessOrder && tail != m) {
-            LinkedHashMapEntry p = m.chainBackward;
-            LinkedHashMapEntry n = m.chainForward;
+            LinkedHashMapEntry<K, V> p = m.chainBackward;
+            LinkedHashMapEntry<K, V> n = m.chainForward;
             n.chainBackward = p;
             if (p != null)
                 p.chainForward = n;
@@ -241,8 +241,8 @@
     /*
      * @param key @param index @return Entry
      */
-    Entry createEntry(Object key, int index, Object value) {
-        LinkedHashMapEntry m = new LinkedHashMapEntry(key, value);
+    Entry<K, V> createEntry(K key, int index, V value) {
+        LinkedHashMapEntry<K, V> m = new LinkedHashMapEntry<K, V>(key, value);
         m.next = elementData[index];
         elementData[index] = m;
         linkEntry(m);
@@ -259,9 +259,9 @@
      * @return The old value if the key was already in the map or null
      *         otherwise.
      */
-    public Object put(Object key, Object value) {
+    public V put(K key, V value) {
         int index = getModuloHash(key);
-        LinkedHashMapEntry m = (LinkedHashMapEntry) findEntry(key, index);
+        LinkedHashMapEntry<K, V> m = (LinkedHashMapEntry<K, V>) findEntry(key,
index);
 
         if (m == null) {
             modCount++;
@@ -274,12 +274,12 @@
                 index = key == null ? 0 : (key.hashCode() & 0x7FFFFFFF)
                         % elementData.length;
             }
-            m = (LinkedHashMapEntry) createEntry(key, index, null);
+            m = (LinkedHashMapEntry<K, V>) createEntry(key, index, null);
         } else {
             linkEntry(m);
         }
 
-        Object result = m.value;
+        V result = m.value;
         m.value = value;
 
         if (removeEldestEntry(head))
@@ -291,7 +291,7 @@
     /*
      * @param m
      */
-    void linkEntry(LinkedHashMapEntry m) {
+    void linkEntry(LinkedHashMapEntry<K, V> m) {
         if (tail == m) {
             return;
         }
@@ -304,8 +304,8 @@
 
         // we need to link the new entry into either the head or tail
         // of the chain depending on if the LinkedHashMap is accessOrder or not
-        LinkedHashMapEntry p = m.chainBackward;
-        LinkedHashMapEntry n = m.chainForward;
+        LinkedHashMapEntry<K, V> p = m.chainBackward;
+        LinkedHashMapEntry<K, V> n = m.chainForward;
         if (p == null) {
             if (n != null) {
                 // The entry must be the head but not the tail
@@ -350,10 +350,8 @@
      * @param m
      *            Input map
      */
-    public void putAll(Map m) {
-        Iterator i = m.entrySet().iterator();
-        while (i.hasNext()) {
-            Map.Entry e = (MapEntry) i.next();
+    public void putAll(Map<? extends K, ? extends V> m) {
+        for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) {
             put(e.getKey(), e.getValue());
         }
     }
@@ -361,24 +359,24 @@
     /**
      * Answers a Set of the mappings contained in this HashMap. Each element in
      * the set is a Map.Entry. The set is backed by this HashMap so changes to
-     * one are relected by the other. The set does not support adding.
+     * one are reflected by the other. The set does not support adding.
      * 
      * @return a Set of the mappings
      */
-    public Set entrySet() {
-        return new LinkedHashMapEntrySet(this);
+    public Set<Map.Entry<K, V>> entrySet() {
+        return new LinkedHashMapEntrySet<K, V>(this);
     }
 
     /**
      * Answers a Set of the keys contained in this HashMap. The set is backed by
-     * this HashMap so changes to one are relected by the other. The set does
+     * this HashMap so changes to one are reflected by the other. The set does
      * not support adding.
      * 
      * @return a Set of the keys
      */
-    public Set keySet() {
+    public Set<K> keySet() {
         if (keySet == null) {
-            keySet = new AbstractSet() {
+            keySet = new AbstractSet<K>() {
                 public boolean contains(Object object) {
                     return containsKey(object);
                 }
@@ -399,9 +397,9 @@
                     return false;
                 }
 
-                public Iterator iterator() {
-                    return new LinkedHashIterator(new MapEntry.Type() {
-                        public Object get(MapEntry entry) {
+                public Iterator<K> iterator() {
+                    return new LinkedHashIterator<K,K,V>(new MapEntry.Type<K,K,V>()
{
+                        public K get(MapEntry<K,V> entry) {
                             return entry.key;
                         }
                     }, LinkedHashMap.this);
@@ -413,14 +411,14 @@
 
     /**
      * Answers a Collection of the values contained in this HashMap. The
-     * collection is backed by this HashMap so changes to one are relected by
+     * collection is backed by this HashMap so changes to one are reflected by
      * the other. The collection does not support adding.
      * 
      * @return a Collection of the values
      */
-    public Collection values() {
+    public Collection<V> values() {
         if (valuesCollection == null) {
-            valuesCollection = new AbstractCollection() {
+            valuesCollection = new AbstractCollection<V>() {
                 public boolean contains(Object object) {
                     return containsValue(object);
                 }
@@ -433,9 +431,9 @@
                     LinkedHashMap.this.clear();
                 }
 
-                public Iterator iterator() {
-                    return new LinkedHashIterator(new MapEntry.Type() {
-                        public Object get(MapEntry entry) {
+                public Iterator<V> iterator() {
+                    return new LinkedHashIterator<V,K,V>(new MapEntry.Type<V,K,V>()
{
+                        public V get(MapEntry<K,V> entry) {
                             return entry.value;
                         }
                     }, LinkedHashMap.this);
@@ -453,12 +451,12 @@
      * @return the value associated with the key or null if the key was no in
      *         the map
      */
-    public Object remove(Object key) {
-        LinkedHashMapEntry m = (LinkedHashMapEntry) removeEntry(key);
+    public V remove(Object key) {
+        LinkedHashMapEntry<K, V> m = (LinkedHashMapEntry<K, V>) removeEntry(key);
         if (m == null)
             return null;
-        LinkedHashMapEntry p = m.chainBackward;
-        LinkedHashMapEntry n = m.chainForward;
+        LinkedHashMapEntry<K, V> p = m.chainBackward;
+        LinkedHashMapEntry<K, V> n = m.chainForward;
         if (p != null)
             p.chainForward = n;
         else
@@ -479,7 +477,7 @@
      * @param eldest
      * @return true if the eldest member should be removed
      */
-    protected boolean removeEldestEntry(Map.Entry eldest) {
+    protected boolean removeEldestEntry(Map.Entry<K,V> eldest) {
         return false;
     }
 
@@ -502,11 +500,9 @@
      * @see java.lang.Cloneable
      */
     public Object clone() {
-        LinkedHashMap map = (LinkedHashMap) super.clone();
+        LinkedHashMap<K, V> map = (LinkedHashMap<K, V>) super.clone();
         map.clear();
-        Iterator entries = entrySet().iterator();
-        while (entries.hasNext()) {
-            Map.Entry entry = (Map.Entry) entries.next();
+        for (Map.Entry<K, V> entry : entrySet()) {
             map.put(entry.getKey(), entry.getValue());
         }
         return map;

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/LinkedHashSet.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/LinkedHashSet.java?rev=410690&r1=410689&r2=410690&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/LinkedHashSet.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/LinkedHashSet.java
Wed May 31 15:24:21 2006
@@ -26,6 +26,7 @@
  * <p>
  * Like HashSet, LinkedHashSet is not thread safe, so access by multiple threads must be
synchronized
  * by an external mechanism such as Collections.synchronizedSet.
+ * @since 1.4
  */
 public class LinkedHashSet<E> extends HashSet<E> implements Set<E>, Cloneable,
 		Serializable {
@@ -33,10 +34,10 @@
 	private static final long serialVersionUID = -2851667679971038690L;
 
 	/**
-	 * Contructs a new empty instance of LinkedHashSet.
+	 * Constructs a new empty instance of LinkedHashSet.
 	 */
 	public LinkedHashSet() {
-		super(new LinkedHashMap());
+		super(new LinkedHashMap<E, HashSet<E>>());
 	}
 
 	/**
@@ -46,7 +47,7 @@
 	 *            the initial capacity of this HashSet
 	 */
 	public LinkedHashSet(int capacity) {
-		super(new LinkedHashMap(capacity));
+		super(new LinkedHashMap<E, HashSet<E>>(capacity));
 	}
 
 	/**
@@ -59,7 +60,7 @@
 	 *            the initial load factor
 	 */
 	public LinkedHashSet(int capacity, float loadFactor) {
-		super(new LinkedHashMap(capacity, loadFactor));
+		super(new LinkedHashMap<E, HashSet<E>>(capacity, loadFactor));
 	}
 
 	/**
@@ -70,15 +71,15 @@
 	 *            the collection of elements to add
 	 */
 	public LinkedHashSet(Collection<? extends E> collection) {
-		super(new LinkedHashMap(collection.size() < 6 ? 11
+		super(new LinkedHashMap<E, HashSet<E>>(collection.size() < 6 ? 11
 				: collection.size() * 2));
-		Iterator it = collection.iterator();
-		while (it.hasNext())
-			add(it.next());
+        for (E e : collection) {
+            add(e);
+        }
 	}
 
 	/* overrides method in HashMap */
-	HashMap createBackingMap(int capacity, float loadFactor) {
-		return new LinkedHashMap(capacity, loadFactor);
+	HashMap<E, HashSet<E>> createBackingMap(int capacity, float loadFactor) {
+		return new LinkedHashMap<E, HashSet<E>>(capacity, loadFactor);
 	}
 }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/LinkedList.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/LinkedList.java?rev=410690&r1=410689&r2=410690&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/LinkedList.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/LinkedList.java
Wed May 31 15:24:21 2006
@@ -26,36 +26,37 @@
  * LinkedList is an implementation of List, backed by a linked list. All
  * optional operations are supported, adding, removing and replacing. The
  * elements can be any objects.
+ * @since 1.2
  */
-public class LinkedList extends AbstractSequentialList implements List,
+public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>,
 		Cloneable, Serializable {
 	
 	private static final long serialVersionUID = 876323262645176354L;
 
 	transient int size = 0;
 
-	transient Link voidLink;
+	transient Link<E> voidLink;
 
-	private static final class Link {
-		Object data;
+	private static final class Link<ET> {
+		ET data;
 
-		Link previous, next;
+		Link<ET> previous, next;
 
-		Link(Object o, Link p, Link n) {
+		Link(ET o, Link<ET> p, Link<ET> n) {
 			data = o;
 			previous = p;
 			next = n;
 		}
 	}
 
-	private static final class LinkIterator implements ListIterator {
+	private static final class LinkIterator<ET> implements ListIterator<ET> {
 		int pos, expectedModCount;
 
-		final LinkedList list;
+		final LinkedList<ET> list;
 
-		Link link, lastLink;
+		Link<ET> link, lastLink;
 
-		LinkIterator(LinkedList object, int location) {
+		LinkIterator(LinkedList<ET> object, int location) {
 			list = object;
 			expectedModCount = list.modCount;
 			if (0 <= location && location <= list.size) {
@@ -74,10 +75,10 @@
 				throw new IndexOutOfBoundsException();
 		}
 
-		public void add(Object object) {
+		public void add(ET object) {
 			if (expectedModCount == list.modCount) {
-				Link next = link.next;
-				Link newLink = new Link(object, link, next);
+				Link<ET> next = link.next;
+				Link<ET> newLink = new Link<ET>(object, link, next);
 				link.next = newLink;
 				next.previous = newLink;
 				link = newLink;
@@ -98,9 +99,9 @@
 			return link != list.voidLink;
 		}
 
-		public Object next() {
+		public ET next() {
 			if (expectedModCount == list.modCount) {
-				LinkedList.Link next = link.next;
+				LinkedList.Link<ET> next = link.next;
 				if (next != list.voidLink) {
 					lastLink = link = next;
 					pos++;
@@ -115,7 +116,7 @@
 			return pos + 1;
 		}
 
-		public Object previous() {
+		public ET previous() {
 			if (expectedModCount == list.modCount) {
 				if (link != list.voidLink) {
 					lastLink = link;
@@ -135,8 +136,8 @@
 		public void remove() {
 			if (expectedModCount == list.modCount) {
 				if (lastLink != null) {
-					Link next = lastLink.next;
-					Link previous = lastLink.previous;
+					Link<ET> next = lastLink.next;
+					Link<ET> previous = lastLink.previous;
 					next.previous = previous;
 					previous.next = next;
 					if (lastLink == link)
@@ -152,7 +153,7 @@
 				throw new ConcurrentModificationException();
 		}
 
-		public void set(Object object) {
+		public void set(ET object) {
 			if (expectedModCount == list.modCount) {
 				if (lastLink != null)
 					lastLink.data = object;
@@ -164,11 +165,11 @@
 	}
 
 	/**
-	 * Contructs a new empty instance of LinkedList.
+	 * Constructs a new empty instance of LinkedList.
 	 * 
 	 */
 	public LinkedList() {
-		voidLink = new Link(null, null, null);
+		voidLink = new Link<E>(null, null, null);
 		voidLink.previous = voidLink;
 		voidLink.next = voidLink;
 	}
@@ -182,7 +183,7 @@
 	 * @param collection
 	 *            the collection of elements to add
 	 */
-	public LinkedList(Collection collection) {
+	public LinkedList(Collection<? extends E> collection) {
 		this();
 		addAll(collection);
 	}
@@ -201,9 +202,9 @@
 	 * @exception IndexOutOfBoundsException
 	 *                when <code>location < 0 || >= size()</code>
 	 */
-	public void add(int location, Object object) {
+	public void add(int location, E object) {
 		if (0 <= location && location <= size) {
-			Link link = voidLink;
+			Link<E> link = voidLink;
 			if (location < (size / 2)) {
 				for (int i = 0; i <= location; i++)
 					link = link.next;
@@ -211,8 +212,8 @@
 				for (int i = size; i > location; i--)
 					link = link.previous;
 			}
-			Link previous = link.previous;
-			Link newLink = new Link(object, previous, link);
+			Link<E> previous = link.previous;
+			Link<E> newLink = new Link<E>(object, previous, link);
 			previous.next = newLink;
 			link.previous = newLink;
 			size++;
@@ -228,10 +229,10 @@
 	 *            the object to add
 	 * @return true
 	 */
-	public boolean add(Object object) {
-		// Cannot call addLast() as sublasses can override
-		Link oldLast = voidLink.previous;
-		Link newLink = new Link(object, oldLast, voidLink);
+	public boolean add(E object) {
+		// Cannot call addLast() as subclasses can override
+		Link<E> oldLast = voidLink.previous;
+		Link<E> newLink = new Link<E>(object, oldLast, voidLink);
 		voidLink.previous = newLink;
 		oldLast.next = newLink;
 		size++;
@@ -251,7 +252,7 @@
      * @exception IndexOutOfBoundsException when
      *            <code>location < 0 || > size()</code>
      */
-    public boolean addAll(int location, Collection collection) {
+    public boolean addAll(int location, Collection<? extends E> collection) {
         if (location < 0 || location > size) {
             throw new IndexOutOfBoundsException();
         }
@@ -259,7 +260,7 @@
         if (adding == 0) {
             return false;
         }
-        Link previous = voidLink;
+        Link<E> previous = voidLink;
         if (location < (size / 2)) {
             for (int i = 0; i < location; i++) {
                 previous = previous.next;
@@ -269,11 +270,9 @@
                 previous = previous.previous;
             }
         }
-        Link next = previous.next;
-
-        Iterator it = collection.iterator();
-        while (it.hasNext()) {
-            Link newLink = new Link(it.next(), previous, null);
+        Link<E> next = previous.next;
+        for (E e : collection) {
+            Link<E> newLink = new Link<E>(e, previous, null);
             previous.next = newLink;
             previous = newLink;
         }
@@ -291,14 +290,13 @@
 	 *            the Collection of objects
 	 * @return true if this LinkedList is modified, false otherwise
 	 */
-	public boolean addAll(Collection collection) {
+	public boolean addAll(Collection<? extends E> collection) {
 		int adding = collection.size();
 		if (adding == 0)
 			return false;
-		Link previous = voidLink.previous;
-		Iterator it = collection.iterator();
-		while (it.hasNext()) {
-			Link newLink = new Link(it.next(), previous, null);
+		Link<E> previous = voidLink.previous;
+        for (E e : collection) {
+			Link<E> newLink = new Link<E>(e, previous, null);
 			previous.next = newLink;
 			previous = newLink;
 		}
@@ -310,14 +308,14 @@
 	}
 
 	/**
-	 * Adds the specified object at the begining of this LinkedList.
+	 * Adds the specified object at the beginning of this LinkedList.
 	 * 
 	 * @param object
 	 *            the object to add
 	 */
-	public void addFirst(Object object) {
-		Link oldFirst = voidLink.next;
-		Link newLink = new Link(object, voidLink, oldFirst);
+	public void addFirst(E object) {
+		Link<E> oldFirst = voidLink.next;
+		Link<E> newLink = new Link<E>(object, voidLink, oldFirst);
 		voidLink.next = newLink;
 		oldFirst.previous = newLink;
 		size++;
@@ -330,9 +328,9 @@
 	 * @param object
 	 *            the object to add
 	 */
-	public void addLast(Object object) {
-		Link oldLast = voidLink.previous;
-		Link newLink = new Link(object, oldLast, voidLink);
+	public void addLast(E object) {
+		Link<E> oldLast = voidLink.previous;
+		Link<E> newLink = new Link<E>(object, oldLast, voidLink);
 		voidLink.previous = newLink;
 		oldLast.next = newLink;
 		size++;
@@ -363,7 +361,7 @@
 	 * @see java.lang.Cloneable
 	 */
 	public Object clone() {
-		return new LinkedList(this);
+		return new LinkedList<E>(this);
 	}
 
 	/**
@@ -375,7 +373,7 @@
 	 *         false otherwise
 	 */
 	public boolean contains(Object object) {
-		Link link = voidLink.next;
+		Link<E> link = voidLink.next;
 		if (object != null) {
 			while (link != voidLink) {
 				if (object.equals(link.data))
@@ -392,14 +390,9 @@
 		return false;
 	}
 
-	/*
-	 * (non-Javadoc)
-	 * 
-	 * @see java.util.List#get(int)
-	 */
-	public Object get(int location) {
+	public E get(int location) {
 		if (0 <= location && location < size) {
-			Link link = voidLink;
+			Link<E> link = voidLink;
 			if (location < (size / 2)) {
 				for (int i = 0; i <= location; i++)
 					link = link.next;
@@ -420,8 +413,8 @@
 	 * @exception NoSuchElementException
 	 *                when this LinkedList is empty
 	 */
-	public Object getFirst() {
-		Link first = voidLink.next;
+	public E getFirst() {
+		Link<E> first = voidLink.next;
 		if (first != voidLink)
 			return first.data;
 		throw new NoSuchElementException();
@@ -435,8 +428,8 @@
 	 * @exception NoSuchElementException
 	 *                when this LinkedList is empty
 	 */
-	public Object getLast() {
-		Link last = voidLink.previous;
+	public E getLast() {
+		Link<E> last = voidLink.previous;
 		if (last != voidLink)
 			return last.data;
 		throw new NoSuchElementException();
@@ -452,7 +445,7 @@
 	 */
 	public int indexOf(Object object) {
 		int pos = 0;
-		Link link = voidLink.next;
+		Link<E> link = voidLink.next;
 		if (object != null) {
 			while (link != voidLink) {
 				if (object.equals(link.data))
@@ -481,7 +474,7 @@
 	 */
 	public int lastIndexOf(Object object) {
 		int pos = size;
-		Link link = voidLink.previous;
+		Link<E> link = voidLink.previous;
 		if (object != null) {
 			while (link != voidLink) {
 				pos--;
@@ -514,8 +507,8 @@
 	 * 
 	 * @see ListIterator
 	 */
-	public ListIterator listIterator(int location) {
-		return new LinkIterator(this, location);
+	public ListIterator<E> listIterator(int location) {
+		return new LinkIterator<E>(this, location);
 	}
 
 	/**
@@ -528,9 +521,9 @@
 	 * @exception IndexOutOfBoundsException
 	 *                when <code>location < 0 || >= size()</code>
 	 */
-	public Object remove(int location) {
+	public E remove(int location) {
 		if (0 <= location && location < size) {
-			Link link = voidLink;
+			Link<E> link = voidLink;
 			if (location < (size / 2)) {
 				for (int i = 0; i <= location; i++)
 					link = link.next;
@@ -538,8 +531,8 @@
 				for (int i = size; i > location; i--)
 					link = link.previous;
 			}
-			Link previous = link.previous;
-			Link next = link.next;
+			Link<E> previous = link.previous;
+			Link<E> next = link.next;
 			previous.next = next;
 			next.previous = previous;
 			size--;
@@ -549,13 +542,8 @@
 			throw new IndexOutOfBoundsException();
 	}
 
-	/*
-	 * (non-Javadoc)
-	 * 
-	 * @see java.util.Collection#remove(java.lang.Object)
-	 */
 	public boolean remove(Object object) {
-		Link link = voidLink.next;
+		Link<E> link = voidLink.next;
 		if (object != null) {
 			while (link != voidLink && !object.equals(link.data))
 				link = link.next;
@@ -565,8 +553,8 @@
 		}
 		if (link == voidLink)
 			return false;
-		Link next = link.next;
-		Link previous = link.previous;
+		Link<E> next = link.next;
+		Link<E> previous = link.previous;
 		previous.next = next;
 		next.previous = previous;
 		size--;
@@ -582,10 +570,10 @@
 	 * @exception NoSuchElementException
 	 *                when this LinkedList is empty
 	 */
-	public Object removeFirst() {
-		Link first = voidLink.next;
+	public E removeFirst() {
+		Link<E> first = voidLink.next;
 		if (first != voidLink) {
-			Link next = first.next;
+			Link<E> next = first.next;
 			voidLink.next = next;
 			next.previous = voidLink;
 			size--;
@@ -603,10 +591,10 @@
 	 * @exception NoSuchElementException
 	 *                when this LinkedList is empty
 	 */
-	public Object removeLast() {
-		Link last = voidLink.previous;
+	public E removeLast() {
+		Link<E> last = voidLink.previous;
 		if (last != voidLink) {
-			Link previous = last.previous;
+			Link<E> previous = last.previous;
 			voidLink.previous = previous;
 			previous.next = voidLink;
 			size--;
@@ -629,9 +617,9 @@
 	 * @exception IndexOutOfBoundsException
 	 *                when <code>location < 0 || >= size()</code>
 	 */
-	public Object set(int location, Object object) {
+	public E set(int location, E object) {
 		if (0 <= location && location < size) {
-			Link link = voidLink;
+			Link<E> link = voidLink;
 			if (location < (size / 2)) {
 				for (int i = 0; i <= location; i++)
 					link = link.next;
@@ -639,7 +627,7 @@
 				for (int i = size; i > location; i--)
 					link = link.previous;
 			}
-			Object result = link.data;
+			E result = link.data;
 			link.data = object;
 			return result;
 		} else
@@ -663,7 +651,7 @@
 	public Object[] toArray() {
 		int index = 0;
 		Object[] contents = new Object[size];
-		Link link = voidLink.next;
+		Link<E> link = voidLink.next;
 		while (link != voidLink) {
 			contents[index++] = link.data;
 			link = link.next;
@@ -686,14 +674,15 @@
 	 *                when the type of an element in this LinkedList cannot be
 	 *                stored in the type of the specified array
 	 */
-	public Object[] toArray(Object[] contents) {
+	public <T> T[] toArray(T[] contents) {
 		int index = 0;
-		if (size > contents.length)
-			contents = (Object[]) Array.newInstance(contents.getClass()
-					.getComponentType(), size);
-		Link link = voidLink.next;
+		if (size > contents.length) {
+            contents = (T[]) Array.newInstance(contents.getClass()
+                    .getComponentType(), size);
+        }
+		Link<E> link = voidLink.next;
 		while (link != voidLink) {
-			contents[index++] = link.data;
+			contents[index++] = (T)link.data;
 			link = link.next;
 		}
 		if (index < contents.length)
@@ -713,10 +702,10 @@
 			ClassNotFoundException {
 		stream.defaultReadObject();
 		size = stream.readInt();
-		voidLink = new Link(null, null, null);
-		Link link = voidLink;
+		voidLink = new Link<E>(null, null, null);
+		Link<E> link = voidLink;
 		for (int i = size; --i >= 0;) {
-			Link nextLink = new Link(stream.readObject(), link, null);
+			Link<E> nextLink = new Link<E>((E)stream.readObject(), link, null);
 			link.next = nextLink;
 			link = nextLink;
 		}



Mime
View raw message