org.apache.commons.collections.list
Class SetUniqueList

java.lang.Object
  extended by org.apache.commons.collections.collection.AbstractCollectionDecorator
      extended by org.apache.commons.collections.list.AbstractListDecorator
          extended by org.apache.commons.collections.list.AbstractSerializableListDecorator
              extended by org.apache.commons.collections.list.SetUniqueList
All Implemented Interfaces:
java.io.Serializable, java.lang.Iterable, java.util.Collection, java.util.List

public class SetUniqueList
extends AbstractSerializableListDecorator

Decorates a List to ensure that no duplicates are present much like a Set.

The List interface makes certain assumptions/requirements. This implementation breaks these in certain ways, but this is merely the result of rejecting duplicates. Each violation is explained in the method, but it should not affect you. Bear in mind that Sets require immutable objects to function correctly.

The ListOrderedSet class provides an alternative approach, by wrapping an existing Set and retaining insertion order in the iterator.

This class is Serializable from Commons Collections 3.1.

Since:
Commons Collections 3.0
Version:
$Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
Author:
Matthew Hawthorne, Stephen Colebourne, Tom Dunham
See Also:
Serialized Form

Field Summary
protected  java.util.Set set
          Internal Set to maintain uniqueness.
 
Fields inherited from class org.apache.commons.collections.collection.AbstractCollectionDecorator
collection
 
Constructor Summary
protected SetUniqueList(java.util.List list, java.util.Set set)
          Constructor that wraps (not copies) the List and specifies the set to use.
 
Method Summary
 void add(int index, java.lang.Object object)
          Adds an element to a specific index in the list if it is not already present.
 boolean add(java.lang.Object object)
          Adds an element to the list if it is not already present.
 boolean addAll(java.util.Collection coll)
          Adds an element to the end of the list if it is not already present.
 boolean addAll(int index, java.util.Collection coll)
          Adds a collection of objects to the end of the list avoiding duplicates.
 java.util.Set asSet()
          Gets an unmodifiable view as a Set.
 void clear()
           
 boolean contains(java.lang.Object object)
           
 boolean containsAll(java.util.Collection coll)
           
static SetUniqueList decorate(java.util.List list)
          Factory method to create a SetList using the supplied list to retain order.
 java.util.Iterator iterator()
           
 java.util.ListIterator listIterator()
           
 java.util.ListIterator listIterator(int index)
           
 java.lang.Object remove(int index)
           
 boolean remove(java.lang.Object object)
           
 boolean removeAll(java.util.Collection coll)
           
 boolean retainAll(java.util.Collection coll)
           
 java.lang.Object set(int index, java.lang.Object object)
          Sets the value at the specified index avoiding duplicates.
 java.util.List subList(int fromIndex, int toIndex)
           
 
Methods inherited from class org.apache.commons.collections.list.AbstractListDecorator
get, getList, indexOf, lastIndexOf
 
Methods inherited from class org.apache.commons.collections.collection.AbstractCollectionDecorator
equals, getCollection, hashCode, isEmpty, size, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
equals, hashCode, isEmpty, size, toArray, toArray
 

Field Detail

set

protected final java.util.Set set
Internal Set to maintain uniqueness.

Constructor Detail

SetUniqueList

protected SetUniqueList(java.util.List list,
                        java.util.Set set)
Constructor that wraps (not copies) the List and specifies the set 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 SetUniqueList decorate(java.util.List list)
Factory method to create a SetList using the supplied list to retain order.

If the list contains duplicates, these are removed (first indexed one kept). A HashSet is used for the set behaviour.

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

asSet

public java.util.Set asSet()
Gets an unmodifiable view as a Set.

Returns:
an unmodifiable set view

add

public boolean add(java.lang.Object object)
Adds an element to the list if it is not already present.

(Violation) The List interface requires that this method returns true always. However this class may return false because of the Set behaviour.

