org.pf.util
Class OrderedSet

java.lang.Object
  extended by org.pf.util.OrderedSet
All Implemented Interfaces:
java.lang.Iterable, java.util.Collection, java.util.List, java.util.Set

public class OrderedSet
extends java.lang.Object
implements java.util.Set, java.util.List

Implements a Set that keeps the order of its elements according to when they were added. Additionally it implements the List interface. So instances of this class can either be used as Set or as List.

Version:
1.2
Author:
Manfred Duchrow

Constructor Summary
OrderedSet()
          Initialize the new instance with default values.
OrderedSet(int initialCapacity)
          Initialize the new set with an initial capacity.
 
Method Summary
 void add(int index, java.lang.Object element)
          Inserts the specified element at the specified position in this list
 boolean add(java.lang.Object obj)
           
 boolean addAll(java.util.Collection coll)
           
 boolean addAll(int index, java.util.Collection c)
          Inserts all of the elements in the specified collection into this list at the specified position if they are not yet in this list.
 void clear()
           
 boolean contains(java.lang.Object obj)
           
 boolean containsAll(java.util.Collection coll)
           
 java.lang.Object get(int index)
          Returns the element at the specified position in this list.
 int indexOf(java.lang.Object o)
          Returns the index in this list of the first occurrence of the specified element, or -1 if this list does not contain this element.
 boolean isEmpty()
           
 java.util.Iterator iterator()
           
 int lastIndexOf(java.lang.Object o)
          Returns the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element.
 java.util.ListIterator listIterator()
          Returns a list iterator of the elements in this list (in proper sequence).
 java.util.ListIterator listIterator(int index)
          Returns a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list.
 java.lang.Object remove(int index)
          Returns the object at the specified index
 boolean remove(java.lang.Object obj)
           
 boolean removeAll(java.util.Collection coll)
           
 boolean retainAll(java.util.Collection coll)
           
 java.lang.Object set(int index, java.lang.Object element)
          Sets the given element at the specified index if the element is not yet already in the collection!
 int size()
           
 java.util.List subList(int fromIndex, int toIndex)
          Returns a sub list containing the elements in the range of the defined indices.
 java.lang.Object[] toArray()
           
 java.lang.Object[] toArray(java.lang.Object[] array)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Set
equals, hashCode
 
Methods inherited from interface java.util.List
equals, hashCode
 

Constructor Detail

OrderedSet

public OrderedSet()
Initialize the new instance with default values.


OrderedSet

public OrderedSet(int initialCapacity)
Initialize the new set with an initial capacity.

Method Detail

add

public boolean add(java.lang.Object obj)
Specified by:
add in interface java.util.Collection
Specified by:
add in interface java.util.List
Specified by:
add in interface java.util.Set
See Also:
Collection.add(java.lang.Object)

addAll

public boolean addAll(java.util.Collection coll)
Specified by:
addAll in interface java.util.Collection
Specified by:
addAll in interface java.util.List
Specified by:
addAll in interface java.util.Set
See Also:
Collection.addAll(java.util.Collection)

clear

public void clear()
Specified by:
clear in interface java.util.Collection
Specified by:
clear in interface java.util.List
Specified by:
clear in interface java.util.Set
See Also:
Collection.clear()

contains

public boolean contains(java.lang.Object obj)
Specified by:
contains in interface java.util.Collection
Specified by:
contains in interface java.util.List
Specified by:
contains in interface java.util.Set
See Also:
Collection.contains(java.lang.Object)

containsAll

public boolean containsAll(java.util.Collection coll)
Specified by:
containsAll in interface java.util.Collection
Specified by:
containsAll in interface java.util.List
Specified by:
containsAll in interface java.util.Set
See Also:
Collection.containsAll(java.util.Collection)

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface java.util.Collection
Specified by:
isEmpty in interface java.util.List
Specified by:
isEmpty in interface java.util.Set
See Also:
Collection.isEmpty()

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
See Also:
Collection.iterator()

remove

public boolean remove(java.lang.Object obj)
Specified by:
remove in interface java.util.Collection
Specified by:
remove in interface java.util.List
Specified by:
remove in interface java.util.Set
See Also:
Collection.remove(java.lang.Object)

removeAll

public boolean removeAll(java.util.Collection coll)
Specified by:
removeAll in interface java.util.Collection
Specified by:
removeAll in interface java.util.List
Specified by:
removeAll in interface java.util.Set
See Also:
Collection.removeAll(java.util.Collection)

retainAll

public boolean retainAll(java.util.Collection coll)
Specified by:
retainAll in interface java.util.Collection
Specified by:
retainAll in interface java.util.List
Specified by:
retainAll in interface java.util.Set
See Also:
Collection.retainAll(java.util.Collection)

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
See Also:
Collection.size()

toArray

public java.lang.Object[] toArray()
Specified by:
toArray in interface java.util.Collection
Specified by:
toArray in interface java.util.List
Specified by:
toArray in interface java.util.Set
See Also:
Collection.toArray()

toArray

public java.lang.Object[] toArray(java.lang.Object[] array)
Specified by:
toArray in interface java.util.Collection
Specified by:
toArray in interface java.util.List
Specified by:
toArray in interface java.util.Set
See Also:
Collection.toArray(java.lang.Object[])

add

public void add(int index,
                java.lang.Object element)
Inserts the specified element at the specified position in this list

Specified by:
add in interface java.util.List

addAll

public boolean addAll(int index,
                      java.util.Collection c)
Inserts all of the elements in the specified collection into this list at the specified position if they are not yet in this list.

Specified by:
addAll in interface java.util.List
Returns:
true if this list changed as a result of the call.

get

public java.lang.Object get(int index)
Returns the element at the specified position in this list.

Specified by:
get in interface java.util.List

indexOf

public int indexOf(java.lang.Object o)
Returns the index in this list of the first occurrence of the specified element, or -1 if this list does not contain this element.

Specified by:
indexOf in interface java.util.List

lastIndexOf

public int lastIndexOf(java.lang.Object o)
Returns the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element.

Specified by:
lastIndexOf in interface java.util.List

listIterator

public java.util.ListIterator listIterator()
Returns a list iterator of the elements in this list (in proper sequence).

Specified by:
listIterator in interface java.util.List

listIterator

public java.util.ListIterator listIterator(int index)
Returns a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list.

Specified by:
listIterator in interface java.util.List

remove

public java.lang.Object remove(int index)
Returns the object at the specified index

Specified by:
remove in interface java.util.List

set

public java.lang.Object set(int index,
                            java.lang.Object element)
Sets the given element at the specified index if the element is not yet already in the collection!

Specified by:
set in interface java.util.List

subList

public java.util.List subList(int fromIndex,
                              int toIndex)
Returns a sub list containing the elements in the range of the defined indices. The result is also an OrderedSet. Contrary to the definition of this method in the List interface here the result list is NOT backed by this list. Tah means, any changes to the result list do not have any impact on this list!

Specified by:
subList in interface java.util.List