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
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Implementation of builder.(package private) static class
(package private) static class
List type used to store multiple elements. -
Field Summary
FieldsModifier and TypeFieldDescription(package private) boolean
True to allow null elements, false to reject them.All elements in the list must fulfill this predicate, if null, all elements are allowedprivate static final boolean
If true the invariants are checked for debugging(package private) KeyListImpl
<E> Back pointer to KeyListImpl if this object is used to implement a KeyList, Key1List, Key2List.(package private) KeyCollectionImpl.KeyMap<E,
Object>[] Maps for element and all defined keys.(package private) int
Maximum absolute or windows size, 0 if this list has no size restriction(package private) boolean
If maxSize is greater than 0, this boolean indicates whether the size is for window (true) or absolute (false)(package private) int
Index of key map which defines order (-1 for no order, only possible for KeyList).(package private) boolean
(package private) int
Size of collection.(package private) static final Object
(package private) static final Object
(package private) static final Object
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
boolean
addAll
(Collection<? extends E> c) (package private) void
Called from KeyListImpl.doAdd() to add element to sorted list.(package private) void
addUnsorted
(E elem) Called from KeyListImpl.doAdd() to add element to sorted list.private void
afterDelete
(E elem) This method is called after an existing element has been removed.private void
afterInsert
(E elem) This method is called after a new element has been added.asSet()
Returns a Set view of the element set.private void
beforeDelete
(E elem) This method is called before an existing element is removed.private void
beforeInsert
(E elem) This method is called before a new element is added.(package private) int
binarySearchSorted
(E elem) (package private) void
checkAddElem
(E elem) (package private) void
checkAsMap
(int keyIndex) Check whether the key map can be viewed as Map.(package private) void
Check whether the element set can be viewed as Set.(package private) void
checkElemAllowed
(E elem) Checks whether element is allowed in collection (null/constraint).(package private) void
checkIndex
(int loIndex, int hiIndex, E elem) Check whether index is valid for the sorted list.(package private) void
checkKeyMap
(int keyIndex) Check whether there is a KeyMap (can also be the elements set) with specified index.void
clear()
protected Object
clone()
boolean
boolean
containsAll
(Collection<?> c) protected <K> boolean
containsKey
(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.(package private) void
Private method to check invariant of KeyCollectionImpl.protected boolean
Helper method for adding an element to the collection.(package private) Object
doAdd
(E elem, KeyCollectionImpl.KeyMap<?, ?> ignore) Add element.(package private) boolean
doAddThrow
(E elem, KeyCollectionImpl.KeyMap<?, ?> ignore) private void
doClear
(KeyCollectionImpl.KeyMap<E, ?> keyMap) private void
doDebugCheck
(KeyCollectionImpl.KeyMap keyMap) private <K> void
doGetAllByKey
(KeyCollectionImpl.KeyMap<E, K> keyMap, K key, Collection<E> coll) (package private) E
doInvalidateKey
(int keyIndex, Object oldKey, Object newKey, E elem) doRemove
(Object elem, KeyCollectionImpl.KeyMap ignore) Remove element.doRemoveByKey
(int keyIndex, Object key) Removes element by key.boolean
(package private) static void
(package private) static void
errorDuplicateKey
(Object key) (package private) static void
(package private) static void
(package private) static void
(package private) static void
(package private) static void
(package private) static void
(package private) static void
(package private) static void
Create 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 void
getAllByKey
(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 E
Returns value for specified key.private <K> E
getByKey
(KeyCollectionImpl.KeyMap<E, K> keyMap, K key) int
Returns the number of equal elements.protected int
getCountByKey
(int keyIndex, Object key) Returns number of elements with specified key.private <K> int
getCountByKey
(KeyCollectionImpl.KeyMap<E, K> keyMap, K key) Returns all distinct elements in the same order as in the collection.protected Set
<?> getDistinctKeys
(int keyIndex) Returns set containing all distinct keys.(package private) Comparator
<Object> (package private) Object
(package private) KeyCollectionImpl.KeyMap
<E, Object> getKeyMap
(int keyIndex) getKeyMapper
(int keyIndex) Returns mapper for specified key map.(package private) boolean
int
hashCode()
(package private) int
indexOfSorted
(E elem) protected void
Initialize this object after the bitwise copy has been made by Object.clone().(package private) void
initCopy
(KeyCollectionImpl<E> that) Initialize object for copy() operation.(package private) void
initCrop
(KeyCollectionImpl<E> that) Initialize object for crop() operation.protected void
invalidate
(E elem) Invalidate element, i.e.invalidate
(KeyCollectionImpl.KeyMap keyMap, Object elem) protected void
invalidateKey
(int keyIndex, Object oldKey, Object newKey, E elem) Invalidate key value of element.(package private) boolean
Determines whether calling contains() will be fast, i.e.boolean
isEmpty()
boolean
isSorted()
Determines whether this list is sorted or not.(package private) boolean
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 E
Adds or replaces element.protected E
boolean
(package private) boolean
remove
(Object elem, KeyCollectionImpl.KeyMap ignore) Remove element.Removes all equal elements.boolean
removeAll
(Collection<?> c) protected Collection
<E> removeAllByKey
(int keyIndex, Object key) Removes element by key.protected void
removeAllByKey
(int keyIndex, Object key, Collection<E> removeds) protected E
removeByKey
(int keyIndex, Object key) boolean
retainAll
(Collection<?> c) (package private) void
int
size()
Object[]
toArray()
<T> T[]
toArray
(T[] a) toList()
Returns all elements contained in this collection as list.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
-
Field Details
-
SYMBOL_ADDED
-
SYMBOL_NOT_ADDED
-
SYMBOL_ERROR_NULL_KEY
-
DEBUG_CHECK
private static final boolean DEBUG_CHECKIf true the invariants are checked for debugging- See Also:
-
size
int sizeSize of collection. The size is cached, as the key maps do not know the size if duplicates are allowed. -
maxSize
int maxSizeMaximum absolute or windows size, 0 if this list has no size restriction -
movingWindow
boolean movingWindowIf maxSize is greater than 0, this boolean indicates whether the size is for window (true) or absolute (false) -
keyMaps
KeyCollectionImpl.KeyMap<E,Object>[] keyMapsMaps for element and all defined keys. keyMaps may be null for a KeyListImpl without keys. Index 0 is reserved for the elem key using an IdentMapper. If there is no elem key, keyMaps[0] contains null. -
orderByKey
int orderByKeyIndex of key map which defines order (-1 for no order, only possible for KeyList). If an order key is defined for a KeyList, it must be implemented as KeyMap.keysList. -
allowNullElem
boolean allowNullElemTrue to allow null elements, false to reject them. -
setBehavior
boolean setBehavior -
constraint
All elements in the list must fulfill this predicate, if null, all elements are allowed -
beforeInsertTrigger
-
afterInsertTrigger
-
beforeDeleteTrigger
-
afterDeleteTrigger
-
keyList
KeyListImpl<E> keyListBack pointer to KeyListImpl if this object is used to implement a KeyList, Key1List, Key2List. Otherwise null if it is part of a KeyCollection, Key1Collection, Key2Collection.
-
-
Constructor Details
-
KeyCollectionImpl
KeyCollectionImpl()Private constructor.
-
-
Method Details
-
initCopy
Initialize object for copy() operation.- Parameters:
that
- source object
-
initCrop
Initialize object for crop() operation.- Parameters:
that
- source object
-
debugCheck
void debugCheck()Private method to check invariant of KeyCollectionImpl. It is only used for debugging. -
doDebugCheck
-
getKey
-
isSorted
public boolean isSorted()Determines whether this list is sorted or not.- Returns:
- true if this a sorted list, false if not
-
isSortedByElem
boolean isSortedByElem() -
getElemSortComparator
Comparator<Object> getElemSortComparator() -
hasElemSet
boolean hasElemSet() -
checkIndex
Check whether index is valid for the sorted list. -
addSorted
Called from KeyListImpl.doAdd() to add element to sorted list. It calls beforeInsert() and afterInsert() as needed.- Parameters:
index
- index where element will be addedelem
- element to add
-
addUnsorted
Called from KeyListImpl.doAdd() to add element to sorted list. It calls beforeInsert() and afterInsert() as needed.- Parameters:
elem
- element to add
-
setSorted
-
binarySearchSorted
-
indexOfSorted
-
checkElemAllowed
Checks whether element is allowed in collection (null/constraint). The constraint is also checked if the element is null.- Parameters:
elem
- element to check- Throws:
IllegalArgumentException
- if the element is not allowed
-
errorNullElement
static void errorNullElement() -
errorConstraintElement
static void errorConstraintElement() -
errorNullKey
static void errorNullKey() -
errorMaxSize
static void errorMaxSize() -
errorDuplicateKey
-
errorInvalidData
static void errorInvalidData() -
errorInvalidIndex
static void errorInvalidIndex() -
errorInvalidateNotSupported
static void errorInvalidateNotSupported() -
errorInvalidSetBehavior
static void errorInvalidSetBehavior() -
errorInvaliDuplicates
static void errorInvaliDuplicates() -
beforeInsert
This method is called before a new element is added. If the addition should not happen, an exception can be thrown.- Parameters:
elem
- element to insert
-
afterInsert
This method is called after a new element has been added.- Parameters:
elem
- element which has been inserted
-
beforeDelete
This method is called before an existing element is removed. If the deletion should not happen, an exception can be thrown.- Parameters:
elem
- element to delete
-
afterDelete
This method is called after an existing element has been removed.- Parameters:
elem
- element which has been deleted
-
add
- Specified by:
add
in 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:
remove
in interfaceCollection<E>
-
checkAddElem
-
remove
Remove element.- Parameters:
elem
- element to removeignore
- KeyMap to ignore (null to remove element from all key maps)- Returns:
- true if element has been removed
-
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:
size
in interfaceCollection<E>
-
isEmpty
public boolean isEmpty()- Specified by:
isEmpty
in interfaceCollection<E>
-
contains
- Specified by:
contains
in interfaceCollection<E>
-
isContainsFast
boolean isContainsFast()Determines whether calling contains() will be fast, i.e. it can use some sort of key lookup instead of traversing through all elements.- Returns:
- true if calling contains() will be fast, otherwise false
-
iterator
-
addAll
- Specified by:
addAll
in interfaceCollection<E>
-
toList
Returns all elements contained in this collection as list.- Returns:
- all elements contained in this collection as list
-
toArray
- Specified by:
toArray
in interfaceCollection<E>
-
toArray
public <T> T[] toArray(T[] a) - Specified by:
toArray
in interfaceCollection<E>
-
containsAll
- Specified by:
containsAll
in interfaceCollection<E>
-
removeAll
- Specified by:
removeAll
in interfaceCollection<E>
-
retainAll
- Specified by:
retainAll
in interfaceCollection<E>
-
toString
-
doRemove
Remove element. It does not adjust the size and does not call triggers.- Parameters:
elem
- element to removeignore
- KeyMap to ignore (null to remove element from all key maps)- Returns:
- optional with element which has been removed
-
copy
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>
- 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:
crop
in 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:
clear
in interfaceCollection<E>
-
doClear
-
doAddThrow
-
doAdd
Add element. This method does not change size, check whether the element may be added or calls any triggers, so this must be done by the calling method.- Parameters:
elem
- element to addignore
- KeyMap to ignore (null to add element to all key maps)- Returns:
- SYMBOL_ADDED if element has successfully been added, SYMBOL_NOT_ADDED if element has not been added, SYMBOL_ERROR_NULL_KEY if a null key is not allowed, otherwise the key which is rejected because of a duplicate exception
-
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
-
checkKeyMap
void checkKeyMap(int keyIndex) Check whether there is a KeyMap (can also be the elements set) with specified index.- Parameters:
keyIndex
- key map index- Throws:
IllegalArgumentException
- if there is no such key map
-
checkAsMap
void checkAsMap(int keyIndex) Check whether the key map can be viewed as Map.- Parameters:
keyIndex
- key map index- Throws:
IllegalArgumentException
- if the key map cannot be viewed as Map
-
checkAsSet
void checkAsSet()Check whether the element set can be viewed as Set.- Parameters:
keyIndex
- key map index- Throws:
IllegalArgumentException
- if the element set cannot be viewed as Set
-
getKeyMap
-
getKeyMapper
Returns mapper for specified key map.- Parameters:
keyIndex
- key index- Returns:
- mapper for specified key map
-
getByKey
Returns value for specified key. If there are several values for this key, the first is returned. If the key is not found, null is returned.- Parameters:
keyIndex
- key indexkey
- key to find- Returns:
- value of specified key or null
-
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
-
doGetAllByKey
-
getCountByKey
Returns number of elements with specified key.- Parameters:
keyIndex
- key indexkey
- key which elements must have- Returns:
- number of elements with key
-
getCountByKey
-
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)
-
doInvalidateKey
-
invalidate
- Parameters:
keyMap
- key mapelem
- elem to invalidate- Returns:
- null if key for keyMap and element is correct, else key which must be added to keymap
-
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
- Specified by:
equals
in interfaceCollection<E>
- Overrides:
equals
in classObject
-
hashCode
public int hashCode()- Specified by:
hashCode
in interfaceCollection<E>
- Overrides:
hashCode
in 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
Returns all distinct elements in the same order as in the collection.- Returns:
- set with distinct elements
-
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>
- Parameters:
predicate
- filter predicate- Returns:
- created list
-
map
Description copied from interface:ICollection
Create a new list by applying the specified mapping function to all elements. The returned list is of typeIList
, typicallyGapList
unless the original type isBigList
.- Specified by:
map
in interfaceICollection<E>
- Parameters:
func
- mapping function- Returns:
- created list
-
flatMap
Description copied from interface:ICollection
Create a new list by applying the specified mapping function to all elements. The returned list is of typeIList
, typicallyGapList
unless the original type isBigList
.- Specified by:
flatMap
in interfaceICollection<E>
- Parameters:
func
- mapping function- Returns:
- created list
-
mapFilter
Description copied from interface:ICollection
Create a new list by applying the specified mapping function to all elements and then filtering it. The returned list is of typeIList
, typicallyGapList
unless the original type isBigList
.- Specified by:
mapFilter
in interfaceICollection<E>
- Parameters:
func
- mapping functionfilter
- filter predicate- Returns:
- created list
-
filterMap
Description copied from interface:ICollection
Create a new list by applying the specified filter first and then the mapping function to all elements selected. The returned list is of typeIList
, typicallyGapList
unless the original type isBigList
.- Specified by:
filterMap
in interfaceICollection<E>
- Parameters:
filter
- filter predicatefunc
- mapping function- Returns:
- created list
-