org.apache.commons.collections.list

Class SetUniqueList

Implemented Interfaces:
Collection, List, Serializable

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.

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.

Version:
$Revision: 1.8 $ $Date: 2004/06/03 22:02:13 $
Authors:
Matthew Hawthorne
Stephen Colebourne
Since:
Commons Collections 3.0

Field Summary

protected Set
set
Internal Set to maintain uniqueness.

Fields inherited from class org.apache.commons.collections.collection.AbstractCollectionDecorator

collection

Constructor Summary

SetUniqueList(List list, Set set)
Constructor that wraps (not copies) the List and specifies the set to use.

Method Summary

boolean
add(Object object)
Adds an element to the list if it is not already present.
void
add(int index, Object object)
Adds an element to a specific index in the list if it is not already present.
boolean
addAll(Collection coll)
Adds an element to the end of the list if it is not already present.
boolean
addAll(int index, Collection coll)
Adds a collection of objects to the end of the list avoiding duplicates.
Set
asSet()
Gets an unmodifiable view as a Set.
void
clear()
boolean
contains(Object object)
boolean
containsAll(Collection coll)
static SetUniqueList
decorate(List list)
Factory method to create a SetList using the supplied list to retain order.
Iterator
iterator()
ListIterator
listIterator()
ListIterator
listIterator(int index)
boolean
remove(Object object)
Object
remove(int index)
boolean
removeAll(Collection coll)
boolean
retainAll(Collection coll)
Object
set(int index, Object object)
Sets the value at the specified index avoiding duplicates.
List
subList(int fromIndex, int toIndex)

Methods inherited from class org.apache.commons.collections.list.AbstractListDecorator

add, addAll, get, getList, indexOf, lastIndexOf, listIterator, listIterator, remove, set, subList

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

set

protected final Set set
Internal Set to maintain uniqueness.

Constructor Details

SetUniqueList

protected SetUniqueList(List list,
                        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:
list - the list to decorate, must not be null
set - the set to decorate, must not be null

Method Details

add

public boolean add(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.

Overrides:
add in interface AbstractCollectionDecorator
Parameters:
object - the object to add
Returns:
true if object was added

add

public void add(int index,
                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.

Overrides:
add in interface AbstractListDecorator
Parameters:
index - the index to insert at
object - the object to add

addAll

public boolean addAll(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.

Overrides:
addAll in interface AbstractCollectionDecorator
Parameters:
coll - the collection to add

addAll

public boolean addAll(int index,
                      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.

Overrides:
addAll in interface AbstractListDecorator
Parameters:
index - the index to insert at
coll - the collection to add in iterator order
Returns:
true if this collection changed

asSet

public Set asSet()
Gets an unmodifiable view as a Set.
Returns:
an unmodifiable set view

clear

public void clear()
Overrides:
clear in interface AbstractCollectionDecorator

contains

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

containsAll

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

decorate

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

iterator

public Iterator iterator()
Overrides:
iterator in interface AbstractCollectionDecorator

listIterator

public ListIterator listIterator()
Overrides:
listIterator in interface AbstractListDecorator

listIterator

public ListIterator listIterator(int index)
Overrides:
listIterator in interface AbstractListDecorator

remove

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

remove

public Object remove(int index)
Overrides:
remove in interface AbstractListDecorator

removeAll

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

retainAll

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

set

public Object set(int index,
                  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 and re-added at this index

Overrides:
set in interface AbstractListDecorator
Parameters:
index - the index to insert at
object - the object to set
Returns:
the previous object

subList

public List subList(int fromIndex,
                    int toIndex)
Overrides:
subList in interface AbstractListDecorator

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