net.sf.morph.util
Class ListOrderedSet

java.lang.Object
  extended by net.sf.morph.util.ListOrderedSet
All Implemented Interfaces:
java.lang.Iterable, java.util.Collection, java.util.Set

public class ListOrderedSet
extends java.lang.Object
implements java.util.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.

Note: this class was copied from Commons Collections.

Since:
Commons Collections 3.0
Version:
$Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
Author:
Stephen Colebourne, Henning P. Schmiedehausen

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

Field Detail

collection

protected java.util.Collection collection
The collection being decorated


setOrder

protected final java.util.List 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(java.util.Set set)
Constructor that wraps (not copies).

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

ListOrderedSet

protected ListOrderedSet(java.util.Set set,
                         java.util.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
Throws:
java.lang.IllegalArgumentException - if set or list is null
Method Detail

decorate

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

The list and set must both be empty.

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

decorate

public static ListOrderedSet decorate(java.util.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
Throws:
java.lang.IllegalArgumentException - if set is null

decorate

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

A HashSet is used for the set behaviour.

NOTE: If the list contains duplicates, the duplicates are removed, altering the specified list.

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

asList

public java.util.List 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 java.util.Collection
Specified by:
clear in interface java.util.Set

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.Set

add

public boolean add(java.lang.Object object)
Specified by:
add in interface java.util.Collection
Specified by:
add in interface java.util.Set

addAll

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

remove

public boolean remove(java.lang.Object object)
Specified by:
remove in interface java.util.Collection
Specified by:
remove in interface java.util.Set

removeAll

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

retainAll

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

toArray

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

toArray

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

get

public java.lang.Object get(int index)

indexOf

public int indexOf(java.lang.Object object)

add

public void add(int index,
                java.lang.Object object)

addAll

public boolean addAll(int index,
                      java.util.Collection coll)

remove

public java.lang.Object remove(int index)

toString

public java.lang.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 java.lang.Object

contains

public boolean contains(java.lang.Object o)
Specified by:
contains in interface java.util.Collection
Specified by:
contains in interface java.util.Set

containsAll

public boolean containsAll(java.util.Collection c)
Specified by:
containsAll in interface java.util.Collection
Specified by:
containsAll in interface java.util.Set

isEmpty

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

size

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


Copyright © 2004-2008.