You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ba...@apache.org on 2009/09/15 07:30:02 UTC
svn commit: r814997 [7/18] - in /commons/proper/collections/trunk/src:
java/org/apache/commons/collections/ java/org/apache/commons/collections/bag/
java/org/apache/commons/collections/bidimap/
java/org/apache/commons/collections/buffer/ java/org/apach...
Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/list/AbstractLinkedList.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/list/AbstractLinkedList.java?rev=814997&r1=814996&r2=814997&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/list/AbstractLinkedList.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/list/AbstractLinkedList.java Tue Sep 15 05:29:56 2009
@@ -45,7 +45,7 @@
* @author Phil Steitz
* @author Stephen Colebourne
*/
-public abstract class AbstractLinkedList implements List {
+public abstract class AbstractLinkedList<E> implements List<E> {
/*
* Implementation notes:
@@ -63,9 +63,11 @@
* hold a value. The value of <code>next</code> is the first item in the
* list. The value of of <code>previous</code> is the last item in the list.
*/
- protected transient Node header;
+ protected transient Node<E> header;
+
/** The size of the list */
protected transient int size;
+
/** Modification count for iterators */
protected transient int modCount;
@@ -81,10 +83,10 @@
/**
* Constructs a list copying data from the specified collection.
- *
+ *
* @param coll the collection to copy
*/
- protected AbstractLinkedList(Collection coll) {
+ protected AbstractLinkedList(Collection<? extends E> coll) {
super();
init();
addAll(coll);
@@ -109,28 +111,28 @@
return (size() == 0);
}
- public Object get(int index) {
- Node node = getNode(index, false);
+ public E get(int index) {
+ Node<E> node = getNode(index, false);
return node.getValue();
}
//-----------------------------------------------------------------------
- public Iterator iterator() {
+ public Iterator<E> iterator() {
return listIterator();
}
- public ListIterator listIterator() {
- return new LinkedListIterator(this, 0);
+ public ListIterator<E> listIterator() {
+ return new LinkedListIterator<E>(this, 0);
}
- public ListIterator listIterator(int fromIndex) {
- return new LinkedListIterator(this, fromIndex);
+ public ListIterator<E> listIterator(int fromIndex) {
+ return new LinkedListIterator<E>(this, fromIndex);
}
//-----------------------------------------------------------------------
public int indexOf(Object value) {
int i = 0;
- for (Node node = header.next; node != header; node = node.next) {
+ for (Node<E> node = header.next; node != header; node = node.next) {
if (isEqualValue(node.getValue(), value)) {
return i;
}
@@ -141,7 +143,7 @@
public int lastIndexOf(Object value) {
int i = size - 1;
- for (Node node = header.previous; node != header; node = node.previous) {
+ for (Node<E> node = header.previous; node != header; node = node.previous) {
if (isEqualValue(node.getValue(), value)) {
return i;
}
@@ -154,31 +156,31 @@
return indexOf(value) != -1;
}
- public boolean containsAll(Collection coll) {
- Iterator it = coll.iterator();
- while (it.hasNext()) {
- if (contains(it.next()) == false) {
+ public boolean containsAll(Collection<?> coll) {
+ for (Object o : coll) {
+ if (!contains(o)) {
return false;
}
}
return true;
}
-
+
//-----------------------------------------------------------------------
public Object[] toArray() {
return toArray(new Object[size]);
}
- public Object[] toArray(Object[] array) {
+ @SuppressWarnings("unchecked")
+ public <T> T[] toArray(T[] array) {
// Extend the array if needed
if (array.length < size) {
Class componentType = array.getClass().getComponentType();
- array = (Object[]) Array.newInstance(componentType, size);
+ array = (T[]) Array.newInstance(componentType, size);
}
// Copy the values into the array
int i = 0;
- for (Node node = header.next; node != header; node = node.next, i++) {
- array[i] = node.getValue();
+ for (Node<E> node = header.next; node != header; node = node.next, i++) {
+ array[i] = (T) node.getValue();
}
// Set the value after the last value to null
if (array.length > size) {
@@ -189,49 +191,48 @@
/**
* Gets a sublist of the main list.
- *
+ *
* @param fromIndexInclusive the index to start from
* @param toIndexExclusive the index to end at
* @return the new sublist
*/
- public List subList(int fromIndexInclusive, int toIndexExclusive) {
- return new LinkedSubList(this, fromIndexInclusive, toIndexExclusive);
+ public List<E> subList(int fromIndexInclusive, int toIndexExclusive) {
+ return new LinkedSubList<E>(this, fromIndexInclusive, toIndexExclusive);
}
-
+
//-----------------------------------------------------------------------
- public boolean add(Object value) {
+ public boolean add(E value) {
addLast(value);
return true;
}
-
- public void add(int index, Object value) {
- Node node = getNode(index, true);
+
+ public void add(int index, E value) {
+ Node<E> node = getNode(index, true);
addNodeBefore(node, value);
}
-
- public boolean addAll(Collection coll) {
+
+ public boolean addAll(Collection<? extends E> coll) {
return addAll(size, coll);
}
- public boolean addAll(int index, Collection coll) {
- Node node = getNode(index, true);
- for (Iterator itr = coll.iterator(); itr.hasNext();) {
- Object value = itr.next();
- addNodeBefore(node, value);
+ public boolean addAll(int index, Collection<? extends E> coll) {
+ Node<E> node = getNode(index, true);
+ for (E e : coll) {
+ addNodeBefore(node, e);
}
return true;
}
//-----------------------------------------------------------------------
- public Object remove(int index) {
- Node node = getNode(index, false);
- Object oldValue = node.getValue();
+ public E remove(int index) {
+ Node<E> node = getNode(index, false);
+ E oldValue = node.getValue();
removeNode(node);
return oldValue;
}
public boolean remove(Object value) {
- for (Node node = header.next; node != header; node = node.next) {
+ for (Node<E> node = header.next; node != header; node = node.next) {
if (isEqualValue(node.getValue(), value)) {
removeNode(node);
return true;
@@ -240,9 +241,9 @@
return false;
}
- public boolean removeAll(Collection coll) {
+ public boolean removeAll(Collection<?> coll) {
boolean modified = false;
- Iterator it = iterator();
+ Iterator<E> it = iterator();
while (it.hasNext()) {
if (coll.contains(it.next())) {
it.remove();
@@ -253,9 +254,9 @@
}
//-----------------------------------------------------------------------
- public boolean retainAll(Collection coll) {
+ public boolean retainAll(Collection<?> coll) {
boolean modified = false;
- Iterator it = iterator();
+ Iterator<E> it = iterator();
while (it.hasNext()) {
if (coll.contains(it.next()) == false) {
it.remove();
@@ -265,9 +266,9 @@
return modified;
}
- public Object set(int index, Object value) {
- Node node = getNode(index, false);
- Object oldValue = node.getValue();
+ public E set(int index, E value) {
+ Node<E> node = getNode(index, false);
+ E oldValue = node.getValue();
updateNode(node, value);
return oldValue;
}
@@ -275,55 +276,56 @@
public void clear() {
removeAllNodes();
}
-
+
//-----------------------------------------------------------------------
- public Object getFirst() {
- Node node = header.next;
+ public E getFirst() {
+ Node<E> node = header.next;
if (node == header) {
throw new NoSuchElementException();
}
return node.getValue();
}
- public Object getLast() {
- Node node = header.previous;
+ public E getLast() {
+ Node<E> node = header.previous;
if (node == header) {
throw new NoSuchElementException();
}
return node.getValue();
}
- public boolean addFirst(Object o) {
+ public boolean addFirst(E o) {
addNodeAfter(header, o);
return true;
}
- public boolean addLast(Object o) {
+ public boolean addLast(E o) {
addNodeBefore(header, o);
return true;
}
- public Object removeFirst() {
- Node node = header.next;
+ public E removeFirst() {
+ Node<E> node = header.next;
if (node == header) {
throw new NoSuchElementException();
}
- Object oldValue = node.getValue();
+ E oldValue = node.getValue();
removeNode(node);
return oldValue;
}
- public Object removeLast() {
- Node node = header.previous;
+ public E removeLast() {
+ Node<E> node = header.previous;
if (node == header) {
throw new NoSuchElementException();
}
- Object oldValue = node.getValue();
+ E oldValue = node.getValue();
removeNode(node);
return oldValue;
}
//-----------------------------------------------------------------------
+ @SuppressWarnings("unchecked")
public boolean equals(Object obj) {
if (obj == this) {
return true;
@@ -348,10 +350,8 @@
public int hashCode() {
int hashCode = 1;
- Iterator it = iterator();
- while (it.hasNext()) {
- Object obj = it.next();
- hashCode = 31 * hashCode + (obj == null ? 0 : obj.hashCode());
+ for (E e : this) {
+ hashCode = 31 * hashCode + (e == null ? 0 : e.hashCode());
}
return hashCode;
}
@@ -363,7 +363,7 @@
StringBuffer buf = new StringBuffer(16 * size());
buf.append("[");
- Iterator it = iterator();
+ Iterator<E> it = iterator();
boolean hasNext = it.hasNext();
while (hasNext) {
Object value = it.next();
@@ -382,7 +382,7 @@
* Compares two values for equals.
* This implementation uses the equals method.
* Subclasses can override this to match differently.
- *
+ *
* @param value1 the first value to compare, may be null
* @param value2 the second value to compare, may be null
* @return true if equal
@@ -390,16 +390,16 @@
protected boolean isEqualValue(Object value1, Object value2) {
return (value1 == value2 || (value1 == null ? false : value1.equals(value2)));
}
-
+
/**
* Updates the node with a new value.
* This implementation sets the value on the node.
* Subclasses can override this to record the change.
- *
+ *
* @param node node to update
* @param value new value of the node
*/
- protected void updateNode(Node node, Object value) {
+ protected void updateNode(Node<E> node, E value) {
node.setValue(value);
}
@@ -407,26 +407,26 @@
* Creates a new node with previous, next and element all set to null.
* This implementation creates a new empty Node.
* Subclasses can override this to create a different class.
- *
+ *
* @return newly created node
*/
- protected Node createHeaderNode() {
- return new Node();
+ protected Node<E> createHeaderNode() {
+ return new Node<E>();
}
/**
* Creates a new node with the specified properties.
* This implementation creates a new Node with data.
* Subclasses can override this to create a different class.
- *
+ *
* @param value value of the new node
*/
- protected Node createNode(Object value) {
- return new Node(value);
+ protected Node<E> createNode(E value) {
+ return new Node<E>(value);
}
/**
- * Creates a new node with the specified object as its
+ * Creates a new node with the specified object as its
* <code>value</code> and inserts it before <code>node</code>.
* <p>
* This implementation uses {@link #createNode(Object)} and
@@ -436,24 +436,24 @@
* @param value value of the newly added node
* @throws NullPointerException if <code>node</code> is null
*/
- protected void addNodeBefore(Node node, Object value) {
- Node newNode = createNode(value);
+ protected void addNodeBefore(Node<E> node, E value) {
+ Node<E> newNode = createNode(value);
addNode(newNode, node);
}
/**
- * Creates a new node with the specified object as its
+ * Creates a new node with the specified object as its
* <code>value</code> and inserts it after <code>node</code>.
* <p>
* This implementation uses {@link #createNode(Object)} and
* {@link #addNode(AbstractLinkedList.Node,AbstractLinkedList.Node)}.
- *
+ *
* @param node node to insert after
* @param value value of the newly added node
* @throws NullPointerException if <code>node</code> is null
*/
- protected void addNodeAfter(Node node, Object value) {
- Node newNode = createNode(value);
+ protected void addNodeAfter(Node<E> node, E value) {
+ Node<E> newNode = createNode(value);
addNode(newNode, node.next);
}
@@ -464,7 +464,7 @@
* @param insertBeforeNode node to insert before
* @throws NullPointerException if either node is null
*/
- protected void addNode(Node nodeToInsert, Node insertBeforeNode) {
+ protected void addNode(Node<E> nodeToInsert, Node<E> insertBeforeNode) {
nodeToInsert.next = insertBeforeNode;
nodeToInsert.previous = insertBeforeNode.previous;
insertBeforeNode.previous.next = nodeToInsert;
@@ -479,7 +479,7 @@
* @param node the node to remove
* @throws NullPointerException if <code>node</code> is null
*/
- protected void removeNode(Node node) {
+ protected void removeNode(Node<E> node) {
node.previous.next = node.next;
node.next.previous = node.previous;
size--;
@@ -498,7 +498,7 @@
/**
* Gets the node at a particular index.
- *
+ *
* @param index the index, starting from 0
* @param endMarkerAllowed whether or not the end marker can be returned if
* startIndex is set to the list's size
@@ -506,7 +506,7 @@
* the size of the list and endMakerAllowed is false; or greater than the
* size of the list
*/
- protected Node getNode(int index, boolean endMarkerAllowed) throws IndexOutOfBoundsException {
+ protected Node<E> getNode(int index, boolean endMarkerAllowed) throws IndexOutOfBoundsException {
// Check the index is within the bounds
if (index < 0) {
throw new IndexOutOfBoundsException("Couldn't get the node: " +
@@ -522,7 +522,7 @@
"list (" + size + ").");
}
// Search the list and get the node
- Node node;
+ Node<E> node;
if (index < (size / 2)) {
// Search forwards
node = header.next;
@@ -542,21 +542,21 @@
//-----------------------------------------------------------------------
/**
* Creates an iterator for the sublist.
- *
+ *
* @param subList the sublist to get an iterator for
*/
- protected Iterator createSubListIterator(LinkedSubList subList) {
+ protected Iterator<E> createSubListIterator(LinkedSubList<E> subList) {
return createSubListListIterator(subList, 0);
}
/**
* Creates a list iterator for the sublist.
- *
+ *
* @param subList the sublist to get an iterator for
* @param fromIndex the index to start from, relative to the sublist
*/
- protected ListIterator createSubListListIterator(LinkedSubList subList, int fromIndex) {
- return new LinkedSubListIterator(subList, fromIndex);
+ protected ListIterator<E> createSubListListIterator(LinkedSubList<E> subList, int fromIndex) {
+ return new LinkedSubListIterator<E>(subList, fromIndex);
}
//-----------------------------------------------------------------------
@@ -569,7 +569,7 @@
protected void doWriteObject(ObjectOutputStream outputStream) throws IOException {
// Write the size so we know how many nodes to read back
outputStream.writeInt(size());
- for (Iterator itr = iterator(); itr.hasNext();) {
+ for (Iterator<E> itr = iterator(); itr.hasNext();) {
outputStream.writeObject(itr.next());
}
}
@@ -580,11 +580,12 @@
* The first serializable subclass must call this method from
* <code>readObject</code>.
*/
+ @SuppressWarnings("unchecked")
protected void doReadObject(ObjectInputStream inputStream) throws IOException, ClassNotFoundException {
init();
int size = inputStream.readInt();
for (int i = 0; i < size; i++) {
- add(inputStream.readObject());
+ add((E) inputStream.readObject());
}
}
@@ -595,14 +596,14 @@
* From Commons Collections 3.1, all access to the <code>value</code> property
* is via the methods on this class.
*/
- protected static class Node {
+ protected static class Node<E> {
/** A pointer to the node before this node */
- protected Node previous;
+ protected Node<E> previous;
/** A pointer to the node after this node */
- protected Node next;
+ protected Node<E> next;
/** The object contained within this node */
- protected Object value;
+ protected E value;
/**
* Constructs a new header node.
@@ -615,85 +616,85 @@
/**
* Constructs a new node.
- *
+ *
* @param value the value to store
*/
- protected Node(Object value) {
+ protected Node(E value) {
super();
this.value = value;
}
-
+
/**
* Constructs a new node.
- *
+ *
* @param previous the previous node in the list
* @param next the next node in the list
* @param value the value to store
*/
- protected Node(Node previous, Node next, Object value) {
+ protected Node(Node<E> previous, Node<E> next, E value) {
super();
this.previous = previous;
this.next = next;
this.value = value;
}
-
+
/**
* Gets the value of the node.
- *
+ *
* @return the value
* @since Commons Collections 3.1
*/
- protected Object getValue() {
+ protected E getValue() {
return value;
}
-
+
/**
* Sets the value of the node.
- *
+ *
* @param value the value
* @since Commons Collections 3.1
*/
- protected void setValue(Object value) {
+ protected void setValue(E value) {
this.value = value;
}
-
+
/**
* Gets the previous node.
- *
+ *
* @return the previous node
* @since Commons Collections 3.1
*/
- protected Node getPreviousNode() {
+ protected Node<E> getPreviousNode() {
return previous;
}
-
+
/**
* Sets the previous node.
- *
+ *
* @param previous the previous node
* @since Commons Collections 3.1
*/
- protected void setPreviousNode(Node previous) {
+ protected void setPreviousNode(Node<E> previous) {
this.previous = previous;
}
-
+
/**
* Gets the next node.
- *
+ *
* @return the next node
* @since Commons Collections 3.1
*/
- protected Node getNextNode() {
+ protected Node<E> getNextNode() {
return next;
}
-
+
/**
* Sets the next node.
- *
+ *
* @param next the next node
* @since Commons Collections 3.1
*/
- protected void setNextNode(Node next) {
+ protected void setNextNode(Node<E> next) {
this.next = next;
}
}
@@ -702,16 +703,16 @@
/**
* A list iterator over the linked list.
*/
- protected static class LinkedListIterator implements ListIterator, OrderedIterator {
-
+ protected static class LinkedListIterator<E> implements ListIterator<E>, OrderedIterator<E> {
+
/** The parent list */
- protected final AbstractLinkedList parent;
+ protected final AbstractLinkedList<E> parent;
/**
* The node that will be returned by {@link #next()}. If this is equal
* to {@link AbstractLinkedList#header} then there are no more values to return.
*/
- protected Node next;
+ protected Node<E> next;
/**
* The index of {@link #next}.
@@ -726,7 +727,7 @@
* Should be accessed through {@link #getLastNodeReturned()} to enforce
* this behaviour.
*/
- protected Node current;
+ protected Node<E> current;
/**
* The modification count that the list is expected to have. If the list
@@ -738,11 +739,11 @@
/**
* Create a ListIterator for a list.
- *
+ *
* @param parent the parent list
* @param fromIndex the index to start at
*/
- protected LinkedListIterator(AbstractLinkedList parent, int fromIndex) throws IndexOutOfBoundsException {
+ protected LinkedListIterator(AbstractLinkedList<E> parent, int fromIndex) throws IndexOutOfBoundsException {
super();
this.parent = parent;
this.expectedModCount = parent.modCount;
@@ -753,7 +754,7 @@
/**
* Checks the modification count of the list is the value that this
* object expects.
- *
+ *
* @throws ConcurrentModificationException If the list's modification
* count isn't the value that was expected.
*/
@@ -765,12 +766,12 @@
/**
* Gets the last node returned.
- *
+ *
* @throws IllegalStateException If {@link #next()} or
* {@link #previous()} haven't been called, or if the node has been removed
* with {@link #remove()} or a new node added with {@link #add(Object)}.
*/
- protected Node getLastNodeReturned() throws IllegalStateException {
+ protected Node<E> getLastNodeReturned() throws IllegalStateException {
if (current == null) {
throw new IllegalStateException();
}
@@ -781,12 +782,12 @@
return next != parent.header;
}
- public Object next() {
+ public E next() {
checkModCount();
if (!hasNext()) {
throw new NoSuchElementException("No element at index " + nextIndex + ".");
}
- Object value = next.getValue();
+ E value = next.getValue();
current = next;
next = next.next;
nextIndex++;
@@ -797,13 +798,13 @@
return next.previous != parent.header;
}
- public Object previous() {
+ public E previous() {
checkModCount();
if (!hasPrevious()) {
throw new NoSuchElementException("Already at start of list.");
}
next = next.previous;
- Object value = next.getValue();
+ E value = next.getValue();
current = next;
nextIndex--;
return value;
@@ -833,12 +834,12 @@
expectedModCount++;
}
- public void set(Object obj) {
+ public void set(E obj) {
checkModCount();
getLastNodeReturned().setValue(obj);
}
- public void add(Object obj) {
+ public void add(E obj) {
checkModCount();
parent.addNodeBefore(next, obj);
current = null;
@@ -852,12 +853,12 @@
/**
* A list iterator over the linked sub list.
*/
- protected static class LinkedSubListIterator extends LinkedListIterator {
-
+ protected static class LinkedSubListIterator<E> extends LinkedListIterator<E> {
+
/** The parent list */
- protected final LinkedSubList sub;
-
- protected LinkedSubListIterator(LinkedSubList sub, int startIndex) {
+ protected final LinkedSubList<E> sub;
+
+ protected LinkedSubListIterator(LinkedSubList<E> sub, int startIndex) {
super(sub.parent, startIndex + sub.offset);
this.sub = sub;
}
@@ -874,26 +875,26 @@
return (super.nextIndex() - sub.offset);
}
- public void add(Object obj) {
+ public void add(E obj) {
super.add(obj);
sub.expectedModCount = parent.modCount;
sub.size++;
}
-
+
public void remove() {
super.remove();
sub.expectedModCount = parent.modCount;
sub.size--;
}
}
-
+
//-----------------------------------------------------------------------
/**
* The sublist implementation for AbstractLinkedList.
*/
- protected static class LinkedSubList extends AbstractList {
+ protected static class LinkedSubList<E> extends AbstractList<E> {
/** The main list */
- AbstractLinkedList parent;
+ AbstractLinkedList<E> parent;
/** Offset from the main list */
int offset;
/** Sublist size */
@@ -901,7 +902,7 @@
/** Sublist modCount */
int expectedModCount;
- protected LinkedSubList(AbstractLinkedList parent, int fromIndex, int toIndex) {
+ protected LinkedSubList(AbstractLinkedList<E> parent, int fromIndex, int toIndex) {
if (fromIndex < 0) {
throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
}
@@ -922,13 +923,13 @@
return size;
}
- public Object get(int index) {
+ public E get(int index) {
rangeCheck(index, size);
checkModCount();
return parent.get(index + offset);
}
- public void add(int index, Object obj) {
+ public void add(int index, E obj) {
rangeCheck(index, size + 1);
checkModCount();
parent.add(index + offset, obj);
@@ -937,21 +938,21 @@
LinkedSubList.this.modCount++;
}
- public Object remove(int index) {
+ public E remove(int index) {
rangeCheck(index, size);
checkModCount();
- Object result = parent.remove(index + offset);
+ E result = parent.remove(index + offset);
expectedModCount = parent.modCount;
size--;
LinkedSubList.this.modCount++;
return result;
}
- public boolean addAll(Collection coll) {
+ public boolean addAll(Collection<? extends E> coll) {
return addAll(size, coll);
}
- public boolean addAll(int index, Collection coll) {
+ public boolean addAll(int index, Collection<? extends E> coll) {
rangeCheck(index, size + 1);
int cSize = coll.size();
if (cSize == 0) {
@@ -966,7 +967,7 @@
return true;
}
- public Object set(int index, Object obj) {
+ public E set(int index, E obj) {
rangeCheck(index, size);
checkModCount();
return parent.set(index + offset, obj);
@@ -974,26 +975,26 @@
public void clear() {
checkModCount();
- Iterator it = iterator();
+ Iterator<E> it = iterator();
while (it.hasNext()) {
it.next();
it.remove();
}
}
- public Iterator iterator() {
+ public Iterator<E> iterator() {
checkModCount();
return parent.createSubListIterator(this);
}
- public ListIterator listIterator(final int index) {
+ public ListIterator<E> listIterator(final int index) {
rangeCheck(index, size + 1);
checkModCount();
return parent.createSubListListIterator(this, index);
}
- public List subList(int fromIndexInclusive, int toIndexExclusive) {
- return new LinkedSubList(parent, fromIndexInclusive + offset, toIndexExclusive + offset);
+ public List<E> subList(int fromIndexInclusive, int toIndexExclusive) {
+ return new LinkedSubList<E>(parent, fromIndexInclusive + offset, toIndexExclusive + offset);
}
protected void rangeCheck(int index, int beyond) {
@@ -1008,5 +1009,5 @@
}
}
}
-
+
}
Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/list/AbstractSerializableListDecorator.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/list/AbstractSerializableListDecorator.java?rev=814997&r1=814996&r2=814997&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/list/AbstractSerializableListDecorator.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/list/AbstractSerializableListDecorator.java Tue Sep 15 05:29:56 2009
@@ -29,8 +29,8 @@
* @author Stephen Colebourne
* @since Commons Collections 3.1
*/
-public abstract class AbstractSerializableListDecorator
- extends AbstractListDecorator
+public abstract class AbstractSerializableListDecorator<E>
+ extends AbstractListDecorator<E>
implements Serializable {
/** Serialization version */
@@ -39,7 +39,7 @@
/**
* Constructor.
*/
- protected AbstractSerializableListDecorator(List list) {
+ protected AbstractSerializableListDecorator(List<E> list) {
super(list);
}
@@ -62,9 +62,10 @@
* @throws IOException
* @throws ClassNotFoundException
*/
+ @SuppressWarnings("unchecked")
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
- collection = (Collection) in.readObject();
+ collection = (Collection<E>) in.readObject();
}
}
Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/list/CursorableLinkedList.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/list/CursorableLinkedList.java?rev=814997&r1=814996&r2=814997&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/list/CursorableLinkedList.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/list/CursorableLinkedList.java Tue Sep 15 05:29:56 2009
@@ -60,13 +60,13 @@
* @author Simon Kitching
* @author Stephen Colebourne
*/
-public class CursorableLinkedList extends AbstractLinkedList implements Serializable {
+public class CursorableLinkedList<E> extends AbstractLinkedList<E> implements Serializable {
/** Ensure serialization compatibility */
private static final long serialVersionUID = 8836393098519411393L;
/** A list of the cursor currently open on this list */
- protected transient List cursors = new ArrayList();
+ protected transient List<WeakReference<Cursor<E>>> cursors;
//-----------------------------------------------------------------------
/**
@@ -82,7 +82,7 @@
*
* @param coll the collection to copy
*/
- public CursorableLinkedList(Collection coll) {
+ public CursorableLinkedList(Collection<E> coll) {
super(coll);
}
@@ -92,7 +92,7 @@
*/
protected void init() {
super.init();
- cursors = new ArrayList();
+ cursors = new ArrayList<WeakReference<Cursor<E>>>();
}
//-----------------------------------------------------------------------
@@ -105,7 +105,7 @@
*
* @return a new iterator that does <b>not</b> support concurrent modification
*/
- public Iterator iterator() {
+ public Iterator<E> iterator() {
return super.listIterator(0);
}
@@ -124,7 +124,7 @@
*
* @return a new cursor iterator
*/
- public ListIterator listIterator() {
+ public ListIterator<E> listIterator() {
return cursor(0);
}
@@ -144,7 +144,7 @@
* @param fromIndex the index to start from
* @return a new cursor iterator
*/
- public ListIterator listIterator(int fromIndex) {
+ public ListIterator<E> listIterator(int fromIndex) {
return cursor(fromIndex);
}
@@ -171,7 +171,7 @@
*
* @return a new cursor iterator
*/
- public CursorableLinkedList.Cursor cursor() {
+ public CursorableLinkedList.Cursor<E> cursor() {
return cursor(0);
}
@@ -202,8 +202,8 @@
* @throws IndexOutOfBoundsException if the index is out of range
* (index < 0 || index > size()).
*/
- public CursorableLinkedList.Cursor cursor(int fromIndex) {
- Cursor cursor = new Cursor(this, fromIndex);
+ public CursorableLinkedList.Cursor<E> cursor(int fromIndex) {
+ Cursor<E> cursor = new Cursor<E>(this, fromIndex);
registerCursor(cursor);
return cursor;
}
@@ -217,7 +217,7 @@
* @param node node to update
* @param value new value of the node
*/
- protected void updateNode(Node node, Object value) {
+ protected void updateNode(Node<E> node, E value) {
super.updateNode(node, value);
broadcastNodeChanged(node);
}
@@ -229,7 +229,7 @@
* @param insertBeforeNode node to insert before
* @throws NullPointerException if either node is null
*/
- protected void addNode(Node nodeToInsert, Node insertBeforeNode) {
+ protected void addNode(Node<E> nodeToInsert, Node<E> insertBeforeNode) {
super.addNode(nodeToInsert, insertBeforeNode);
broadcastNodeInserted(nodeToInsert);
}
@@ -240,7 +240,7 @@
* @param node the node to remove
* @throws NullPointerException if <code>node</code> is null
*/
- protected void removeNode(Node node) {
+ protected void removeNode(Node<E> node) {
super.removeNode(node);
broadcastNodeRemoved(node);
}
@@ -251,7 +251,7 @@
protected void removeAllNodes() {
if (size() > 0) {
// superclass implementation would break all the iterators
- Iterator it = iterator();
+ Iterator<E> it = iterator();
while (it.hasNext()) {
it.next();
it.remove();
@@ -265,16 +265,16 @@
*
* @param cursor the cursor to register
*/
- protected void registerCursor(Cursor cursor) {
+ protected void registerCursor(Cursor<E> cursor) {
// We take this opportunity to clean the cursors list
// of WeakReference objects to garbage-collected cursors.
- for (Iterator it = cursors.iterator(); it.hasNext();) {
- WeakReference ref = (WeakReference) it.next();
+ for (Iterator<WeakReference<Cursor<E>>> it = cursors.iterator(); it.hasNext();) {
+ WeakReference<Cursor<E>> ref = it.next();
if (ref.get() == null) {
it.remove();
}
}
- cursors.add(new WeakReference(cursor));
+ cursors.add(new WeakReference<Cursor<E>>(cursor));
}
/**
@@ -282,16 +282,15 @@
*
* @param cursor the cursor to deregister
*/
- protected void unregisterCursor(Cursor cursor) {
- for (Iterator it = cursors.iterator(); it.hasNext();) {
- WeakReference ref = (WeakReference) it.next();
- Cursor cur = (Cursor) ref.get();
+ protected void unregisterCursor(Cursor<E> cursor) {
+ for (Iterator<WeakReference<Cursor<E>>> it = cursors.iterator(); it.hasNext();) {
+ WeakReference<Cursor<E>> ref = it.next();
+ Cursor<E> cur = ref.get();
if (cur == null) {
// some other unrelated cursor object has been
// garbage-collected; let's take the opportunity to
// clean up the cursors list anyway..
it.remove();
-
} else if (cur == cursor) {
ref.clear();
it.remove();
@@ -307,11 +306,11 @@
*
* @param node the node that was changed
*/
- protected void broadcastNodeChanged(Node node) {
- Iterator it = cursors.iterator();
+ protected void broadcastNodeChanged(Node<E> node) {
+ Iterator<WeakReference<Cursor<E>>> it = cursors.iterator();
while (it.hasNext()) {
- WeakReference ref = (WeakReference) it.next();
- Cursor cursor = (Cursor) ref.get();
+ WeakReference<Cursor<E>> ref = it.next();
+ Cursor<E> cursor = ref.get();
if (cursor == null) {
it.remove(); // clean up list
} else {
@@ -326,11 +325,11 @@
*
* @param node the node that was changed
*/
- protected void broadcastNodeRemoved(Node node) {
- Iterator it = cursors.iterator();
+ protected void broadcastNodeRemoved(Node<E> node) {
+ Iterator<WeakReference<Cursor<E>>> it = cursors.iterator();
while (it.hasNext()) {
- WeakReference ref = (WeakReference) it.next();
- Cursor cursor = (Cursor) ref.get();
+ WeakReference<Cursor<E>> ref = it.next();
+ Cursor<E> cursor = ref.get();
if (cursor == null) {
it.remove(); // clean up list
} else {
@@ -345,11 +344,11 @@
*
* @param node the node that was changed
*/
- protected void broadcastNodeInserted(Node node) {
- Iterator it = cursors.iterator();
+ protected void broadcastNodeInserted(Node<E> node) {
+ Iterator<WeakReference<Cursor<E>>> it = cursors.iterator();
while (it.hasNext()) {
- WeakReference ref = (WeakReference) it.next();
- Cursor cursor = (Cursor) ref.get();
+ WeakReference<Cursor<E>> ref = it.next();
+ Cursor<E> cursor = ref.get();
if (cursor == null) {
it.remove(); // clean up list
} else {
@@ -382,8 +381,8 @@
* @param subList the sublist to get an iterator for
* @param fromIndex the index to start from, relative to the sublist
*/
- protected ListIterator createSubListListIterator(LinkedSubList subList, int fromIndex) {
- SubCursor cursor = new SubCursor(subList, fromIndex);
+ protected ListIterator<E> createSubListListIterator(LinkedSubList<E> subList, int fromIndex) {
+ SubCursor<E> cursor = new SubCursor<E>(subList, fromIndex);
registerCursor(cursor);
return cursor;
}
@@ -393,7 +392,7 @@
* An extended <code>ListIterator</code> that allows concurrent changes to
* the underlying list.
*/
- public static class Cursor extends AbstractLinkedList.LinkedListIterator {
+ public static class Cursor<E> extends AbstractLinkedList.LinkedListIterator<E> {
/** Is the cursor valid (not closed) */
boolean valid = true;
/** Is the next index valid */
@@ -406,7 +405,7 @@
*
* @param index the index to start from
*/
- protected Cursor(CursorableLinkedList parent, int index) {
+ protected Cursor(CursorableLinkedList<E> parent, int index) {
super(parent, index);
valid = true;
}
@@ -443,7 +442,7 @@
*
* @param obj the object to add
*/
- public void add(Object obj) {
+ public void add(E obj) {
// overridden, as the nodeInserted() method updates the iterator state
super.add(obj);
// matches the (next.previous == node) clause in nodeInserted()
@@ -467,7 +466,7 @@
nextIndex = parent.size();
} else {
int pos = 0;
- Node temp = parent.header.next;
+ Node<E> temp = parent.header.next;
while (temp != next) {
pos++;
temp = temp.next;
@@ -484,7 +483,7 @@
*
* @param node the node that changed
*/
- protected void nodeChanged(Node node) {
+ protected void nodeChanged(Node<E> node) {
// do nothing
}
@@ -493,7 +492,7 @@
*
* @param node the node that was removed
*/
- protected void nodeRemoved(Node node) {
+ protected void nodeRemoved(Node<E> node) {
if (node == next && node == current) {
// state where next() followed by previous()
next = node.next;
@@ -521,7 +520,7 @@
*
* @param node the node that was added
*/
- protected void nodeInserted(Node node) {
+ protected void nodeInserted(Node<E> node) {
if (node.previous == current) {
next = node;
} else if (next.previous == node) {
@@ -550,7 +549,7 @@
*/
public void close() {
if (valid) {
- ((CursorableLinkedList) parent).unregisterCursor(this);
+ ((CursorableLinkedList<E>) parent).unregisterCursor(this);
valid = false;
}
}
@@ -562,18 +561,18 @@
*
* @since Commons Collections 3.2
*/
- protected static class SubCursor extends Cursor {
+ protected static class SubCursor<E> extends Cursor<E> {
/** The parent list */
- protected final LinkedSubList sub;
+ protected final LinkedSubList<E> sub;
/**
* Constructs a new cursor.
*
* @param index the index to start from
*/
- protected SubCursor(LinkedSubList sub, int index) {
- super((CursorableLinkedList) sub.parent, index + sub.offset);
+ protected SubCursor(LinkedSubList<E> sub, int index) {
+ super((CursorableLinkedList<E>) sub.parent, index + sub.offset);
this.sub = sub;
}
@@ -589,7 +588,7 @@
return (super.nextIndex() - sub.offset);
}
- public void add(Object obj) {
+ public void add(E obj) {
super.add(obj);
sub.expectedModCount = parent.modCount;
sub.size++;
Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/list/NodeCachingLinkedList.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/list/NodeCachingLinkedList.java?rev=814997&r1=814996&r2=814997&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/list/NodeCachingLinkedList.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/list/NodeCachingLinkedList.java Tue Sep 15 05:29:56 2009
@@ -44,7 +44,7 @@
* @author Phil Steitz
* @author Stephen Colebourne
*/
-public class NodeCachingLinkedList extends AbstractLinkedList implements Serializable {
+public class NodeCachingLinkedList<E> extends AbstractLinkedList<E> implements Serializable {
/** Serialization version */
private static final long serialVersionUID = 6897789178562232073L;
@@ -59,7 +59,7 @@
* Cached nodes are stored in a singly-linked list with
* <code>next</code> pointing to the next element.
*/
- protected transient Node firstCachedNode;
+ protected transient Node<E> firstCachedNode;
/**
* The size of the cache.
@@ -84,7 +84,7 @@
*
* @param coll the collection to copy
*/
- public NodeCachingLinkedList(Collection coll) {
+ public NodeCachingLinkedList(Collection<E> coll) {
super(coll);
this.maximumCacheSize = DEFAULT_MAXIMUM_CACHE_SIZE;
}
@@ -137,11 +137,11 @@
*
* @return a node, or <code>null</code> if there are no nodes in the cache.
*/
- protected Node getNodeFromCache() {
+ protected Node<E> getNodeFromCache() {
if (cacheSize == 0) {
return null;
}
- Node cachedNode = firstCachedNode;
+ Node<E> cachedNode = firstCachedNode;
firstCachedNode = cachedNode.next;
cachedNode.next = null; // This should be changed anyway, but defensively
// set it to null.
@@ -164,13 +164,13 @@
*
* @param node the node to add to the cache
*/
- protected void addNodeToCache(Node node) {
+ protected void addNodeToCache(Node<E> node) {
if (isCacheFull()) {
// don't cache the node.
return;
}
// clear the node's contents and add it to the cache.
- Node nextCachedNode = firstCachedNode;
+ Node<E> nextCachedNode = firstCachedNode;
node.previous = null;
node.next = nextCachedNode;
node.setValue(null);
@@ -186,14 +186,13 @@
* @param value value of the new node
* @return the newly created node
*/
- protected Node createNode(Object value) {
- Node cachedNode = getNodeFromCache();
+ protected Node<E> createNode(E value) {
+ Node<E> cachedNode = getNodeFromCache();
if (cachedNode == null) {
return super.createNode(value);
- } else {
- cachedNode.setValue(value);
- return cachedNode;
}
+ cachedNode.setValue(value);
+ return cachedNode;
}
/**
@@ -202,7 +201,7 @@
*
* @param node the node to remove
*/
- protected void removeNode(Node node) {
+ protected void removeNode(Node<E> node) {
super.removeNode(node);
addNodeToCache(node);
}
@@ -218,9 +217,9 @@
// {@link AbstractLinkedList.removeAllNodes()} removes the
// nodes by removing references directly from {@link #header}.
int numberOfNodesToCache = Math.min(size, maximumCacheSize - cacheSize);
- Node node = header.next;
+ Node<E> node = header.next;
for (int currentIndex = 0; currentIndex < numberOfNodesToCache; currentIndex++) {
- Node oldNode = node;
+ Node<E> oldNode = node;
node = node.next;
addNodeToCache(oldNode);
}
Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/list/SetUniqueList.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/list/SetUniqueList.java?rev=814997&r1=814996&r2=814997&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/list/SetUniqueList.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/list/SetUniqueList.java Tue Sep 15 05:29:56 2009
@@ -51,7 +51,7 @@
* @author Stephen Colebourne
* @author Tom Dunham
*/
-public class SetUniqueList extends AbstractSerializableListDecorator {
+public class SetUniqueList<E> extends AbstractSerializableListDecorator<E> {
/** Serialization version */
private static final long serialVersionUID = 7196982186153478694L;
@@ -59,30 +59,29 @@
/**
* Internal Set to maintain uniqueness.
*/
- protected final Set set;
+ protected final Set<E> set;
/**
* Factory method to create a SetList using the supplied list to retain order.
* <p>
* If the list contains duplicates, these are removed (first indexed one kept).
* A <code>HashSet</code> is used for the set behaviour.
- *
+ *
* @param list the list to decorate, must not be null
* @throws IllegalArgumentException if list is null
*/
- public static SetUniqueList decorate(List list) {
+ public static <E> SetUniqueList<E> decorate(List<E> list) {
if (list == null) {
throw new IllegalArgumentException("List must not be null");
}
if (list.isEmpty()) {
- return new SetUniqueList(list, new HashSet());
- } else {
- List temp = new ArrayList(list);
- list.clear();
- SetUniqueList sl = new SetUniqueList(list, new HashSet());
- sl.addAll(temp);
- return sl;
+ return new SetUniqueList<E>(list, new HashSet<E>());
}
+ List<E> temp = new ArrayList<E>(list);
+ list.clear();
+ SetUniqueList<E> sl = new SetUniqueList<E>(list, new HashSet<E>());
+ sl.addAll(temp);
+ return sl;
}
//-----------------------------------------------------------------------
@@ -90,12 +89,12 @@
* Constructor that wraps (not copies) the List and specifies the set to use.
* <p>
* The set and list must both be correctly initialised to the same elements.
- *
+ *
* @param set the set to decorate, must not be null
* @param list the list to decorate, must not be null
* @throws IllegalArgumentException if set or list is null
*/
- protected SetUniqueList(List list, Set set) {
+ protected SetUniqueList(List<E> list, Set<E> set) {
super(list);
if (set == null) {
throw new IllegalArgumentException("Set must not be null");
@@ -106,10 +105,10 @@
//-----------------------------------------------------------------------
/**
* Gets an unmodifiable view as a Set.
- *
+ *
* @return an unmodifiable set view
*/
- public Set asSet() {
+ public Set<E> asSet() {
return UnmodifiableSet.decorate(set);
}
@@ -121,11 +120,11 @@
* The <code>List</code> interface requires that this method returns
* <code>true</code> always. However this class may return <code>false</code>
* because of the <code>Set</code> behaviour.
- *
+ *
* @param object the object to add
* @return true if object was added
*/
- public boolean add(Object object) {
+ public boolean add(E object) {
// gets initial size
final int sizeBefore = size();
@@ -142,11 +141,11 @@
* <i>(Violation)</i>
* The <code>List</code> interface makes the assumption that the element is
* always inserted. This may not happen with this implementation.
- *
+ *
* @param index the index to insert at
* @param object the object to add
*/
- public void add(int index, Object object) {
+ public void add(int index, E object) {
// adds element if it is not contained already
if (set.contains(object) == false) {
super.add(index, object);
@@ -163,11 +162,11 @@
* <i>(Violation)</i>
* The <code>List</code> interface makes the assumption that the elements
* are always inserted. This may not happen with this implementation.
- *
+ *
* @param coll the collection to add in iterator order
* @return true if this collection changed
*/
- public boolean addAll(Collection coll) {
+ public boolean addAll(Collection<? extends E> coll) {
return addAll(size(), coll);
}
@@ -181,27 +180,24 @@
* <i>(Violation)</i>
* The <code>List</code> interface makes the assumption that the elements
* are always inserted. This may not happen with this implementation.
- *
+ *
* @param index the index to insert at
* @param coll the collection to add in iterator order
* @return true if this collection changed
*/
- public boolean addAll(int index, Collection coll) {
- // gets initial size
- final int sizeBefore = size();
-
- // adds all elements
- for (final Iterator it = coll.iterator(); it.hasNext();) {
- int sizeBeforeAddNext = size();
- add(index, it.next());
- // if it was inserted, then increase the target index
- if (sizeBeforeAddNext != size()) {
- index++;
+ public boolean addAll(int index, Collection<? extends E> coll) {
+ HashSet<E> temp = new HashSet<E>(coll);
+ temp.removeAll(set);
+ if (temp.isEmpty()) {
+ return false;
+ }
+ for (E e : coll) {
+ if (temp.contains(e)) {
+ add(index, e);
+ index++;
}
}
-
- // compares sizes to detect if collection changed
- return sizeBefore != size();
+ return true;
}
//-----------------------------------------------------------------------
@@ -212,14 +208,14 @@
* Afterwards, any previous duplicate is removed
* If the object is not already in the list then a normal set occurs.
* If it is present, then the old version is removed.
- *
+ *
* @param index the index to insert at
* @param object the object to set
* @return the previous object
*/
- public Object set(int index, Object object) {
+ public E set(int index, E object) {
int pos = indexOf(object);
- Object removed = super.set(index, object);
+ E removed = super.set(index, object);
if (pos != -1 && pos != index) {
// the object is already in the uniq list
@@ -239,19 +235,19 @@
return result;
}
- public Object remove(int index) {
- Object result = super.remove(index);
+ public E remove(int index) {
+ E result = super.remove(index);
set.remove(result);
return result;
}
- public boolean removeAll(Collection coll) {
+ public boolean removeAll(Collection<?> coll) {
boolean result = super.removeAll(coll);
set.removeAll(coll);
return result;
}
- public boolean retainAll(Collection coll) {
+ public boolean retainAll(Collection<?> coll) {
boolean result = super.retainAll(coll);
set.retainAll(coll);
return result;
@@ -266,39 +262,40 @@
return set.contains(object);
}
- public boolean containsAll(Collection coll) {
+ public boolean containsAll(Collection<?> coll) {
return set.containsAll(coll);
}
- public Iterator iterator() {
- return new SetListIterator(super.iterator(), set);
+ public Iterator<E> iterator() {
+ return new SetListIterator<E>(super.iterator(), set);
}
- public ListIterator listIterator() {
- return new SetListListIterator(super.listIterator(), set);
+ public ListIterator<E> listIterator() {
+ return new SetListListIterator<E>(super.listIterator(), set);
}
- public ListIterator listIterator(int index) {
- return new SetListListIterator(super.listIterator(index), set);
+ public ListIterator<E> listIterator(int index) {
+ return new SetListListIterator<E>(super.listIterator(index), set);
}
- public List subList(int fromIndex, int toIndex) {
- List superSubList = super.subList(fromIndex, toIndex);
- Set subSet = createSetBasedOnList(set, superSubList);
- return new SetUniqueList(superSubList, subSet);
+ public List<E> subList(int fromIndex, int toIndex) {
+ List<E> superSubList = super.subList(fromIndex, toIndex);
+ Set<E> subSet = createSetBasedOnList(set, superSubList);
+ return new SetUniqueList<E>(superSubList, subSet);
}
- protected Set createSetBasedOnList(Set set, List list) {
- Set subSet = null;
- if(set.getClass().equals(HashSet.class)) {
- subSet = new HashSet();
+ @SuppressWarnings("unchecked")
+ protected Set<E> createSetBasedOnList(Set<E> set, List<E> list) {
+ Set<E> subSet = null;
+ if (set.getClass().equals(HashSet.class)) {
+ subSet = new HashSet<E>();
} else {
try {
- subSet = (Set) set.getClass().newInstance();
- } catch(InstantiationException ie) {
- subSet = new HashSet();
- } catch(IllegalAccessException iae) {
- subSet = new HashSet();
+ subSet = (Set<E>) set.getClass().newInstance();
+ } catch (InstantiationException ie) {
+ subSet = new HashSet<E>();
+ } catch (IllegalAccessException iae) {
+ subSet = new HashSet<E>();
}
}
subSet.addAll(list);
@@ -309,17 +306,17 @@
/**
* Inner class iterator.
*/
- static class SetListIterator extends AbstractIteratorDecorator {
-
- protected final Set set;
- protected Object last = null;
-
- protected SetListIterator(Iterator it, Set set) {
+ static class SetListIterator<E> extends AbstractIteratorDecorator<E> {
+
+ protected final Set<E> set;
+ protected E last = null;
+
+ protected SetListIterator(Iterator<E> it, Set<E> set) {
super(it);
this.set = set;
}
-
- public Object next() {
+
+ public E next() {
last = super.next();
return last;
}
@@ -330,26 +327,26 @@
last = null;
}
}
-
+
/**
* Inner class iterator.
*/
- static class SetListListIterator extends AbstractListIteratorDecorator {
-
- protected final Set set;
- protected Object last = null;
-
- protected SetListListIterator(ListIterator it, Set set) {
+ static class SetListListIterator<E> extends AbstractListIteratorDecorator<E> {
+
+ protected final Set<E> set;
+ protected E last = null;
+
+ protected SetListListIterator(ListIterator<E> it, Set<E> set) {
super(it);
this.set = set;
}
-
- public Object next() {
+
+ public E next() {
last = super.next();
return last;
}
- public Object previous() {
+ public E previous() {
last = super.previous();
return last;
}
@@ -360,16 +357,16 @@
last = null;
}
- public void add(Object object) {
+ public void add(E object) {
if (set.contains(object) == false) {
super.add(object);
set.add(object);
}
}
-
- public void set(Object object) {
+
+ public void set(E object) {
throw new UnsupportedOperationException("ListIterator does not support set");
}
}
-
+
}
Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/list/TransformedList.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/list/TransformedList.java?rev=814997&r1=814996&r2=814997&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/list/TransformedList.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/list/TransformedList.java Tue Sep 15 05:29:56 2009
@@ -39,7 +39,7 @@
*
* @author Stephen Colebourne
*/
-public class TransformedList extends TransformedCollection implements List {
+public class TransformedList<E> extends TransformedCollection<E> implements List<E> {
/** Serialization version */
private static final long serialVersionUID = 1077193035000013141L;
@@ -55,8 +55,8 @@
* @param transformer the transformer to use for conversion, must not be null
* @throws IllegalArgumentException if list or transformer is null
*/
- public static List decorate(List list, Transformer transformer) {
- return new TransformedList(list, transformer);
+ public static <E> List<E> decorate(List<E> list, Transformer<? super E, ? extends E> transformer) {
+ return new TransformedList<E>(list, transformer);
}
/**
@@ -73,13 +73,14 @@
* @throws IllegalArgumentException if list or transformer is null
* @since Commons Collections 3.3
*/
+ // TODO: Generics
public static List decorateTransform(List list, Transformer transformer) {
TransformedList decorated = new TransformedList(list, transformer);
if (transformer != null && list != null && list.size() > 0) {
Object[] values = list.toArray();
list.clear();
for(int i=0; i<values.length; i++) {
- decorated.getCollection().add(transformer.transform(values[i]));
+ decorated.decorated().add(transformer.transform(values[i]));
}
}
return decorated;
@@ -96,7 +97,7 @@
* @param transformer the transformer to use for conversion, must not be null
* @throws IllegalArgumentException if list or transformer is null
*/
- protected TransformedList(List list, Transformer transformer) {
+ protected TransformedList(List<E> list, Transformer<? super E, ? extends E> transformer) {
super(list, transformer);
}
@@ -105,12 +106,12 @@
*
* @return the decorated list
*/
- protected List getList() {
- return (List) collection;
+ protected List<E> getList() {
+ return (List<E>) collection;
}
//-----------------------------------------------------------------------
- public Object get(int index) {
+ public E get(int index) {
return getList().get(index);
}
@@ -122,54 +123,54 @@
return getList().lastIndexOf(object);
}
- public Object remove(int index) {
+ public E remove(int index) {
return getList().remove(index);
}
//-----------------------------------------------------------------------
- public void add(int index, Object object) {
+ public void add(int index, E object) {
object = transform(object);
getList().add(index, object);
}
- public boolean addAll(int index, Collection coll) {
+ public boolean addAll(int index, Collection<? extends E> coll) {
coll = transform(coll);
return getList().addAll(index, coll);
}
- public ListIterator listIterator() {
+ public ListIterator<E> listIterator() {
return listIterator(0);
}
- public ListIterator listIterator(int i) {
+ public ListIterator<E> listIterator(int i) {
return new TransformedListIterator(getList().listIterator(i));
}
- public Object set(int index, Object object) {
+ public E set(int index, E object) {
object = transform(object);
return getList().set(index, object);
}
- public List subList(int fromIndex, int toIndex) {
- List sub = getList().subList(fromIndex, toIndex);
- return new TransformedList(sub, transformer);
+ public List<E> subList(int fromIndex, int toIndex) {
+ List<E> sub = getList().subList(fromIndex, toIndex);
+ return new TransformedList<E>(sub, transformer);
}
/**
* Inner class Iterator for the TransformedList
*/
- protected class TransformedListIterator extends AbstractListIteratorDecorator {
-
- protected TransformedListIterator(ListIterator iterator) {
+ protected class TransformedListIterator extends AbstractListIteratorDecorator<E> {
+
+ protected TransformedListIterator(ListIterator<E> iterator) {
super(iterator);
}
-
- public void add(Object object) {
+
+ public void add(E object) {
object = transform(object);
iterator.add(object);
}
-
- public void set(Object object) {
+
+ public void set(E object) {
object = transform(object);
iterator.set(object);
}
Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/list/TreeList.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/list/TreeList.java?rev=814997&r1=814996&r2=814997&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/java/org/apache/commons/collections/list/TreeList.java (original)
+++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/list/TreeList.java Tue Sep 15 05:29:56 2009
@@ -48,7 +48,7 @@
* <p>
* <code>LinkedList</code> is rarely a good choice of implementation.
* <code>TreeList</code> is almost always a good replacement for it, although it
- * does use sligtly more memory.
+ * does use slightly more memory.
*
* @since Commons Collections 3.1
* @version $Revision$ $Date$
@@ -56,14 +56,14 @@
* @author Joerg Schmuecker
* @author Stephen Colebourne
*/
-public class TreeList extends AbstractList {
+public class TreeList<E> extends AbstractList<E> {
// add; toArray; iterator; insert; get; indexOf; remove
// TreeList = 1260;7360;3080; 160; 170;3400; 170;
// ArrayList = 220;1480;1760; 6870; 50;1540; 7200;
// LinkedList = 270;7360;3350;55860;290720;2910;55200;
/** The root node in the AVL tree */
- private AVLNode root;
+ private AVLNode<E> root;
/** The current size of the list */
private int size;
@@ -78,11 +78,11 @@
/**
* Constructs a new empty list that copies the specified list.
- *
+ *
* @param coll the collection to copy
* @throws NullPointerException if the collection is null
*/
- public TreeList(Collection coll) {
+ public TreeList(Collection<E> coll) {
super();
addAll(coll);
}
@@ -90,18 +90,18 @@
//-----------------------------------------------------------------------
/**
* Gets the element at the specified index.
- *
+ *
* @param index the index to retrieve
* @return the element at the specified index
*/
- public Object get(int index) {
+ public E get(int index) {
checkInterval(index, 0, size() - 1);
return root.get(index).getValue();
}
/**
* Gets the current size of the list.
- *
+ *
* @return the current size
*/
public int size() {
@@ -110,40 +110,40 @@
/**
* Gets an iterator over the list.
- *
+ *
* @return an iterator over the list
*/
- public Iterator iterator() {
+ public Iterator<E> iterator() {
// override to go 75% faster
return listIterator(0);
}
/**
* Gets a ListIterator over the list.
- *
+ *
* @return the new iterator
*/
- public ListIterator listIterator() {
+ public ListIterator<E> listIterator() {
// override to go 75% faster
return listIterator(0);
}
/**
* Gets a ListIterator over the list.
- *
+ *
* @param fromIndex the index to start from
* @return the new iterator
*/
- public ListIterator listIterator(int fromIndex) {
+ public ListIterator<E> listIterator(int fromIndex) {
// override to go 75% faster
// cannot use EmptyIterator as iterator.add() must work
checkInterval(fromIndex, 0, size());
- return new TreeListIterator(this, fromIndex);
+ return new TreeListIterator<E>(this, fromIndex);
}
/**
* Searches for the index of an object in the list.
- *
+ *
* @return the index of the object, -1 if not found
*/
public int indexOf(Object object) {
@@ -156,7 +156,7 @@
/**
* Searches for the presence of an object in the list.
- *
+ *
* @return true if the object is found
*/
public boolean contains(Object object) {
@@ -165,7 +165,7 @@
/**
* Converts the list into an array.
- *
+ *
* @return the list as an array
*/
public Object[] toArray() {
@@ -180,15 +180,15 @@
//-----------------------------------------------------------------------
/**
* Adds a new element to the list.
- *
+ *
* @param index the index to add before
* @param obj the element to add
*/
- public void add(int index, Object obj) {
+ public void add(int index, E obj) {
modCount++;
checkInterval(index, 0, size());
if (root == null) {
- root = new AVLNode(index, obj, null, null);
+ root = new AVLNode<E>(index, obj, null, null);
} else {
root = root.insert(index, obj);
}
@@ -197,30 +197,30 @@
/**
* Sets the element at the specified index.
- *
+ *
* @param index the index to set
* @param obj the object to store at the specified index
* @return the previous object at that index
* @throws IndexOutOfBoundsException if the index is invalid
*/
- public Object set(int index, Object obj) {
+ public E set(int index, E obj) {
checkInterval(index, 0, size() - 1);
- AVLNode node = root.get(index);
- Object result = node.value;
+ AVLNode<E> node = root.get(index);
+ E result = node.value;
node.setValue(obj);
return result;
}
/**
* Removes the element at the specified index.
- *
+ *
* @param index the index to remove
* @return the previous object at that index
*/
- public Object remove(int index) {
+ public E remove(int index) {
modCount++;
checkInterval(index, 0, size() - 1);
- Object result = get(index);
+ E result = get(index);
root = root.remove(index);
size--;
return result;
@@ -238,7 +238,7 @@
//-----------------------------------------------------------------------
/**
* Checks whether the index is valid.
- *
+ *
* @param index the index to check
* @param startIndex the first allowed index
* @param endIndex the last allowed index
@@ -263,13 +263,13 @@
* The Faedelung calculation stores a flag for both the left and right child
* to indicate if they are a child (false) or a link as in linked list (true).
*/
- static class AVLNode {
+ static class AVLNode<E> {
/** The left child node or the predecessor if {@link #leftIsPrevious}.*/
- private AVLNode left;
+ private AVLNode<E> left;
/** Flag indicating that left reference is not a subtree but the predecessor. */
private boolean leftIsPrevious;
/** The right child node or the successor if {@link #rightIsNext}. */
- private AVLNode right;
+ private AVLNode<E> right;
/** Flag indicating that right reference is not a subtree but the successor. */
private boolean rightIsNext;
/** How many levels of left/right are below this one. */
@@ -277,17 +277,17 @@
/** The relative position, root holds absolute position. */
private int relativePosition;
/** The stored element. */
- private Object value;
+ private E value;
/**
* Constructs a new node with a relative position.
- *
+ *
* @param relativePosition the relative position of the node
* @param obj the value for the ndoe
* @param rightFollower the node with the value following this one
* @param leftFollower the node with the value leading this one
*/
- private AVLNode(int relativePosition, Object obj, AVLNode rightFollower, AVLNode leftFollower) {
+ private AVLNode(int relativePosition, E obj, AVLNode<E> rightFollower, AVLNode<E> leftFollower) {
this.relativePosition = relativePosition;
value = obj;
rightIsNext = true;
@@ -298,19 +298,19 @@
/**
* Gets the value.
- *
+ *
* @return the value of this node
*/
- Object getValue() {
+ E getValue() {
return value;
}
/**
* Sets the value.
- *
+ *
* @param obj the value to store
*/
- void setValue(Object obj) {
+ void setValue(E obj) {
this.value = obj;
}
@@ -318,14 +318,14 @@
* Locate the element with the given index relative to the
* offset of the parent of this node.
*/
- AVLNode get(int index) {
+ AVLNode<E> get(int index) {
int indexRelativeToMe = index - relativePosition;
if (indexRelativeToMe == 0) {
return this;
}
- AVLNode nextNode = ((indexRelativeToMe < 0) ? getLeftSubTree() : getRightSubTree());
+ AVLNode<E> nextNode = ((indexRelativeToMe < 0) ? getLeftSubTree() : getRightSubTree());
if (nextNode == null) {
return null;
}
@@ -353,7 +353,7 @@
/**
* Stores the node and its children into the array specified.
- *
+ *
* @param array the array to be filled
* @param index the index of this node
*/
@@ -369,10 +369,10 @@
/**
* Gets the next node in the list after this one.
- *
+ *
* @return the next node
*/
- AVLNode next() {
+ AVLNode<E> next() {
if (rightIsNext || right == null) {
return right;
}
@@ -381,10 +381,10 @@
/**
* Gets the node in the list before this one.
- *
+ *
* @return the previous node
*/
- AVLNode previous() {
+ AVLNode<E> previous() {
if (leftIsPrevious || left == null) {
return left;
}
@@ -392,27 +392,26 @@
}
/**
- * Inserts a node at the position index.
- *
- * @param index is the index of the position relative to the position of
+ * Inserts a node at the position index.
+ *
+ * @param index is the index of the position relative to the position of
* the parent node.
* @param obj is the object to be stored in the position.
*/
- AVLNode insert(int index, Object obj) {
+ AVLNode<E> insert(int index, E obj) {
int indexRelativeToMe = index - relativePosition;
if (indexRelativeToMe <= 0) {
return insertOnLeft(indexRelativeToMe, obj);
- } else {
- return insertOnRight(indexRelativeToMe, obj);
}
+ return insertOnRight(indexRelativeToMe, obj);
}
- private AVLNode insertOnLeft(int indexRelativeToMe, Object obj) {
- AVLNode ret = this;
+ private AVLNode<E> insertOnLeft(int indexRelativeToMe, E obj) {
+ AVLNode<E> ret = this;
if (getLeftSubTree() == null) {
- setLeft(new AVLNode(-1, obj, this, left), null);
+ setLeft(new AVLNode<E>(-1, obj, this, left), null);
} else {
setLeft(left.insert(indexRelativeToMe, obj), null);
}
@@ -425,11 +424,11 @@
return ret;
}
- private AVLNode insertOnRight(int indexRelativeToMe, Object obj) {
- AVLNode ret = this;
+ private AVLNode<E> insertOnRight(int indexRelativeToMe, E obj) {
+ AVLNode<E> ret = this;
if (getRightSubTree() == null) {
- setRight(new AVLNode(+1, obj, right, this), null);
+ setRight(new AVLNode<E>(+1, obj, right, this), null);
} else {
setRight(right.insert(indexRelativeToMe, obj), null);
}
@@ -445,42 +444,42 @@
/**
* Gets the left node, returning null if its a faedelung.
*/
- private AVLNode getLeftSubTree() {
+ private AVLNode<E> getLeftSubTree() {
return (leftIsPrevious ? null : left);
}
/**
* Gets the right node, returning null if its a faedelung.
*/
- private AVLNode getRightSubTree() {
+ private AVLNode<E> getRightSubTree() {
return (rightIsNext ? null : right);
}
/**
* Gets the rightmost child of this node.
- *
+ *
* @return the rightmost child (greatest index)
*/
- private AVLNode max() {
+ private AVLNode<E> max() {
return (getRightSubTree() == null) ? this : right.max();
}
/**
* Gets the leftmost child of this node.
- *
+ *
* @return the leftmost child (smallest index)
*/
- private AVLNode min() {
+ private AVLNode<E> min() {
return (getLeftSubTree() == null) ? this : left.min();
}
/**
* Removes the node at a given position.
- *
- * @param index is the index of the element to be removed relative to the position of
+ *
+ * @param index is the index of the element to be removed relative to the position of
* the parent node of the current node.
*/
- AVLNode remove(int index) {
+ AVLNode<E> remove(int index) {
int indexRelativeToMe = index - relativePosition;
if (indexRelativeToMe == 0) {
@@ -501,7 +500,7 @@
return balance();
}
- private AVLNode removeMax() {
+ private AVLNode<E> removeMax() {
if (getRightSubTree() == null) {
return removeSelf();
}
@@ -513,7 +512,7 @@
return balance();
}
- private AVLNode removeMin() {
+ private AVLNode<E> removeMin() {
if (getLeftSubTree() == null) {
return removeSelf();
}
@@ -530,7 +529,7 @@
*
* @return the node that replaces this one in the parent
*/
- private AVLNode removeSelf() {
+ private AVLNode<E> removeSelf() {
if (getRightSubTree() == null && getLeftSubTree() == null) {
return null;
}
@@ -549,7 +548,7 @@
if (heightRightMinusLeft() > 0) {
// more on the right, so delete from the right
- AVLNode rightMin = right.min();
+ AVLNode<E> rightMin = right.min();
value = rightMin.value;
if (leftIsPrevious) {
left = rightMin.left;
@@ -560,12 +559,12 @@
}
} else {
// more on the left or equal, so delete from the left
- AVLNode leftMax = left.max();
+ AVLNode<E> leftMax = left.max();
value = leftMax.value;
if (rightIsNext) {
right = leftMax.right;
}
- AVLNode leftPrevious = left.left;
+ AVLNode<E> leftPrevious = left.left;
left = left.removeMax();
if (left == null) {
// special case where left that was deleted was a double link
@@ -585,7 +584,7 @@
/**
* Balances according to the AVL algorithm.
*/
- private AVLNode balance() {
+ private AVLNode<E> balance() {
switch (heightRightMinusLeft()) {
case 1 :
case 0 :
@@ -609,7 +608,7 @@
/**
* Gets the relative position.
*/
- private int getOffset(AVLNode node) {
+ private int getOffset(AVLNode<E> node) {
if (node == null) {
return 0;
}
@@ -619,7 +618,7 @@
/**
* Sets the relative position.
*/
- private int setOffset(AVLNode node, int newOffest) {
+ private int setOffset(AVLNode<E> node, int newOffest) {
if (node == null) {
return 0;
}
@@ -640,7 +639,7 @@
/**
* Returns the height of the node or -1 if the node is null.
*/
- private int getHeight(AVLNode node) {
+ private int getHeight(AVLNode<E> node) {
return (node == null ? -1 : node.height);
}
@@ -651,9 +650,9 @@
return getHeight(getRightSubTree()) - getHeight(getLeftSubTree());
}
- private AVLNode rotateLeft() {
- AVLNode newTop = right; // can't be faedelung!
- AVLNode movedNode = getRightSubTree().getLeftSubTree();
+ private AVLNode<E> rotateLeft() {
+ AVLNode<E> newTop = right; // can't be faedelung!
+ AVLNode<E> movedNode = getRightSubTree().getLeftSubTree();
int newTopPosition = relativePosition + getOffset(newTop);
int myNewPosition = -newTop.relativePosition;
@@ -668,9 +667,9 @@
return newTop;
}
- private AVLNode rotateRight() {
- AVLNode newTop = left; // can't be faedelung
- AVLNode movedNode = getLeftSubTree().getRightSubTree();
+ private AVLNode<E> rotateRight() {
+ AVLNode<E> newTop = left; // can't be faedelung
+ AVLNode<E> movedNode = getLeftSubTree().getRightSubTree();
int newTopPosition = relativePosition + getOffset(newTop);
int myNewPosition = -newTop.relativePosition;
@@ -691,7 +690,7 @@
* @param node the new left subtree node
* @param previous the previous node in the linked list
*/
- private void setLeft(AVLNode node, AVLNode previous) {
+ private void setLeft(AVLNode<E> node, AVLNode<E> previous) {
leftIsPrevious = (node == null);
left = (leftIsPrevious ? previous : node);
recalcHeight();
@@ -703,7 +702,7 @@
* @param node the new left subtree node
* @param next the next node in the linked list
*/
- private void setRight(AVLNode node, AVLNode next) {
+ private void setRight(AVLNode<E> node, AVLNode<E> next) {
rightIsNext = (node == null);
right = (rightIsNext ? next : node);
recalcHeight();
@@ -747,7 +746,7 @@
// }
// return count + left.checkLeftSubNode();
// }
-//
+//
// private int checkRightSubNode() {
// AVLNode right = getRightSubTree();
// if (right == null) {
@@ -773,13 +772,13 @@
/**
* A list iterator over the linked list.
*/
- static class TreeListIterator implements ListIterator, OrderedIterator {
+ static class TreeListIterator<E> implements ListIterator<E>, OrderedIterator<E> {
/** The parent list */
- protected final TreeList parent;
+ protected final TreeList<E> parent;
/**
* Cache of the next node that will be returned by {@link #next()}.
*/
- protected AVLNode next;
+ protected AVLNode<E> next;
/**
* The index of the next node to be returned.
*/
@@ -788,7 +787,7 @@
* Cache of the last node that was returned by {@link #next()}
* or {@link #previous()}.
*/
- protected AVLNode current;
+ protected AVLNode<E> current;
/**
* The index of the last node that was returned.
*/
@@ -803,11 +802,11 @@
/**
* Create a ListIterator for a list.
- *
+ *
* @param parent the parent list
* @param fromIndex the index to start at
*/
- protected TreeListIterator(TreeList parent, int fromIndex) throws IndexOutOfBoundsException {
+ protected TreeListIterator(TreeList<E> parent, int fromIndex) throws IndexOutOfBoundsException {
super();
this.parent = parent;
this.expectedModCount = parent.modCount;
@@ -819,7 +818,7 @@
/**
* Checks the modification count of the list is the value that this
* object expects.
- *
+ *
* @throws ConcurrentModificationException If the list's modification
* count isn't the value that was expected.
*/
@@ -833,7 +832,7 @@
return (nextIndex < parent.size());
}
- public Object next() {
+ public E next() {
checkModCount();
if (!hasNext()) {
throw new NoSuchElementException("No element at index " + nextIndex + ".");
@@ -841,7 +840,7 @@
if (next == null) {
next = parent.root.get(nextIndex);
}
- Object value = next.getValue();
+ E value = next.getValue();
current = next;
currentIndex = nextIndex++;
next = next.next();
@@ -852,7 +851,7 @@
return (nextIndex > 0);
}
- public Object previous() {
+ public E previous() {
checkModCount();
if (!hasPrevious()) {
throw new NoSuchElementException("Already at start of list.");
@@ -862,7 +861,7 @@
} else {
next = next.previous();
}
- Object value = next.getValue();
+ E value = next.getValue();
current = next;
currentIndex = --nextIndex;
return value;
@@ -895,7 +894,7 @@
expectedModCount++;
}
- public void set(Object obj) {
+ public void set(E obj) {
checkModCount();
if (current == null) {
throw new IllegalStateException();
@@ -903,7 +902,7 @@
current.setValue(obj);
}
- public void add(Object obj) {
+ public void add(E obj) {
checkModCount();
parent.add(nextIndex, obj);
current = null;