org.apache.commons.collections4.collection
Class IndexedCollection<K,C>

java.lang.Object
  extended by org.apache.commons.collections4.collection.AbstractCollectionDecorator<C>
      extended by org.apache.commons.collections4.collection.IndexedCollection<K,C>
Type Parameters:
K - the type of object in the index.
C - the type of object in the collection.
All Implemented Interfaces:
Serializable, Iterable<C>, Collection<C>

public class IndexedCollection<K,C>
extends AbstractCollectionDecorator<C>

An IndexedCollection is a Map-like view onto a Collection. It accepts a keyTransformer to define how the keys are converted from the values.

Modifications made to this decorator modify the index as well as the decorated Collection. However, modifications to the underlying Collection will not updated the index and it will get out of sync.

If modification to the decorated Collection is unavoidable, then a call to reindex() will update the index to the current contents of the Collection.

Since:
4.0
Version:
$Id: IndexedCollection.java 1477746 2013-04-30 18:11:20Z tn $
See Also:
Serialized Form

Constructor Summary
IndexedCollection(Collection<C> coll, Transformer<C,K> keyTransformer, MultiMap<K,C> map, boolean uniqueIndex)
          Create a IndexedCollection.
 
Method Summary
 boolean add(C object)
          
 boolean addAll(Collection<? extends C> coll)
           
 void clear()
           
 boolean contains(Object object)
          
 boolean containsAll(Collection<?> coll)
          
 C get(K key)
          Get the element associated with the given key.
static
<K,C> IndexedCollection<K,C>
nonUniqueIndexedCollection(Collection<C> coll, Transformer<C,K> keyTransformer)
          Create an IndexedCollection for a non-unique index.
 void reindex()
          Clears the index and re-indexes the entire decorated Collection.
 boolean remove(Object object)
           
 boolean removeAll(Collection<?> coll)
           
 boolean retainAll(Collection<?> coll)
           
static
<K,C> IndexedCollection<K,C>
uniqueIndexedCollection(Collection<C> coll, Transformer<C,K> keyTransformer)
          Create an IndexedCollection for a unique index.
 Collection<C> values(K key)
          Get all elements associated with the given key.
 
Methods inherited from class org.apache.commons.collections4.collection.AbstractCollectionDecorator
decorated, equals, hashCode, isEmpty, iterator, setCollection, size, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

IndexedCollection

public IndexedCollection(Collection<C> coll,
                         Transformer<C,K> keyTransformer,
                         MultiMap<K,C> map,
                         boolean uniqueIndex)
Create a IndexedCollection.

Parameters:
coll - decorated Collection
keyTransformer - Transformer for generating index keys
map - map to use as index
uniqueIndex - if the index shall enforce uniqueness of index keys
Method Detail

uniqueIndexedCollection

public static <K,C> IndexedCollection<K,C> uniqueIndexedCollection(Collection<C> coll,
                                                                   Transformer<C,K> keyTransformer)
Create an IndexedCollection for a unique index.

If an element is added, which maps to an existing key, an IllegalArgumentException will be thrown.

Type Parameters:
K - the index object type.
C - the collection type.
Parameters:
coll - the decorated Collection.
keyTransformer - the Transformer for generating index keys.
Returns:
the created IndexedCollection.

nonUniqueIndexedCollection

public static <K,C> IndexedCollection<K,C> nonUniqueIndexedCollection(Collection<C> coll,
                                                                      Transformer<C,K> keyTransformer)
Create an IndexedCollection for a non-unique index.

Type Parameters:
K - the index object type.
C - the collection type.
Parameters:
coll - the decorated Collection.
keyTransformer - the Transformer for generating index keys.
Returns:
the created IndexedCollection.

add

public boolean add(C object)

Specified by:
add in interface Collection<C>
Overrides:
add in class AbstractCollectionDecorator<C>
Throws:
IllegalArgumentException - if the object maps to an existing key and the index enforces a uniqueness constraint

addAll

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

clear

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

contains

public boolean contains(Object object)

Note: uses the index for fast lookup

Specified by:
contains in interface Collection<C>
Overrides:
contains in class AbstractCollectionDecorator<C>

containsAll

public boolean containsAll(Collection<?> coll)

Note: uses the index for fast lookup

Specified by:
containsAll in interface Collection<C>
Overrides:
containsAll in class AbstractCollectionDecorator<C>

get

public C get(K key)
Get the element associated with the given key.

In case of a non-unique index, this method will return the first value associated with the given key. To retrieve all elements associated with a key, use values(Object).

Parameters:
key - key to look up
Returns:
element found
See Also:
values(Object)

values

public Collection<C> values(K key)
Get all elements associated with the given key.

Parameters:
key - key to look up
Returns:
a collection of elements found, or null if contains(key) == false

reindex

public void reindex()
Clears the index and re-indexes the entire decorated Collection.


remove

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

removeAll

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

retainAll

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


Copyright © 2001–2013 The Apache Software Foundation. All rights reserved.