com.taco.data
Class ListSet

java.lang.Object
  extended by java.util.AbstractCollection
      extended by java.util.AbstractSet
          extended by com.taco.data.ListSet
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Iterable, java.util.Collection, java.util.List, java.util.Set

public class ListSet
extends java.util.AbstractSet
implements java.util.List, java.lang.Cloneable, java.io.Serializable

A list of objects presented as a set. Unlike a set, its elements are random-accessible. Attempts to add a duplicate (according to the equals() method of the object to be added) of an object already in the list will have no effect, except possibly to change the order in the list. Null objects may NOT be added to this collection.

See Also:
Serialized Form

Field Summary
protected  java.util.LinkedList _list
          The list that backs this collection.
static ICollectionFactory LIST_SET_FACTORY
          A factory that creates instances of ListSet.
 
Constructor Summary
ListSet()
          Create an initially empty instance.
ListSet(java.util.Collection collection)
          Copy the elements of the argument collection into a new list that will back this instance.
ListSet(java.util.LinkedList list)
          Use the argument list to back this instance.
 
Method Summary
 void add(int index, java.lang.Object obj)
          This implementation throws an UnsupportedOperationException if an equivalent element already exists.
 boolean add(java.lang.Object obj)
          Add obj to the end of the list.
 boolean add(java.lang.Object obj, boolean replace)
          Add obj to the end of the list.
 boolean addAll(java.util.Collection c)
          Add all the elements to this list in the order returned by the iterator of the argument collection.
 boolean addAll(int index, java.util.Collection c)
          If any of the elements exist in the collection already, keep trying to elements before returning false.
 boolean addFirst(java.lang.Object obj)
          Add obj to the front of the list.
 boolean addFirst(java.lang.Object obj, boolean replace)
          Add obj to the front of the list.
 boolean addLast(java.lang.Object obj)
          Add obj to the end of the list.
 boolean addLast(java.lang.Object obj, boolean replace)
          Add obj to the end of the list.
 void clear()
          Clear all elements from the list.
 java.lang.Object clone()
          Clone this instance by cloning the list that backs this instance.
static ListSet fromArray(java.lang.Object[] array)
          Create a new instance with the elements in the argument array.
 java.lang.Object get(int i)
          Return the ith object in the list.
 int indexOf(java.lang.Object obj)
           
 java.util.Iterator iterator()
           
 int lastIndexOf(java.lang.Object obj)
           
 java.util.ListIterator listIterator()
           
 java.util.ListIterator listIterator(int i)
           
 java.lang.Object remove(int index)
           
 boolean remove(java.lang.Object obj)
          Remove obj from the list.
 boolean removeAll(java.util.Collection c)
          Remove all elements in c.
 boolean retainAll(java.util.Collection c)
          Remove all elements not in c.
 java.lang.Object set(int i, java.lang.Object obj)
          If the set does not contain an equivalent element, replace the ith element with obj and return obj.
 java.lang.Object set(int i, java.lang.Object obj, boolean replace)
           
 int size()
           
 java.util.List subList(int start, int end)
          The returned value is not a ListSet nor does it implement the Set interface!
 
Methods inherited from class java.util.AbstractSet
equals, hashCode
 
Methods inherited from class java.util.AbstractCollection
contains, containsAll, isEmpty, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
contains, containsAll, equals, hashCode, isEmpty, toArray, toArray
 
Methods inherited from interface java.util.Set
contains, containsAll, isEmpty, toArray, toArray
 

Field Detail

LIST_SET_FACTORY

public static final ICollectionFactory LIST_SET_FACTORY
A factory that creates instances of ListSet.


_list

protected java.util.LinkedList _list
The list that backs this collection.

Constructor Detail

ListSet

public ListSet()
Create an initially empty instance.


ListSet

public ListSet(java.util.LinkedList list)
Use the argument list to back this instance. This constructor does not remove duplicates!


ListSet

public ListSet(java.util.Collection collection)
Copy the elements of the argument collection into a new list that will back this instance. If an element appears more than once in the argument collection, it will only appear once in this instance.

Method Detail

clone

public java.lang.Object clone()
Clone this instance by cloning the list that backs this instance. The elements themselves are not cloned.

Overrides:
clone in class java.lang.Object

size

public int size()
Specified by:
size in interface java.util.Collection
Specified by:
size in interface java.util.List
Specified by:
size in interface java.util.Set
Specified by:
size in class java.util.AbstractCollection

get

public java.lang.Object get(int i)
Return the ith object in the list. This implementation is O(n).

Specified by:
get in interface java.util.List

set

public java.lang.Object set(int i,
                            java.lang.Object obj)
If the set does not contain an equivalent element, replace the ith element with obj and return obj. If the ith element is equivalent, return the original element without modifying the list. Otherwise throw an UnsupportedOperationException.

Specified by:
set in interface java.util.List

set

public java.lang.Object set(int i,
                            java.lang.Object obj,
                            boolean replace)

indexOf

public int indexOf(java.lang.Object obj)
Specified by:
indexOf in interface java.util.List

