org.opengts.util
Class OrderedSet<K>

java.lang.Object
  extended by org.opengts.util.OrderedSet<K>
All Implemented Interfaces:
java.lang.Cloneable, java.lang.Iterable<K>, java.util.Collection<K>, java.util.List<K>, java.util.Set<K>

public class OrderedSet<K>
extends java.lang.Object
implements java.util.Set<K>, java.util.List<K>, java.lang.Cloneable

OrderedSet provides a Set where values can also be retrieved in the order they were added


Nested Class Summary
static interface OrderedSet.ChangeListener
          ChangeListener interface
static class OrderedSet.ChangeListenerAdapter
          ChangeListener adapter
 
Field Summary
protected static int ENTRY_ADDED
           
protected static int ENTRY_REMOVED
           
 
Constructor Summary
OrderedSet()
          Constructor
OrderedSet(boolean retainOriginalValue)
          Construtor
OrderedSet(java.util.Collection<? extends K> c)
          Construtor
OrderedSet(java.util.Collection<? extends K> c, boolean retainOriginalValue)
          Construtor
OrderedSet(K[] a)
          Construtor
OrderedSet(K[] a, boolean retainOriginalValue)
          Construtor
OrderedSet(OrderedSet<K> os)
          Copy Construtor
 
Method Summary
protected  void _add(int ndx, K obj)
          Adds the Object at the specified index
protected  boolean _remove(java.lang.Object obj)
          Removes the specified object from this set
protected  boolean _remove(java.lang.Object obj, java.util.Iterator i)
          Removes the specified object referenced by the specified iterator from this set
 void add(int ndx, K obj)
          Adds the Object at the specified index
 boolean add(K obj)
          Adds the Object to the end of the list
 boolean addAll(java.util.Collection<? extends K> c)
          Adds all Objects in the specified Collection to this set
 boolean addAll(int ndx, java.util.Collection<? extends K> c)
          Throws an UnsupportedOperationException
 boolean addAll(K[] a)
          Adds all Objects in the specified array to this set
 void addChangeListener(OrderedSet.ChangeListener cl)
          Adds a change listener to this OrderedSet
 void clear()
          Clears all Objects from this set
 java.lang.Object clone()
          Returns a clone of this OrderedSet
 boolean contains(java.lang.Object obj)
          Returns true if this set contains the specified Object
 boolean containsAll(java.util.Collection<?> c)
          Returns true if this set contains all items specified in the Collection
 boolean equals(java.lang.Object other)
          Returns true if this set is equivalent to the specified Object
 K get(int ndx)
          Gets the Objects at the specified index
protected  java.util.List<K> getBackingList()
          Gets the ordered backing list for this OrderedSet
protected  java.util.List<OrderedSet.ChangeListener> getChangeListeners(boolean create)
          Gets a list of change listeners
protected  java.util.List<K> getList()
          Gets the ordered backing list for this OrderedSet.
 boolean getRetainOriginalValue()
          Returns true if original added entries should be retained if a duplicate entry is subsequently added.
protected  boolean hasChangeListeners()
          Returns true if any change listeners have been registered
 int hashCode()
          Returns the hashcode for this set
 int indexOf(java.lang.Object obj)
          Returns the index of the specified Object in this set
 boolean isEmpty()
          Returns true if this set is empty
 java.util.Iterator<K> iterator()
          Returns an ordered Iterator over the elements in this set.
 int lastIndexOf(java.lang.Object obj)
          Returns the last index of the specified Object in this set.
 java.util.ListIterator<K> listIterator()
          Returns a ListIterator over this set
 java.util.ListIterator<K> listIterator(int ndx)
          Returns a ListIterator over this set.
