K
- the type of object in the index.C
- the type of object in the collection.public class IndexedCollection<K,C> extends AbstractCollectionDecorator<C>
Modifications made to this decorator modify the index as well as the
decorated Collection
. However, modifications to the underlying
Collection
will not update the index and it will get out of sync.
If modification of the decorated Collection
is unavoidable, then a
call to reindex()
will update the index to the current contents of
the Collection
.
Constructor and Description |
---|
IndexedCollection(Collection<C> coll,
Transformer<C,K> keyTransformer,
MultiMap<K,C> map,
boolean uniqueIndex)
Create a
IndexedCollection . |
Modifier and Type | Method and Description |
---|---|
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.
|
decorated, isEmpty, iterator, setCollection, size, toArray, toArray, toString
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
equals, hashCode, parallelStream, removeIf, spliterator, stream
public IndexedCollection(Collection<C> coll, Transformer<C,K> keyTransformer, MultiMap<K,C> map, boolean uniqueIndex)
IndexedCollection
.coll
- decorated Collection
keyTransformer
- Transformer
for generating index keysmap
- map to use as indexuniqueIndex
- if the index shall enforce uniqueness of index keyspublic static <K,C> IndexedCollection<K,C> uniqueIndexedCollection(Collection<C> coll, Transformer<C,K> keyTransformer)
IndexedCollection
for a unique index.
If an element is added, which maps to an existing key, an IllegalArgumentException
will be thrown.
K
- the index object type.C
- the collection type.coll
- the decorated Collection
.keyTransformer
- the Transformer
for generating index keys.IndexedCollection
.public static <K,C> IndexedCollection<K,C> nonUniqueIndexedCollection(Collection<C> coll, Transformer<C,K> keyTransformer)
IndexedCollection
for a non-unique index.K
- the index object type.C
- the collection type.coll
- the decorated Collection
.keyTransformer
- the Transformer
for generating index keys.IndexedCollection
.public boolean add(C object)
add
in interface Collection<C>
add
in class AbstractCollectionDecorator<C>
IllegalArgumentException
- if the object maps to an existing key and the index
enforces a uniqueness constraintpublic boolean addAll(Collection<? extends C> coll)
addAll
in interface Collection<C>
addAll
in class AbstractCollectionDecorator<C>
public void clear()
clear
in interface Collection<C>
clear
in class AbstractCollectionDecorator<C>
public boolean contains(Object object)
Note: uses the index for fast lookup
contains
in interface Collection<C>
contains
in class AbstractCollectionDecorator<C>
public boolean containsAll(Collection<?> coll)
Note: uses the index for fast lookup
containsAll
in interface Collection<C>
containsAll
in class AbstractCollectionDecorator<C>
public C get(K 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)
.
key
- key to look upvalues(Object)
public Collection<C> values(K key)
key
- key to look upcontains(key) == false
public void reindex()
Collection
.public boolean remove(Object object)
remove
in interface Collection<C>
remove
in class AbstractCollectionDecorator<C>
public boolean removeAll(Collection<?> coll)
removeAll
in interface Collection<C>
removeAll
in class AbstractCollectionDecorator<C>
public boolean retainAll(Collection<?> coll)
retainAll
in interface Collection<C>
retainAll
in class AbstractCollectionDecorator<C>
Copyright © 2001–2018 The Apache Software Foundation. All rights reserved.