Class KeyCollectionImpl<E>
java.lang.Object
org.magicwerk.brownies.collections.KeyCollectionImpl<E>
- Type Parameters:
E- type of elements stored in the list
- All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, ICollection<E>
- Direct Known Subclasses:
Key1Collection, Key2Collection, KeyCollection
Add:
- validation fails: null / constraint
- duplicate not allowed (mode replace)
Triggers:
- triggers are called after the add/remove operation has finished
- if an exception is thrown in the trigger, the change already made to the collection is not undone
- Author:
- Thomas Mauch
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classImplementation of builder. -
Method Summary
Modifier and TypeMethodDescriptionbooleanbooleanaddAll(Collection<? extends E> c) asSet()Returns a Set view of the element set.voidclear()protected Objectclone()booleanbooleancontainsAll(Collection<?> c) protected <K> booleancontainsKey(int keyIndex, K key) Checks whether the specified key exists in this list.copy()Returns a copy of this collection with all its elements.crop()Returns a copy of this collection but without elements.protected booleanHelper method for adding an element to the collection.doRemoveByKey(int keyIndex, Object key) Removes element by key.booleanCreate a new collection by applying the specified filter to all elements.<R> IList<R> Create a new list by applying the specified filter first and then the mapping function to all elements selected.<R, C extends Collection<R>>
IList<R> Create a new list by applying the specified mapping function to all elements.Returns all equal elements.protected Collection<E> getAllByKey(int keyIndex, Object key) Returns a collection with all elements with the specified key.protected voidgetAllByKey(int keyIndex, Object key, Collection<E> coll) Fill the collection with all elements with the specified key.protected IList<?> getAllKeys(int keyIndex) Returns list containing all keys in element order.protected EReturns value for specified key.intReturns the number of equal elements.protected intgetCountByKey(int keyIndex, Object key) Returns number of elements with specified key.Returns all distinct elements in the same order as in the collection.protected Set<?> getDistinctKeys(int keyIndex) Returns set containing all distinct keys.getKeyMapper(int keyIndex) Returns mapper for specified key map.inthashCode()protected voidInitialize this object after the bitwise copy has been made by Object.clone().protected voidinvalidate(E elem) Invalidate element, i.e. all keys of the element are extracted again and stored in the key maps.protected voidinvalidateKey(int keyIndex, Object oldKey, Object newKey, E elem) Invalidate key value of element.booleanisEmpty()booleanisSorted()Determines whether this list is sorted or not.iterator()<R> IList<R> Create a new list by applying the specified mapping function to all elements.<R> IList<R> Create a new list by applying the specified mapping function to all elements and then filtering it.protected EAdds or replaces element.protected EbooleanRemoves all equal elements.booleanremoveAll(Collection<?> c) protected Collection<E> removeAllByKey(int keyIndex, Object key) Removes element by key.protected voidremoveAllByKey(int keyIndex, Object key, Collection<E> removeds) protected EremoveByKey(int keyIndex, Object key) booleanretainAll(Collection<?> c) intsize()Object[]toArray()<T> T[]toArray(T[] a) toList()Returns all elements contained in this collection as list.toString()Methods inherited from interface Collection
parallelStream, removeIf, spliterator, stream, toArrayMethods inherited from interface ICollection
containsIf, countIf, getFirst, getFirstOrNull, getIf, getSingle, getSingleOrNull
-
Method Details
-
isSorted
public boolean isSorted()Determines whether this list is sorted or not.- Returns:
- true if this a sorted list, false if not
-
add
- Specified by:
addin interfaceCollection<E>
-
doAdd
Helper method for adding an element to the collection. This is the only method which really adds an element. Override if you need to validity checks before adding. This method is called by both add() and addAll(). -
remove
- Specified by:
removein interfaceCollection<E>
-
put
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.- Parameters:
elem- element- Returns:
- element which has been replaced or null otherwise
-
size
public int size()- Specified by:
sizein interfaceCollection<E>
-
isEmpty
public boolean isEmpty()- Specified by:
isEmptyin interfaceCollection<E>
-
contains
- Specified by:
containsin interfaceCollection<E>
-
iterator
-
addAll
- Specified by:
addAllin interfaceCollection<E>
-
toList
-
toArray
- Specified by:
toArrayin interfaceCollection<E>
-
toArray
public <T> T[] toArray(T[] a) - Specified by:
toArrayin interfaceCollection<E>
-
containsAll
- Specified by:
containsAllin interfaceCollection<E>
-
removeAll
- Specified by:
removeAllin interfaceCollection<E>
-
retainAll
- Specified by:
retainAllin interfaceCollection<E>
-
toString
-
copy
Returns a copy of this collection with all its elements. The new collection will use the same comparator, ordering, etc.- Specified by:
copyin interfaceICollection<E>- Returns:
- a copy of this collection
-
crop
Returns a copy of this collection but without elements. The new collection will use the same comparator, ordering, etc.- Specified by:
cropin interfaceICollection<E>- Returns:
- an empty copy of this collection
-
clone
-
initClone
Initialize this object after the bitwise copy has been made by Object.clone().- Parameters:
that- source object
-
asSet
Returns a Set view of the element set.- Returns:
- set view
- Throws:
IllegalArgumentException- if the element set cannot be viewed as Set
-
clear
public void clear()- Specified by:
clearin interfaceCollection<E>
-
containsKey
protected <K> boolean containsKey(int keyIndex, K key) Checks whether the specified key exists in this list.- Parameters:
keyIndex- key indexkey- key to look for- Returns:
- true if the key exists, otherwise false
-
getDistinctKeys
Returns set containing all distinct keys.- Parameters:
keyIndex- key index- Returns:
- list containing all distinct keys
-
getAllKeys
Returns list containing all keys in element order.- Parameters:
keyIndex- key index- Returns:
- list containing all keys
-
getKeyMapper
-
getByKey
-
getAllByKey
Returns a collection with all elements with the specified key.- Parameters:
keyIndex- key indexkey- key which elements must have- Returns:
- list with all elements
-
getAllByKey
Fill the collection with all elements with the specified key.- Parameters:
keyIndex- key indexkey- key which elements must havecoll- collection with all elements
-
getCountByKey
Returns number of elements with specified key.- Parameters:
keyIndex- key indexkey- key which elements must have- Returns:
- number of elements with key
-
invalidate
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.- Parameters:
elem- element to invalidate
-
invalidateKey
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:
keyIndex- key indexoldKey- old key valuenewKey- new key valueelem- element to invalidate (can be null if there are no duplicates with this key)
-
removeByKey
-
doRemoveByKey
Removes element by key. It adjusts the size, but does not call triggers. If there are duplicates, only one element is removed.- Parameters:
keyIndex- index of key mapkey- key of element to remove- Returns:
- option with removed element as value or option without value if no element has been removed
-
removeAllByKey
Removes element by key. If there are duplicates, all elements are removed.- Parameters:
keyIndex- key indexkey- key of element to remove- Returns:
- true if elements have been removed, false otherwise
-
removeAllByKey
-
putByKey
-
equals
-
hashCode
public int hashCode()- Specified by:
hashCodein interfaceCollection<E>- Overrides:
hashCodein classObject
-
getAll
Returns all equal elements.- Parameters:
elem- element- Returns:
- all equal elements (never null)
-
getCount
Returns the number of equal elements.- Parameters:
elem- element- Returns:
- number of equal elements
-
removeAll
Removes all equal elements.- Parameters:
elem- element- Returns:
- removed equal elements (never null)
-
getDistinct
-
filter
Description copied from interface:ICollectionCreate a new collection by applying the specified filter to all elements. The returned collection has the same type as the original one.- Specified by:
filterin interfaceICollection<E>- Parameters:
predicate- filter predicate- Returns:
- created list
-
map
Description copied from interface:ICollectionCreate a new list by applying the specified mapping function to all elements. The returned list is of typeIList, typicallyGapListunless the original type isBigList.- Specified by:
mapin interfaceICollection<E>- Parameters:
func- mapping function- Returns:
- created list
-
flatMap
Description copied from interface:ICollectionCreate a new list by applying the specified mapping function to all elements. The returned list is of typeIList, typicallyGapListunless the original type isBigList.- Specified by:
flatMapin interfaceICollection<E>- Parameters:
func- mapping function- Returns:
- created list
-
mapFilter
Description copied from interface:ICollectionCreate a new list by applying the specified mapping function to all elements and then filtering it. The returned list is of typeIList, typicallyGapListunless the original type isBigList.- Specified by:
mapFilterin interfaceICollection<E>- Parameters:
func- mapping functionfilter- filter predicate- Returns:
- created list
-
filterMap
Description copied from interface:ICollectionCreate a new list by applying the specified filter first and then the mapping function to all elements selected. The returned list is of typeIList, typicallyGapListunless the original type isBigList.- Specified by:
filterMapin interfaceICollection<E>- Parameters:
filter- filter predicatefunc- mapping function- Returns:
- created list
-