Return-Path: Delivered-To: apmail-incubator-harmony-commits-archive@www.apache.org Received: (qmail 27838 invoked from network); 31 May 2006 22:24:52 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 31 May 2006 22:24:52 -0000 Received: (qmail 737 invoked by uid 500); 31 May 2006 22:24:46 -0000 Delivered-To: apmail-incubator-harmony-commits-archive@incubator.apache.org Received: (qmail 640 invoked by uid 500); 31 May 2006 22:24:46 -0000 Mailing-List: contact harmony-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: harmony-dev@incubator.apache.org Delivered-To: mailing list harmony-commits@incubator.apache.org Received: (qmail 498 invoked by uid 99); 31 May 2006 22:24:45 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 31 May 2006 15:24:45 -0700 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received-SPF: pass (asf.osuosl.org: local policy) Received: from [140.211.166.113] (HELO eris.apache.org) (140.211.166.113) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 31 May 2006 15:24:43 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id EA3FD1A983A; Wed, 31 May 2006 15:24:22 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: harmony-commits@incubator.apache.org From: tellison@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20060531222422.EA3FD1A983A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N 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> backingMap; /** - * Contructs a new empty instance of HashSet. + * Constructs a new empty instance of HashSet. */ public HashSet() { - this(new HashMap()); + this(new HashMap>()); } /** @@ -46,7 +46,7 @@ * the initial capacity of this HashSet */ public HashSet(int capacity) { - this(new HashMap(capacity)); + this(new HashMap>(capacity)); } /** @@ -59,7 +59,7 @@ * the initial load factor */ public HashSet(int capacity, float loadFactor) { - this(new HashMap(capacity, loadFactor)); + this(new HashMap>(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 collection) { + this(new HashMap>(collection.size() < 6 ? 11 : collection.size() * 2)); + for (E e : collection) { + add(e); + } } - HashSet(HashMap backingMap) { + HashSet(HashMap> 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 clone = (HashSet) super.clone(); + clone.backingMap = (HashMap>) backingMap.clone(); return clone; } catch (CloneNotSupportedException e) { return null; @@ -149,7 +149,7 @@ * * @see Iterator */ - public Iterator iterator() { + public Iterator 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> 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> createBackingMap(int capacity, float loadFactor) { + return new HashMap>(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. *

- * If the three arg constructor is used, and order is specified as true, + * If the three argument constructor is used, and order is specified as true, * 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. *

* Null elements are allowed, and all the optional Map operations are supported. *

- * + * @since 1.4 */ -public class LinkedHashMap extends HashMap { +public class LinkedHashMap extends HashMap { private static final long serialVersionUID = 3801124242820219131L; private final boolean accessOrder; - transient private LinkedHashMapEntry head, tail; + transient private LinkedHashMapEntry 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 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 extends HashMapIterator { + LinkedHashIterator(MapEntry.Type value, LinkedHashMap 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)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 m = (LinkedHashMapEntry) 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) 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 lhme = (LinkedHashMapEntry) lastEntry; + LinkedHashMapEntry p = lhme.chainBackward; + LinkedHashMapEntry n = lhme.chainForward; + LinkedHashMap lhm = (LinkedHashMap) 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 extends HashMapEntrySet { + public LinkedHashMapEntrySet(LinkedHashMap lhm) { super(lhm); } - public Iterator iterator() { - return new LinkedHashIterator(new MapEntry.Type() { - public Object get(MapEntry entry) { + public Iterator> iterator() { + return new LinkedHashIterator,KT,VT>(new MapEntry.Type, KT, VT>() { + public Map.Entry get(MapEntry entry) { return entry; } - }, (LinkedHashMap) hashMap()); + }, (LinkedHashMap) hashMap()); } } - static final class LinkedHashMapEntry extends Entry { - LinkedHashMapEntry chainForward, chainBackward; + static final class LinkedHashMapEntry extends Entry { + LinkedHashMapEntry 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 entry = (LinkedHashMapEntry) super.clone(); entry.chainBackward = chainBackward; entry.chainForward = chainForward; - LinkedHashMapEntry lnext = (LinkedHashMapEntry) entry.next; + LinkedHashMapEntry lnext = (LinkedHashMapEntry) entry.next; if (lnext != null) - entry.next = (LinkedHashMapEntry) lnext.clone(); + entry.next = (LinkedHashMapEntry) lnext.clone(); return entry; } } @@ -207,7 +207,7 @@ * @param s * @return Reference to the element array */ - Entry[] newElementArray(int s) { + Entry[] 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 m = (LinkedHashMapEntry) getEntry(key); if (m == null) return null; if (accessOrder && tail != m) { - LinkedHashMapEntry p = m.chainBackward; - LinkedHashMapEntry n = m.chainForward; + LinkedHashMapEntry p = m.chainBackward; + LinkedHashMapEntry 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 createEntry(K key, int index, V value) { + LinkedHashMapEntry m = new LinkedHashMapEntry(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 m = (LinkedHashMapEntry) 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) 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 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 p = m.chainBackward; + LinkedHashMapEntry 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 m) { + for (Map.Entry 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> entrySet() { + return new LinkedHashMapEntrySet(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 keySet() { if (keySet == null) { - keySet = new AbstractSet() { + keySet = new AbstractSet() { 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 iterator() { + return new LinkedHashIterator(new MapEntry.Type() { + public K get(MapEntry 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 values() { if (valuesCollection == null) { - valuesCollection = new AbstractCollection() { + valuesCollection = new AbstractCollection() { 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 iterator() { + return new LinkedHashIterator(new MapEntry.Type() { + public V get(MapEntry 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 m = (LinkedHashMapEntry) removeEntry(key); if (m == null) return null; - LinkedHashMapEntry p = m.chainBackward; - LinkedHashMapEntry n = m.chainForward; + LinkedHashMapEntry p = m.chainBackward; + LinkedHashMapEntry 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 eldest) { return false; } @@ -502,11 +500,9 @@ * @see java.lang.Cloneable */ public Object clone() { - LinkedHashMap map = (LinkedHashMap) super.clone(); + LinkedHashMap map = (LinkedHashMap) super.clone(); map.clear(); - Iterator entries = entrySet().iterator(); - while (entries.hasNext()) { - Map.Entry entry = (Map.Entry) entries.next(); + for (Map.Entry 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 @@ *

* 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 extends HashSet implements Set, 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>()); } /** @@ -46,7 +47,7 @@ * the initial capacity of this HashSet */ public LinkedHashSet(int capacity) { - super(new LinkedHashMap(capacity)); + super(new LinkedHashMap>(capacity)); } /** @@ -59,7 +60,7 @@ * the initial load factor */ public LinkedHashSet(int capacity, float loadFactor) { - super(new LinkedHashMap(capacity, loadFactor)); + super(new LinkedHashMap>(capacity, loadFactor)); } /** @@ -70,15 +71,15 @@ * the collection of elements to add */ public LinkedHashSet(Collection collection) { - super(new LinkedHashMap(collection.size() < 6 ? 11 + super(new LinkedHashMap>(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> createBackingMap(int capacity, float loadFactor) { + return new LinkedHashMap>(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 extends AbstractSequentialList implements List, Cloneable, Serializable { private static final long serialVersionUID = 876323262645176354L; transient int size = 0; - transient Link voidLink; + transient Link voidLink; - private static final class Link { - Object data; + private static final class Link { + ET data; - Link previous, next; + Link previous, next; - Link(Object o, Link p, Link n) { + Link(ET o, Link p, Link n) { data = o; previous = p; next = n; } } - private static final class LinkIterator implements ListIterator { + private static final class LinkIterator implements ListIterator { int pos, expectedModCount; - final LinkedList list; + final LinkedList list; - Link link, lastLink; + Link link, lastLink; - LinkIterator(LinkedList object, int location) { + LinkIterator(LinkedList 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 next = link.next; + Link newLink = new Link(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 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 next = lastLink.next; + Link 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(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 collection) { this(); addAll(collection); } @@ -201,9 +202,9 @@ * @exception IndexOutOfBoundsException * when location < 0 || >= size() */ - public void add(int location, Object object) { + public void add(int location, E object) { if (0 <= location && location <= size) { - Link link = voidLink; + Link 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 previous = link.previous; + Link newLink = new Link(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 oldLast = voidLink.previous; + Link newLink = new Link(object, oldLast, voidLink); voidLink.previous = newLink; oldLast.next = newLink; size++; @@ -251,7 +252,7 @@ * @exception IndexOutOfBoundsException when * location < 0 || > size() */ - public boolean addAll(int location, Collection collection) { + public boolean addAll(int location, Collection collection) { if (location < 0 || location > size) { throw new IndexOutOfBoundsException(); } @@ -259,7 +260,7 @@ if (adding == 0) { return false; } - Link previous = voidLink; + Link 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 next = previous.next; + for (E e : collection) { + Link newLink = new Link(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 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 previous = voidLink.previous; + for (E e : collection) { + Link newLink = new Link(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 oldFirst = voidLink.next; + Link newLink = new Link(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 oldLast = voidLink.previous; + Link newLink = new Link(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(this); } /** @@ -375,7 +373,7 @@ * false otherwise */ public boolean contains(Object object) { - Link link = voidLink.next; + Link 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 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 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 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 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 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 listIterator(int location) { + return new LinkIterator(this, location); } /** @@ -528,9 +521,9 @@ * @exception IndexOutOfBoundsException * when location < 0 || >= size() */ - public Object remove(int location) { + public E remove(int location) { if (0 <= location && location < size) { - Link link = voidLink; + Link 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 previous = link.previous; + Link 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 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 next = link.next; + Link 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 first = voidLink.next; if (first != voidLink) { - Link next = first.next; + Link 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 last = voidLink.previous; if (last != voidLink) { - Link previous = last.previous; + Link previous = last.previous; voidLink.previous = previous; previous.next = voidLink; size--; @@ -629,9 +617,9 @@ * @exception IndexOutOfBoundsException * when location < 0 || >= size() */ - public Object set(int location, Object object) { + public E set(int location, E object) { if (0 <= location && location < size) { - Link link = voidLink; + Link 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 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[] 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 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(null, null, null); + Link link = voidLink; for (int i = size; --i >= 0;) { - Link nextLink = new Link(stream.readObject(), link, null); + Link nextLink = new Link((E)stream.readObject(), link, null); link.next = nextLink; link = nextLink; }