Index: src/main/java/java/util/LinkedList.java =================================================================== --- src/main/java/java/util/LinkedList.java (revision 408706) +++ src/main/java/java/util/LinkedList.java (working copy) @@ -27,35 +27,35 @@ * optional operations are supported, adding, removing and replacing. The * elements can be any objects. */ -public class LinkedList extends AbstractSequentialList implements List, - Cloneable, Serializable { +public class LinkedList extends AbstractSequentialList implements List, + Cloneable, Queue, 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 { + E data; - Link previous, next; + Link previous, next; - Link(Object o, Link p, Link n) { + Link(E 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 +74,10 @@ throw new IndexOutOfBoundsException(); } - public void add(Object object) { + public void add(E 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 +98,9 @@ return link != list.voidLink; } - public Object next() { + public E 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 +115,7 @@ return pos + 1; } - public Object previous() { + public E previous() { if (expectedModCount == list.modCount) { if (link != list.voidLink) { lastLink = link; @@ -135,8 +135,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 +152,7 @@ throw new ConcurrentModificationException(); } - public void set(Object object) { + public void set(E object) { if (expectedModCount == list.modCount) { if (lastLink != null) lastLink.data = object; @@ -168,7 +168,7 @@ * */ public LinkedList() { - voidLink = new Link(null, null, null); + voidLink = new Link(null, null, null); voidLink.previous = voidLink; voidLink.next = voidLink; } @@ -182,7 +182,7 @@ * @param collection * the collection of elements to add */ - public LinkedList(Collection collection) { + public LinkedList(Collection collection) { this(); addAll(collection); } @@ -201,9 +201,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 +211,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 +228,10 @@ * the object to add * @return true */ - public boolean add(Object object) { + public boolean add(E object) { // Cannot call addLast() as sublasses can override - Link oldLast = voidLink.previous; - Link newLink = new Link(object, oldLast, voidLink); + Link oldLast = voidLink.previous; + Link newLink = new Link(object, oldLast, voidLink); voidLink.previous = newLink; oldLast.next = newLink; size++; @@ -253,12 +253,12 @@ * @exception IndexOutOfBoundsException * when location < 0 || >= size() */ - public boolean addAll(int location, Collection collection) { + public boolean addAll(int location, Collection collection) { int adding = collection.size(); if (adding == 0) return false; if (0 <= location && location <= size) { - Link previous = voidLink; + Link previous = voidLink; if (location < (size / 2)) { for (int i = 0; i < location; i++) previous = previous.next; @@ -266,11 +266,11 @@ for (int i = size; i >= location; i--) previous = previous.previous; } - Link next = previous.next; + Link next = previous.next; Iterator it = collection.iterator(); while (it.hasNext()) { - Link newLink = new Link(it.next(), previous, null); + Link newLink = new Link((E) it.next(), previous, null); previous.next = newLink; previous = newLink; } @@ -290,14 +290,14 @@ * 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; + Link previous = voidLink.previous; Iterator it = collection.iterator(); while (it.hasNext()) { - Link newLink = new Link(it.next(), previous, null); + Link newLink = new Link((E) it.next(), previous, null); previous.next = newLink; previous = newLink; } @@ -314,9 +314,9 @@ * @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++; @@ -329,9 +329,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++; @@ -362,7 +362,7 @@ * @see java.lang.Cloneable */ public Object clone() { - return new LinkedList(this); + return new LinkedList(this); } /** @@ -374,7 +374,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)) @@ -396,9 +396,9 @@ * * @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; @@ -419,8 +419,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(); @@ -434,8 +434,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(); @@ -451,7 +451,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)) @@ -480,7 +480,7 @@ */ public int lastIndexOf(Object object) { int pos = size; - Link link = voidLink.previous; + Link link = voidLink.previous; if (object != null) { while (link != voidLink) { pos--; @@ -513,8 +513,8 @@ * * @see ListIterator */ - public ListIterator listIterator(int location) { - return new LinkIterator(this, location); + public ListIterator listIterator(int location) { + return new LinkIterator(this, location); } /** @@ -527,9 +527,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; @@ -537,8 +537,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--; @@ -554,7 +554,7 @@ * @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; @@ -564,8 +564,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--; @@ -581,10 +581,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--; @@ -602,10 +602,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--; @@ -628,9 +628,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; @@ -638,7 +638,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 @@ -662,7 +662,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; @@ -685,21 +685,44 @@ * 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() + contents = (T[]) Array.newInstance(contents.getClass() .getComponentType(), size); - Link link = voidLink.next; + Object array[] = contents; + Link link = voidLink.next; while (link != voidLink) { - contents[index++] = link.data; + array[index++] = link.data; link = link.next; } if (index < contents.length) - contents[index] = null; + array[index] = null; return contents; } + public boolean offer(E o) { + add(o); + return true; + } + + public E poll() { + return size == 0 ? null : removeFirst(); + } + + public E remove() { + return removeFirst(); + } + + public E peek() { + Link first = voidLink.next; + return first == voidLink ? null : first.data; + } + + public E element() { + return getFirst(); + } + private void writeObject(ObjectOutputStream stream) throws IOException { stream.defaultWriteObject(); stream.writeInt(size); @@ -712,10 +735,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; }