protected  void notifyChangeListeners(int action, java.lang.Object obj)
          Notifies all change listeners of a change to this OrderedSet
 void printContents()
          Prints the contents of this set (for debug/testing purposes)
 K remove(int ndx)
          Throws UnsupportedOperationException
 boolean remove(java.lang.Object obj)
          Removes the specified object from this set
 boolean removeAll(java.util.Collection<?> c)
          Remove all Objects contained in the specified Collection from this set
 void removeChangeListener(OrderedSet.ChangeListener cl)
          Removes a change listener to this OrderedSet
 boolean retainAll(java.util.Collection<?> c)
          Removes all Object from this set that are not reference in the specified Collection
 K set(int ndx, K obj)
          Throws an UnsupportedOperationException
 void setRetainOriginalValue(boolean state)
          Sets the retain-original state for this OrderedSet if duplicate entries are added.
 int size()
          Returns the number of elements in this set
 java.util.List<K> subList(int fromIndex, int toIndex)
          Thows UnsupportedOperationException
 java.lang.Object[] toArray()
          Returns an array of Object elements in this set
<K> K[]
toArray(K[] a)
          Returns an array of Object elements in this set
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ENTRY_ADDED

protected static final int ENTRY_ADDED
See Also:
Constant Field Values

ENTRY_REMOVED

protected static final int ENTRY_REMOVED
See Also:
Constant Field Values
Constructor Detail

OrderedSet

public OrderedSet()
Constructor


OrderedSet

public OrderedSet(boolean retainOriginalValue)
Construtor

Parameters:
retainOriginalValue - True to ignore duplicate entries, false overwrite existing entries with any newly added duplicates.

OrderedSet

public OrderedSet(java.util.Collection<? extends K> c,
                  boolean retainOriginalValue)
Construtor

Parameters:
c - Collection of Objects used to initialize this set.
retainOriginalValue - True to ignore duplicate entries, false overwrite existing entries with any newly added duplicates.

OrderedSet

public OrderedSet(java.util.Collection<? extends K> c)
Construtor

Parameters:
c - Collection of Objects used to initialize this set.

OrderedSet

public OrderedSet(K[] a,
                  boolean retainOriginalValue)
Construtor

Parameters:
a - Array of Objects used to initialize this set.
retainOriginalValue - True to ignore duplicate entries, false overwrite existing entries with any newly added duplicates.

OrderedSet

public OrderedSet(K[] a)
Construtor

Parameters:
a - Array of Objects used to initialize this set.

OrderedSet

public OrderedSet(OrderedSet<K> os)
Copy Construtor

Parameters:
os - Other OrderedSet used to initialize this set
Method Detail

clone

public java.lang.Object clone()
Returns a clone of this OrderedSet

Overrides:
clone in class java.lang.Object
Returns:
The cloned OrderedSet

getChangeListeners

protected java.util.List<OrderedSet.ChangeListener> getChangeListeners(boolean create)
Gets a list of change listeners

Parameters:
create - True to create an empty list if no change listeners have been added
Returns:
The list of change listeners

hasChangeListeners

protected boolean hasChangeListeners()
Returns true if any change listeners have been registered

Returns:
True if any change listeners have been registered

addChangeListener

public void addChangeListener(OrderedSet.ChangeListener cl)
Adds a change listener to this OrderedSet

Parameters:
cl - The change listener to add

removeChangeListener

public void removeChangeListener(OrderedSet.ChangeListener cl)
Removes a change listener to this OrderedSet

Parameters:
cl - The change listener to remove

notifyChangeListeners

protected void notifyChangeListeners(int action,
                                     java.lang.Object obj)
Notifies all change listeners of a change to this OrderedSet

Parameters:
action - The change action
obj - The Object changed

getBackingList

protected java.util.List<K> getBackingList()
Gets the ordered backing list for this OrderedSet

Returns:
The backing list instance

getList

protected java.util.List<K> getList()
Gets the ordered backing list for this OrderedSet. Warning: This method does not protect against having the set contain more than one instance of the same item!!!

Returns:
The backing list instance

getRetainOriginalValue

public boolean getRetainOriginalValue()
Returns true if original added entries should be retained if a duplicate entry is subsequently added.

Returns:
True if original added entries should be retained

setRetainOriginalValue

public void setRetainOriginalValue(boolean state)
Sets the retain-original state for this OrderedSet if duplicate entries are added.

Parameters:
state - True to retain original added entries

get

public K get(int ndx)
Gets the Objects at the specified index