lastIndexOf

public int lastIndexOf(java.lang.Object obj)
Specified by:
lastIndexOf in interface java.util.List

iterator

public java.util.Iterator iterator()
Specified by:
iterator in interface java.lang.Iterable
Specified by:
iterator in interface java.util.Collection
Specified by:
iterator in interface java.util.List
Specified by:
iterator in interface java.util.Set
Specified by:
iterator in class java.util.AbstractCollection

listIterator

public java.util.ListIterator listIterator()
Specified by:
listIterator in interface java.util.List

listIterator

public java.util.ListIterator listIterator(int i)
Specified by:
listIterator in interface java.util.List

subList

public java.util.List subList(int start,
                              int end)
The returned value is not a ListSet nor does it implement the Set interface!

Specified by:
subList in interface java.util.List

addAll

public boolean addAll(java.util.Collection c)
Add all the elements to this list in the order returned by the iterator of the argument collection. If any element cannot be added, return false. However, keep trying to add elements even on a previous failure. If all the elements are added, return true.

Specified by:
addAll in interface java.util.Collection
Specified by:
addAll in interface java.util.List
Specified by:
addAll in interface java.util.Set
Overrides:
addAll in class java.util.AbstractCollection

addAll

public boolean addAll(int index,
                      java.util.Collection c)
If any of the elements exist in the collection already, keep trying to elements before returning false.

Specified by:
addAll in interface java.util.List

add

public void add(int index,
                java.lang.Object obj)
This implementation throws an UnsupportedOperationException if an equivalent element already exists.

Specified by:
add in interface java.util.List

addFirst

public boolean addFirst(java.lang.Object obj)
Add obj to the front of the list. Leave the list unchanged if an equivalent object exists in the list. Return true if the list is changed (if a duplicate of obj is not already at the front of the list). Note: null may not be added to this list! This implementation is O(n).


addFirst

public boolean addFirst(java.lang.Object obj,
                        boolean replace)
Add obj to the front of the list. If replace is true and an equivalent object exists in the list already, remove it first. Otherwise, leave the list unchanged if an equivalent object exists in the list. Return true if the list is changed (if a duplicate of obj is not already at the front of the list). Note: null may not be added to this list! This implementation is O(n).


addLast

public boolean addLast(java.lang.Object obj)
Add obj to the end of the list. Leave the list unchanged if an equivalent object exists in the list. Return true if the list is changed (if a duplicate of obj is not already at the front of the list). Note: null may not be added to this list! This implementation is O(n).


addLast

public boolean addLast(java.lang.Object obj,
                       boolean replace)
Add obj to the end of the list. If replace is true and an equivalent object exists in the list already, remove it first. Otherwise, leave the list unchanged if an equivalent object exists in the list. Return true if the list is changed (if a duplicate of obj is not already at the front of the list). Note: null may not be added to this list! This implementation is O(n).


add

public boolean add(java.lang.Object obj)
Add obj to the end of the list. If a duplicate of obj already exists in the set, don't add obj. Return true if the set is changed (if a duplicate of obj is not already in the set). Note: null may not be added to this set! This implementation is O(n).

Specified by:
add in interface java.util.Collection
Specified by:
add in interface java.util.List
Specified by:
add in interface java.util.Set
Overrides:
add in class java.util.AbstractCollection

add

public boolean add(java.lang.Object obj,
                   boolean replace)
Add obj to the end of the list. If replace is true and an equivalent object exists in the list already, remove it first. Otherwise, if a duplicate of obj already exists in the set, don't add obj. Return true if the set is changed (if a duplicate of obj is not already in the set). Note: null may not be added to this set! This implementation is O(n).


remove

public java.lang.Object remove(int index)
Specified by:
remove in interface java.util.List

remove

public boolean remove(java.lang.Object obj)
Remove obj from the list. Overridden for efficiency.

Specified by:
remove in interface java.util.Collection
Specified by:
remove in interface java.util.List
Specified by:
remove in interface java.util.Set
Overrides:
remove in class java.util.AbstractCollection

removeAll

public boolean removeAll(java.util.Collection c)
Remove all elements in c. Overridden for efficiency.

Specified by:
removeAll in interface java.util.Collection
Specified by:
removeAll in interface java.util.List
Specified by:
removeAll in interface java.util.Set
Overrides:
removeAll in class java.util.AbstractSet

retainAll

public boolean retainAll(java.util.Collection c)
Remove all elements not in c. Overridden for efficiency.

Specified by:
retainAll in interface java.util.Collection
Specified by:
retainAll in interface java.util.List
Specified by:
retainAll in interface java.util.Set
Overrides:
retainAll in class java.util.AbstractCollection

clear

public void clear()
Clear all elements from the list. Overridden for efficiency.

Specified by:
clear in interface java.util.Collection
Specified by:
clear in interface java.util.List
Specified by:
clear in interface java.util.Set
Overrides:
clear in class java.util.AbstractCollection

fromArray

public static ListSet fromArray(java.lang.Object[] array)
Create a new instance with the elements in the argument array.