org.apache.commons.collections.set
Class ListOrderedSet<E>

java.lang.Object
  extended by org.apache.commons.collections.collection.AbstractCollectionDecorator<E>
      extended by org.apache.commons.collections.set.AbstractSetDecorator<E>
          extended by org.apache.commons.collections.set.AbstractSerializableSetDecorator<E>
              extended by org.apache.commons.collections.set.ListOrderedSet<E>
All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>, Set<E>

public class ListOrderedSet<E>
extends AbstractSerializableSetDecorator<E>
implements Set<E>

Decorates another Set to ensure that the order of addition is retained and used by the iterator.

If an object is added to the set for a second time, it will remain in the original position in the iteration. The order can be observed from the set via the iterator or toArray methods.

The ListOrderedSet also has various useful direct methods. These include many from List, such as get(int), remove(int) and indexOf(int). An unmodifiable List view of the set can be obtained via asList().

This class cannot implement the List interface directly as various interface methods (notably equals/hashCode) are incompatable with a set.

This class is Serializable from Commons Collections 3.1.

Since:
Commons Collections 3.0
Version:
$Revision: 1.1.1.1 $ $Date: 2005/05/23 04:36:20 $
Author:
Matt Hall, John Watkinson, Stephen Colebourne, Henning P. Schmiedehausen
See Also:
Serialized Form

Field Summary
protected  List<E> setOrder
          Internal list to hold the sequence of objects
 
Fields inherited from class org.apache.commons.collections.collection.AbstractCollectionDecorator
collection
 
Constructor Summary
  ListOrderedSet()
          Constructs a new empty ListOrderedSet using a HashSet and an ArrayList internally.
protected ListOrderedSet(Set<E> set)
          Constructor that wraps (not copies).
protected ListOrderedSet(Set<E> set, List<E> list)
          Constructor that wraps (not copies) the Set and specifies the list to use.
 
Method Summary
 boolean add(E object)
           
 void add(int index, E object)
           
 boolean addAll(Collection<? extends E> coll)
           
 boolean addAll(int index, Collection<? extends E> coll)
           
 List<E> asList()
          Gets an unmodifiable view of the order of the Set.
 void clear()
           
static
<E> ListOrderedSet<E>
decorate(List<E> list)
          Factory method to create an ordered set using the supplied list to retain order.
static
<E> ListOrderedSet<E>
decorate(Set<E> set)
          Factory method to create an ordered set.
static
<E> ListOrderedSet<E>
decorate(Set<E> set, List<E> list)
          Factory method to create an ordered set specifying the list and set to use.
 E get(int index)
           
 int indexOf(E object)
           
 Iterator<E> iterator()
           
 E remove(int index)
           
 boolean remove(Object object)
           
 boolean removeAll(Collection<?> coll)
           
 boolean retainAll(Collection<?> coll)
           
 Object[] toArray()
           
<T> T[]
toArray(T[] a)
           
 String toString()
          Uses the underlying List's toString so that order is achieved.
 
Methods inherited from class org.apache.commons.collections.set.AbstractSetDecorator
getSet
 
Methods inherited from class org.apache.commons.collections.collection.AbstractCollectionDecorator
contains, containsAll, equals, getCollection, hashCode, isEmpty, size
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
contains, containsAll, equals, hashCode, isEmpty, size
 

Field Detail

setOrder

protected final List<E> setOrder
Internal list to hold the sequence of objects

Constructor Detail

ListOrderedSet

public ListOrderedSet()
Constructs a new empty ListOrderedSet using a HashSet and an ArrayList internally.

Since:
Commons Collections 3.1

ListOrderedSet

protected ListOrderedSet(Set<E> set)
Constructor that wraps (not copies).

Parameters:
set - the set to decorate, must not be null
Throws:
IllegalArgumentException - if set is null

ListOrderedSet

protected ListOrderedSet(Set<E> set,
                         List<E> list)
Constructor that wraps (not copies) the Set and specifies the list to use.

The set and list must both be correctly initialised to the same elements.

Parameters:
set - the set to decorate, must not be null
list - the list to decorate, must not be null
Throws:
IllegalArgumentException - if set or list is null
Method Detail

decorate

public static <E> ListOrderedSet<E> decorate(Set<E> set,
                                             List<E> list)
Factory method to create an ordered set specifying the list and set to use.

Parameters:
set - the set to decorate, must be empty and not null
list - the list to decorate, must be empty and not null
Throws:
IllegalArgumentException - if set or list is null
IllegalArgumentException - if either the set or list is not empty
Since:
Commons Collections 3.1

decorate

public static <E> ListOrderedSet<E> decorate(Set<E> set)
Factory method to create an ordered set.

An ArrayList is used to retain order.

Parameters:
set - the set to decorate, must not be null
Throws:
IllegalArgumentException - if set is null

decorate

public static <E> ListOrderedSet<E> decorate(List<E> list)
Factory method to create an ordered set using the supplied list to retain order.

A HashSet is used for the set behaviour.

Parameters:
list - the list to decorate, must not be null
Throws:
IllegalArgumentException - if list is null

asList

public List<E> asList()
Gets an unmodifiable view of the order of the Set.

Returns:
an unmodifiable list view

clear

public void clear()
Specified by:
clear in interface Collection<E>
Specified by:
clear in interface Set<E>
Overrides:
clear in class AbstractCollectionDecorator<E>

iterator

public Iterator<E> iterator()
Specified by:
iterator in interface Iterable<E>
Specified by:
iterator in interface Collection<E>
Specified by:
iterator in interface Set<E>
Overrides:
iterator in class AbstractCollectionDecorator<E>

add

public boolean add(E object)
Specified by:
add in interface Collection<E>
Specified by:
add in interface Set<E>
Overrides:
add in class AbstractCollectionDecorator<E>

addAll

public boolean addAll(Collection<? extends E> coll)
Specified by:
addAll in interface Collection<E>
Specified by:
addAll in interface Set<E>
Overrides:
addAll in class AbstractCollectionDecorator<E>

remove

public boolean remove(Object object)
Specified by:
remove in interface Collection<E>
Specified by:
remove in interface Set<E>
Overrides:
remove in class AbstractCollectionDecorator<E>

removeAll

public boolean removeAll(Collection<?> coll)
Specified by:
removeAll in interface Collection<E>
Specified by:
removeAll in interface Set<E>
Overrides:
removeAll in class AbstractCollectionDecorator<E>

retainAll

public boolean retainAll(Collection<?> coll)
Specified by:
retainAll in interface Collection<E>
Specified by:
retainAll in interface Set<E>
Overrides:
retainAll in class AbstractCollectionDecorator<E>

toArray

public Object[] toArray()
Specified by:
toArray in interface Collection<E>
Specified by:
toArray in interface Set<E>
Overrides:
toArray in class AbstractCollectionDecorator<E>

toArray

public <T> T[] toArray(T[] a)
Specified by:
toArray in interface Collection<E>
Specified by:
toArray in interface Set<E>
Overrides:
toArray in class AbstractCollectionDecorator<E>

get

public E get(int index)

indexOf

public int indexOf(E object)

add

public void add(int index,
                E object)

addAll

public boolean addAll(int index,
                      Collection<? extends E> coll)

remove

public E remove(int index)

toString

public String toString()
Uses the underlying List's toString so that order is achieved. This means that the decorated Set's toString is not used, so any custom toStrings will be ignored.

Overrides:
toString in class AbstractCollectionDecorator<E>


Copyright © 2005-2005 Apache Software Foundation, Matt Hall, John Watkinson. All Rights Reserved.