Class Key2Collection<E,K1,K2>
- java.lang.Object
-
- org.magicwerk.brownies.collections.KeyCollectionImpl<E>
-
- org.magicwerk.brownies.collections.Key2Collection<E,K1,K2>
-
- Type Parameters:
E
- type of elements stored in the collectionK1
- type of first keyK2
- type of second key
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Cloneable
,java.lang.Iterable<E>
,java.util.Collection<E>
,ICollection<E>
- Direct Known Subclasses:
Key2Set
public class Key2Collection<E,K1,K2> extends KeyCollectionImpl<E>
Key2Collection implements a collection with 2 keys. These keys can be accessed fast. It can provide fast access to its elements like a Set. The elements allowed in the collection can be constraint (null/duplicate values).- See Also:
Key1List
, Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Key2Collection.Builder<E,K1,K2>
Builder to construct Key2Collection instances.-
Nested classes/interfaces inherited from class org.magicwerk.brownies.collections.KeyCollectionImpl
KeyCollectionImpl.BuilderImpl<E>, KeyCollectionImpl.KeyMap<E,K>, KeyCollectionImpl.KeyMapList<E>
-
-
Field Summary
-
Fields inherited from class org.magicwerk.brownies.collections.KeyCollectionImpl
afterDeleteTrigger, afterInsertTrigger, allowNullElem, beforeDeleteTrigger, beforeInsertTrigger, constraint, keyList, keyMaps, maxSize, movingWindow, orderByKey, setBehavior, size, SYMBOL_ADDED, SYMBOL_ERROR_NULL_KEY, SYMBOL_NOT_ADDED
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
Key2Collection()
Protected constructor used by builder or derived collections.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.Map<K1,E>
asMap1()
Returns a map view to the key map.java.util.Map<K2,E>
asMap2()
Returns a map view to the key map.boolean
containsKey1(K1 key)
Checks whether an element with specified key exists.boolean
containsKey2(K2 key)
Checks whether an element with specified key exists.Key2Collection<E,K1,K2>
copy()
Returns a copy of this collection with all its elements.Key2Collection<E,K1,K2>
crop()
Returns a copy of this collection but without elements.Key2Collection<E,K1,K2>
filter(java.util.function.Predicate<? super E> filter)
Create a new collection by applying the specified filter to all elements.Key2Collection<E,K1,K2>
getAll(E elem)
Returns all equal elements.Key2Collection<E,K1,K2>
getAllByKey1(K1 key)
Returns all elements with specified key.Key2Collection<E,K1,K2>
getAllByKey2(K2 key)
Returns all elements with specified key.IList<K1>
getAllKeys1()
Returns list containing all keys in element order.IList<K2>
getAllKeys2()
Returns list containing all keys in element order.protected Key2Collection.Builder<E,K1,K2>
getBuilder()
E
getByKey1(K1 key)
Returns element with specified key.E
getByKey2(K2 key)
Returns element with specified key.int
getCountByKey1(K1 key)
Returns the number of elements with specified key.int
getCountByKey2(K2 key)
Returns the number of elements with specified key.java.util.Set<K1>
getDistinctKeys1()
Returns all distinct keys in the same order as in the key map.java.util.Set<K2>
getDistinctKeys2()
Returns all distinct keys in the same order as in the key map.java.util.function.Function<E,K1>
getKey1Mapper()
Returns mapper for key map.java.util.function.Function<E,K2>
getKey2Mapper()
Returns mapper for key map.void
invalidate(E elem)
Invalidate element, i.e.void
invalidateKey1(K1 oldKey, K1 newKey, E elem)
Invalidate key value of element.void
invalidateKey2(K2 oldKey, K2 newKey, E elem)
Invalidate key value of element.E
put(E elem)
Adds or replaces element.E
putByKey1(E elem)
Adds element by key.E
putByKey2(E elem)
Adds element by key.Key2Collection<E,K1,K2>
removeAll(E elem)
Removes all equal elements.Key2Collection<E,K1,K2>
removeAllByKey1(K1 key)
Removes all elements with specified key.Key2Collection<E,K1,K2>
removeAllByKey2(K2 key)
Removes all elements with specified key.E
removeByKey1(K1 key)
Removes element with specified key.E
removeByKey2(K2 key)
Removes element with specified key.-
Methods inherited from class org.magicwerk.brownies.collections.KeyCollectionImpl
add, addAll, addSorted, addUnsorted, asSet, binarySearchSorted, checkAddElem, checkAsMap, checkAsSet, checkElemAllowed, checkIndex, checkKeyMap, clear, clone, contains, containsAll, containsKey, debugCheck, doAdd, doAdd, doAddThrow, doInvalidateKey, doRemove, doRemoveByKey, equals, errorConstraintElement, errorDuplicateKey, errorInvalidateNotSupported, errorInvalidData, errorInvalidIndex, errorInvalidSetBehavior, errorInvaliDuplicates, errorMaxSize, errorNullElement, errorNullKey, filterMap, flatMap, getAllByKey, getAllByKey, getAllKeys, getByKey, getCount, getCountByKey, getDistinct, getDistinctKeys, getElemSortComparator, getKey, getKeyMap, getKeyMapper, hasElemSet, hashCode, indexOfSorted, initClone, initCopy, initCrop, invalidateKey, isContainsFast, isEmpty, isSorted, isSortedByElem, iterator, map, mapFilter, putByKey, remove, remove, removeAll, removeAllByKey, removeAllByKey, removeByKey, retainAll, setSorted, size, toArray, toArray, toList, toString
-
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Collection
parallelStream, removeIf, spliterator, stream, toArray
-
Methods inherited from interface org.magicwerk.brownies.collections.ICollection
containsIf, countIf, getFirst, getFirstOrNull, getIf, getSingle, getSingleOrNull
-
-
-
-
Method Detail
-
getBuilder
protected Key2Collection.Builder<E,K1,K2> getBuilder()
- Returns:
- builder to use in extending classes
-
copy
public Key2Collection<E,K1,K2> copy()
Description copied from class:KeyCollectionImpl
Returns a copy of this collection with all its elements. The new collection will use the same comparator, ordering, etc.- Specified by:
copy
in interfaceICollection<E>
- Overrides:
copy
in classKeyCollectionImpl<E>
- Returns:
- a copy of this collection
-
crop
public Key2Collection<E,K1,K2> crop()
Description copied from class:KeyCollectionImpl
Returns a copy of this collection but without elements. The new collection will use the same comparator, ordering, etc.- Specified by:
crop
in interfaceICollection<E>
- Overrides:
crop
in classKeyCollectionImpl<E>
- Returns:
- an empty copy of this collection
-
getAll
public Key2Collection<E,K1,K2> getAll(E elem)
Description copied from class:KeyCollectionImpl
Returns all equal elements.- Overrides:
getAll
in classKeyCollectionImpl<E>
- Parameters:
elem
- element- Returns:
- all equal elements (never null)
-
removeAll
public Key2Collection<E,K1,K2> removeAll(E elem)
Description copied from class:KeyCollectionImpl
Removes all equal elements.- Overrides:
removeAll
in classKeyCollectionImpl<E>
- Parameters:
elem
- element- Returns:
- removed equal elements (never null)
-
put
public E put(E elem)
Description copied from class:KeyCollectionImpl
Adds or replaces element. If there is no such element, the element is added. If there is such an element, the element is replaced. So said simply, it is a shortcut for the following code:remove(elem); add(elem);
However the method is atomic in the sense that all or none operations are executed. So if there is already such an element, but adding the new one fails due to a constraint violation, the old element remains in the list.- Overrides:
put
in classKeyCollectionImpl<E>
- Parameters:
elem
- element- Returns:
- element which has been replaced or null otherwise
-
invalidate
public void invalidate(E elem)
Description copied from class:KeyCollectionImpl
Invalidate element, i.e. all keys of the element are extracted again and stored in the key maps. Old key values are removed if needed. You must call an invalidate method if an element's key value has changed after adding it to the collection.- Overrides:
invalidate
in classKeyCollectionImpl<E>
- Parameters:
elem
- element to invalidate
-
getKey1Mapper
public java.util.function.Function<E,K1> getKey1Mapper()
Returns mapper for key map.- Returns:
- mapper for key map
-
asMap1
public java.util.Map<K1,E> asMap1()
Returns a map view to the key map. The collection can be modified through the map as long as the constraint are not violated. The collections returned by the methods entrySet(), keySet(), and values() are immutable however.- Returns:
- map view to key map
- Throws:
java.lang.IllegalArgumentException
- if the key map cannot be viewed as Map
-
containsKey1
public boolean containsKey1(K1 key)
Checks whether an element with specified key exists.- Parameters:
key
- key- Returns:
- true if element with specified key exists, otherwise false
-
getByKey1
public E getByKey1(K1 key)
Returns element with specified key. If there are several elements with the same key, the one added first will be returned.- Parameters:
key
- key- Returns:
- element with specified key or null
-
getAllByKey1
public Key2Collection<E,K1,K2> getAllByKey1(K1 key)
Returns all elements with specified key.- Parameters:
key
- key- Returns:
- all elements with specified key (never null)
-
getCountByKey1
public int getCountByKey1(K1 key)
Returns the number of elements with specified key.- Parameters:
key
- key- Returns:
- number of elements with specified key
-
removeByKey1
public E removeByKey1(K1 key)
Removes element with specified key. If there are several elements with the same key, the one added first will be removed.- Parameters:
key
- key- Returns:
- element with specified key or null
-
removeAllByKey1
public Key2Collection<E,K1,K2> removeAllByKey1(K1 key)
Removes all elements with specified key.- Parameters:
key
- key- Returns:
- removed elements with specified key (never null)
-
getAllKeys1
public IList<K1> getAllKeys1()
Returns list containing all keys in element order.- Returns:
- list containing all keys
-
getDistinctKeys1
public java.util.Set<K1> getDistinctKeys1()
Returns all distinct keys in the same order as in the key map.- Returns:
- distinct keys
-
putByKey1
public E putByKey1(E elem)
Adds element by key. If there is no such element, the element is added. If there is such an element, the element is replaced. So said simply, it is a shortcut for the following code:removeByKey1(elem.getKey1()); add(elem);
However the method is atomic in the sense that all or none operations are executed. So if there is already such an element, but adding the new one fails due to a constraint violation, the old element remains in the list.- Parameters:
elem
- element- Returns:
- element with the same key which has been replaced or null otherwise
-
invalidateKey1
public void invalidateKey1(K1 oldKey, K1 newKey, E elem)
Invalidate key value of element. You must call an invalidate method if an element's key value has changed after adding it to the collection.- Parameters:
oldKey
- old key valuenewKey
- new key valueelem
- element to invalidate (can be null if there are no duplicates with this key)
-
getKey2Mapper
public java.util.function.Function<E,K2> getKey2Mapper()
Returns mapper for key map.- Returns:
- mapper for key map
-
asMap2
public java.util.Map<K2,E> asMap2()
Returns a map view to the key map. The collection can be modified through the map as long as the constraint are not violated. The collections returned by the methods entrySet(), keySet(), and values() are immutable however.- Returns:
- map view to key map
- Throws:
java.lang.IllegalArgumentException
- if the key map cannot be viewed as Map
-
containsKey2
public boolean containsKey2(K2 key)
Checks whether an element with specified key exists.- Parameters:
key
- key- Returns:
- true if element with specified key exists, otherwise false
-
getByKey2
public E getByKey2(K2 key)
Returns element with specified key. If there are several elements with the same key, the one added first will be returned.- Parameters:
key
- key- Returns:
- element with specified key or null
-
getAllByKey2
public Key2Collection<E,K1,K2> getAllByKey2(K2 key)
Returns all elements with specified key.- Parameters:
key
- key- Returns:
- all elements with specified key (never null)
-
getCountByKey2
public int getCountByKey2(K2 key)
Returns the number of elements with specified key.- Parameters:
key
- key- Returns:
- number of elements with specified key
-
removeByKey2
public E removeByKey2(K2 key)
Removes element with specified key. If there are several elements with the same key, the one added first will be removed.- Parameters:
key
- key- Returns:
- element with specified key or null
-
removeAllByKey2
public Key2Collection<E,K1,K2> removeAllByKey2(K2 key)
Removes all elements with specified key.- Parameters:
key
- key- Returns:
- removed elements with specified key (never null)
-
getAllKeys2
public IList<K2> getAllKeys2()
Returns list containing all keys in element order.- Returns:
- list containing all keys
-
getDistinctKeys2
public java.util.Set<K2> getDistinctKeys2()
Returns all distinct keys in the same order as in the key map.- Returns:
- distinct keys
-
putByKey2
public E putByKey2(E elem)
Adds element by key. If there is no such element, the element is added. If there is such an element, the element is replaced. So said simply, it is a shortcut for the following code:removeByKey2(elem.getKey2()); add(elem);
However the method is atomic in the sense that all or none operations are executed. So if there is already such an element, but adding the new one fails due to a constraint violation, the old element remains in the list.- Parameters:
elem
- element- Returns:
- element with the same key which has been replaced or null otherwise
-
invalidateKey2
public void invalidateKey2(K2 oldKey, K2 newKey, E elem)
Invalidate key value of element. You must call an invalidate method if an element's key value has changed after adding it to the collection.- Parameters:
oldKey
- old key valuenewKey
- new key valueelem
- element to invalidate (can be null if there are no duplicates with this key)
-
filter
public Key2Collection<E,K1,K2> filter(java.util.function.Predicate<? super E> filter)
Description copied from interface:ICollection
Create a new collection by applying the specified filter to all elements. The returned collection has the same type as the original one.- Specified by:
filter
in interfaceICollection<E>
- Overrides:
filter
in classKeyCollectionImpl<E>
- Parameters:
filter
- filter predicate- Returns:
- created list
-
-