Specified by:
add in interface java.util.Collection
Specified by:
add in interface java.util.List
Overrides:
add in class AbstractCollectionDecorator
Parameters:
object - the object to add
Returns:
true if object was added

add

public void add(int index,
                java.lang.Object object)
Adds an element to a specific index in the list if it is not already present.

(Violation) The List interface makes the assumption that the element is always inserted. This may not happen with this implementation.

Specified by:
add in interface java.util.List
Overrides:
add in class AbstractListDecorator
Parameters:
index - the index to insert at
object - the object to add

addAll

public boolean addAll(java.util.Collection coll)
Adds an element to the end of the list if it is not already present.

(Violation) The List interface makes the assumption that the element is always inserted. This may not happen with this implementation.

Specified by:
addAll in interface java.util.Collection
Specified by:
addAll in interface java.util.List
Overrides:
addAll in class AbstractCollectionDecorator
Parameters:
coll - the collection to add

addAll

public boolean addAll(int index,
                      java.util.Collection coll)
Adds a collection of objects to the end of the list avoiding duplicates.

Only elements that are not already in this list will be added, and duplicates from the specified collection will be ignored.

(Violation) The List interface makes the assumption that the elements are always inserted. This may not happen with this implementation.

Specified by:
addAll in interface java.util.List
Overrides:
addAll in class AbstractListDecorator
Parameters:
index - the index to insert at
coll - the collection to add in iterator order
Returns:
true if this collection changed

set

public java.lang.Object set(int index,
                            java.lang.Object object)
Sets the value at the specified index avoiding duplicates.

The object is set into the specified index. Afterwards, any previous duplicate is removed If the object is not already in the list then a normal set occurs. If it is present, then the old version is removed.

Specified by:
set in interface java.util.List
Overrides:
set in class AbstractListDecorator
Parameters:
index - the index to insert at
object - the object to set
Returns:
the previous object

remove

public boolean remove(java.lang.Object object)
Specified by:
remove in interface java.util.Collection
Specified by:
remove in interface java.util.List
Overrides:
remove in class AbstractCollectionDecorator

remove

public java.lang.Object remove(int index)
Specified by:
remove in interface java.util.List
Overrides:
remove in class AbstractListDecorator

removeAll

public boolean removeAll(java.util.Collection coll)
Specified by:
removeAll in interface java.util.Collection
Specified by:
removeAll in interface java.util.List
Overrides:
removeAll in class AbstractCollectionDecorator

retainAll

public boolean retainAll(java.util.Collection coll)
Specified by:
retainAll in interface java.util.Collection
Specified by:
retainAll in interface java.util.List
Overrides:
retainAll in class AbstractCollectionDecorator

clear

public void clear()
Specified by:
clear in interface java.util.Collection
Specified by:
clear in interface java.util.List
Overrides:
clear in class AbstractCollectionDecorator

contains

public boolean contains(java.lang.Object object)
Specified by:
contains in interface java.util.Collection
Specified by:
contains in interface java.util.List
Overrides:
contains in class AbstractCollectionDecorator

containsAll

public boolean containsAll(java.util.Collection coll)
Specified by:
containsAll in interface java.util.Collection
Specified by:
containsAll in interface java.util.List
Overrides:
containsAll in class AbstractCollectionDecorator

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
Overrides:
iterator in class AbstractCollectionDecorator

listIterator

public java.util.ListIterator listIterator()
Specified by:
listIterator in interface java.util.List
Overrides:
listIterator in class AbstractListDecorator

listIterator

public java.util.ListIterator listIterator(int index)
Specified by:
listIterator in interface java.util.List
Overrides:
listIterator in class AbstractListDecorator

subList

public java.util.List subList(int fromIndex,
                              int toIndex)
Specified by:
subList in interface java.util.List
Overrides:
subList in class AbstractListDecorator


Copyright © 2001-2008 The Apache Software Foundation. All Rights Reserved.