org.apache.commons.collections.set

Class ListOrderedSet

Implemented Interfaces:
Collection, Serializable, Set

public class ListOrderedSet
extends AbstractSerializableSetDecorator
implements Set

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.

Version:
$Revision: 1.9 $ $Date: 2004/06/07 21:42:12 $
Authors:
Stephen Colebourne
Henning P. Schmiedehausen
Since:
Commons Collections 3.0

Field Summary

protected List
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.
ListOrderedSet(Set set)
Constructor that wraps (not copies).
ListOrderedSet(Set set, List list)
Constructor that wraps (not copies) the Set and specifies the list to use.

Method Summary

boolean
add(Object object)
void
add(int index, Object object)
boolean
addAll(Collection coll)
boolean
addAll(int index, Collection coll)
List
asList()
Gets an unmodifiable view of the order of the Set.
void
clear()
static ListOrderedSet
decorate(List list)
Factory method to create an ordered set using the supplied list to retain order.
static ListOrderedSet
decorate(Set set)
Factory method to create an ordered set.
static ListOrderedSet
decorate(Set set, List list)
Factory method to create an ordered set specifying the list and set to use.
Object
get(int index)
int
indexOf(Object object)
Iterator
iterator()
boolean
remove(Object object)
Object
remove(int index)
boolean
removeAll(Collection coll)
boolean
retainAll(Collection coll)
Object[]
toArray()
Object[]
toArray(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

add, addAll, clear, contains, containsAll, equals, getCollection, hashCode, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray, toString

Field Details

setOrder

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

Constructor Details

ListOrderedSet

public ListOrderedSet()
Constructs a new empty ListOrderedSet using a HashSet and an ArrayList internally.
Since:
Commons Collections 3.1

ListOrderedSet

protected ListOrderedSet(Set set)
Constructor that wraps (not copies).
Parameters:
set - the set to decorate, must not be null

ListOrderedSet

protected ListOrderedSet(Set set,
                         List 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

Method Details

add

public boolean add(Object object)
Overrides:
add in interface AbstractCollectionDecorator

add

public void add(int index,
                Object object)

addAll

public boolean addAll(Collection coll)
Overrides:
addAll in interface AbstractCollectionDecorator

addAll

public boolean addAll(int index,
                      Collection coll)

asList

public List asList()
Gets an unmodifiable view of the order of the Set.
Returns:
an unmodifiable list view

clear

public void clear()
Overrides:
clear in interface AbstractCollectionDecorator

decorate

public static ListOrderedSet decorate(List 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

decorate

public static ListOrderedSet decorate(Set 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

decorate

public static ListOrderedSet decorate(Set set,
                                      List 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
Since:
Commons Collections 3.1

get

public Object get(int index)

indexOf

public int indexOf(Object object)

iterator

public Iterator iterator()
Overrides:
iterator in interface AbstractCollectionDecorator

remove

public boolean remove(Object object)
Overrides:
remove in interface AbstractCollectionDecorator

remove

public Object remove(int index)

removeAll

public boolean removeAll(Collection coll)
Overrides:
removeAll in interface AbstractCollectionDecorator

retainAll

public boolean retainAll(Collection coll)
Overrides:
retainAll in interface AbstractCollectionDecorator

toArray

public Object[] toArray()
Overrides:
toArray in interface AbstractCollectionDecorator

toArray

public Object[] toArray(a[] )

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 interface AbstractCollectionDecorator

Copyright © 2001-2006 Apache Software Foundation. All Rights Reserved.