Specified by:
get in interface java.util.List<K>
Parameters:
ndx - The index
Returns:
The object at the specified index

set

public K set(int ndx,
             K obj)
Throws an UnsupportedOperationException

Specified by:
set in interface java.util.List<K>
Parameters:
ndx - The index
obj - The Object to set
Returns:
The previous Object
Throws:
java.lang.UnsupportedOperationException - always

_add

protected void _add(int ndx,
                    K obj)
Adds the Object at the specified index

Parameters:
ndx - The index
obj - The Object to add

add

public boolean add(K obj)
Adds the Object to the end of the list

Specified by:
add in interface java.util.Collection<K>
Specified by:
add in interface java.util.List<K>
Specified by:
add in interface java.util.Set<K>
Parameters:
obj - The Object to add

addAll

public boolean addAll(java.util.Collection<? extends K> c)
Adds all Objects in the specified Collection to this set

Specified by:
addAll in interface java.util.Collection<K>
Specified by:
addAll in interface java.util.List<K>
Specified by:
addAll in interface java.util.Set<K>
Parameters:
c - The Collection
Returns:
True if any items were added to this list

addAll

public boolean addAll(K[] a)
Adds all Objects in the specified array to this set

Parameters:
a - The array
Returns:
True if any items were added to this list

add

public void add(int ndx,
                K obj)
Adds the Object at the specified index

Specified by:
add in interface java.util.List<K>
Parameters:
ndx - The index
obj - The Object to add

addAll

public boolean addAll(int ndx,
                      java.util.Collection<? extends K> c)
Throws an UnsupportedOperationException

Specified by:
addAll in interface java.util.List<K>
Parameters:
ndx - The index
c - The Collection
Returns:
True if this set was changed
Throws:
java.lang.UnsupportedOperationException - always

contains

public boolean contains(java.lang.Object obj)
Returns true if this set contains the specified Object

Specified by:
contains in interface java.util.Collection<K>
Specified by:
contains in interface java.util.List<K>
Specified by:
contains in interface java.util.Set<K>
Returns:
True if this set contains the specified Object

containsAll

public boolean containsAll(java.util.Collection<?> c)
Returns true if this set contains all items specified in the Collection

Specified by:
containsAll in interface java.util.Collection<K>
Specified by:
containsAll in interface java.util.List<K>
Specified by:
containsAll in interface java.util.Set<K>
Returns:
True if this set contains all items specified in the Collection

equals

public boolean equals(java.lang.Object other)
Returns true if this set is equivalent to the specified Object

Specified by:
equals in interface java.util.Collection<K>
Specified by:
equals in interface java.util.List<K>
Specified by:
equals in interface java.util.Set<K>
Overrides:
equals in class java.lang.Object
Parameters:
other - The other Object
Returns:
True if this set is equivalent to the specified Object

hashCode

public int hashCode()
Returns the hashcode for this set

Specified by:
hashCode in interface java.util.Collection<K>
Specified by:
hashCode in interface java.util.List<K>
Specified by:
hashCode in interface java.util.Set<K>
Overrides:
hashCode in class java.lang.Object
Returns:
The hashcode for this set

_remove

protected boolean _remove(java.lang.Object obj)
Removes the specified object from this set

Parameters:
obj - The Object to remove
Returns:
True if the object was remove, false if the object didn't exist in this set

_remove

protected boolean _remove(java.lang.Object obj,
                          java.util.Iterator i)
Removes the specified object referenced by the specified iterator from this set

Parameters:
obj - The Object to remove
i - The Iterator which references the Objet to remove
Returns:
True

remove

public K remove(int ndx)
Throws UnsupportedOperationException

Specified by:
remove in interface java.util.List<K>
Parameters:
ndx - The object index to remove
Returns:
The removed Object
Throws:
java.lang.UnsupportedOperationException - always

remove

public boolean remove(java.lang.Object obj)
Removes the specified object from this set

Specified by:
remove in interface java.util.Collection<K>
Specified by:
remove in interface java.util.List<K>
Specified by:
remove in interface java.util.Set<K>
Parameters:
obj - THe Object to remove
Returns:
True if the object was removed

removeAll

public boolean removeAll(java.util.Collection<?> c)
Remove all Objects contained in the specified Collection from this set

Specified by:
removeAll in interface java.util.Collection<K>
Specified by:
removeAll in interface java.util.List<K>
Specified by:
removeAll in interface java.util.Set<K>
Parameters:
c - The Collection containing the list of Objects to remove from this set
Returns:
True if elements were removed from this set

retainAll

public boolean retainAll(java.util.Collection<?> c)
Removes all Object from this set that are not reference in the specified Collection

Specified by:
retainAll in interface java.util.Collection<K>
Specified by:
retainAll in interface java.util.List<K>
Specified by:
retainAll in interface java.util.Set<K>
Parameters:
c - The Collection of Objects to keep
Returns:
True if any Objects were removed from this list

clear

public void clear()
Clears all Objects from this set

Specified by:
clear in interface java.util.Collection<K>
Specified by:
clear in interface java.util.List<K>
Specified by:
clear in interface java.util.Set<K>

size

public int size()
Returns the number of elements in this set

Specified by:
size in interface java.util.Collection<K>
Specified by:
size in interface java.util.List<K>
Specified by:
size in interface java.util.Set<K>
Returns:
The number of elements in this set

isEmpty

public boolean isEmpty()
Returns true if this set is empty

Specified by:
isEmpty in interface java.util.Collection<K>
Specified by:
isEmpty in interface java.util.List<K>
Specified by:
isEmpty in interface java.util.Set<K>
Returns:
True if this set is empty

indexOf

public int indexOf(java.lang.Object obj)
Returns the index of the specified Object in this set

Specified by:
indexOf in interface java.util.List<K>
Parameters:
obj - The Object for which the index is returned
Returns:
The index of the specified Object, or -1 if the Object does not exist in this set.

lastIndexOf

public int lastIndexOf(java.lang.Object obj)
Returns the last index of the specified Object in this set. Since this is a 'Set', any value exists at-most once, this is essentially the same as calling 'indexOf(obj)'.

Specified by:
lastIndexOf in interface java.util.List<K>
Parameters:
obj - The Object for which the index is returned
Returns:
The last index of the specified Object, or -1 if the Object does not exist in this set.

iterator

public java.util.Iterator<K> iterator()
Returns an ordered Iterator over the elements in this set.

Specified by:
iterator in interface java.lang.Iterable<K>
Specified by:
iterator in interface java.util.Collection<K>
Specified by:
iterator in interface java.util.List<K>
Specified by:
iterator in interface java.util.Set<K>
Returns:
An Iterator over the elements in this set.

listIterator

public java.util.ListIterator<K> listIterator()
Returns a ListIterator over this set

Specified by:
listIterator in interface java.util.List<K>
Returns:
The ListIterator

listIterator

public java.util.ListIterator<K> listIterator(int ndx)
Returns a ListIterator over this set.

Specified by:
listIterator in interface java.util.List<K>
Parameters:
ndx - The starting index.
Returns:
The ListIterator

subList

public java.util.List<K> subList(int fromIndex,
                                 int toIndex)
Thows UnsupportedOperationException

Specified by:
subList in interface java.util.List<K>
Parameters:
fromIndex - The 'from' index.
toIndex - The 'to' index.
Throws:
java.lang.UnsupportedOperationException - always

toArray

public java.lang.Object[] toArray()
Returns an array of Object elements in this set

Specified by:
toArray in interface java.util.Collection<K>
Specified by:
toArray in interface java.util.List<K>
Specified by:
toArray in interface java.util.Set<K>
Returns:
An array of Object elements in this set

toArray

public <K> K[] toArray(K[] a)
Returns an array of Object elements in this set

Specified by:
toArray in interface java.util.Collection<K>
Specified by:
toArray in interface java.util.List<K>
Specified by:
toArray in interface java.util.Set<K>
Parameters:
a - The array into which the elements are copied
Returns:
An array of Object elements in this set

printContents

public void printContents()
Prints the contents of this set (for debug/testing purposes)