Class KeyListImpl<E>
- Type Parameters:
E
- type of elements stored in the list
- All Implemented Interfaces:
Serializable
,Cloneable
,Iterable<E>
,Collection<E>
,List<E>
,RandomAccess
,SequencedCollection<E>
,ICollection<E>
,IListable<E>
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class org.magicwerk.brownies.collections.IList
IList.IListableFromArray<E>, IList.IListableFromCollection<E>, IList.IListableFromList<E>, IList.IListableFromMult<E>, IList.Iter, IList.ListIter
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final boolean
If true the invariants are checked for debugging(package private) KeyCollectionImpl
<E> Key collection used for key storage (never null).List where the list content of this KeyListImpl is stored (never null).Fields inherited from class java.util.AbstractList
modCount
-
Constructor Summary
ConstructorsModifierConstructorDescription(package private)
Constructor.protected
KeyListImpl
(boolean copy, KeyListImpl<E> that) -
Method Summary
Modifier and TypeMethodDescriptionvoid
Inserts the specified element at the specified position in this list.boolean
asSet()
Returns a Set view of the element set.<K> int
binarySearch
(int index, int len, K key, Comparator<? super K> comparator) Searches the specified range for an object using the binary search algorithm.int
capacity()
Returns capacity of this list.void
clear()
clone()
Returns a shallow copy of this list.boolean
boolean
containsKey
(int keyIndex, Object key) Checks whether the specified key exists in this list.int
Counts how many times the specified element is contained in the list.crop()
Returns a copy this list but without elements.private void
Private method to check invariant of KeyListImpl.protected boolean
Helper method for adding an element to the list.protected void
Assign this list the content of the that list.protected void
Initialize this object after the bitwise copy has been made by Object.clone().doCreate
(int capacity) Create list with specified capacity.protected void
doEnsureCapacity
(int minCapacity) Increases the capacity of this list instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.protected E
doGet
(int index) Helper method for getting an element from the list.protected <T> void
doGetAll
(T[] array, int index, int len) Helper method to fill the specified elements in an array.protected E
doRemove
(int index) Helper method to remove an element.protected void
doRemoveAll
(int index, int len) Remove specified range of elements from list.protected E
Sets an element at specified position.protected E
Helper method for setting an element in the list.void
ensureCapacity
(int minCapacity) Increases the capacity of this instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.get
(int index) Return element at specified positionReturns all elements in the list equal to the specified element.getAllByKey
(int keyIndex, Object key) Returns a list with all elements with the specified key.getAllKeys
(int keyIndex) Returns list containing all keys in element order.Returns value for given key.int
getCountByKey
(int keyIndex, Object key) Returns number of elements with specified key.protected E
Returns distinct elements in the list.Set
<?> getDistinctKeys
(int keyIndex) Returns set containing all distinct keys.getKeyMapper
(int keyIndex) Returns mapper for specified key map.int
int
indexOfKey
(int keyIndex, Object key) Find given key and return its index.int
indexOfKey
(int keyIndex, Object key, int start) Find given key and return its index.(package private) void
initCopy
(KeyListImpl<E> that) Initialize object for copy() operation.(package private) void
initCrop
(KeyListImpl<E> that) Initialize object for crop() operation.protected void
invalidate
(E elem) Invalidate element, i.e.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.(package private) boolean
Determines whether calling indexOf() will be fast, i.e.boolean
isSorted()
Determines whether this list is sorted or not.protected E
Adds or replaces element.protected E
Put element by key into list.boolean
Removes all equal elements.removeAllByKey
(int keyIndex, Object key) Removes element by key.protected E
removeByKey
(int keyIndex, Object key) Removes element by key.int
size()
Return size of listvoid
sort
(int index, int len, Comparator<? super E> comparator) Sort specified elements in the list using the specified comparator.void
An application can use this operation to minimize the storage of an instance.unwrap()
Returns the elements stored in the KeyList as simple IList without constraints etc.Methods inherited from class org.magicwerk.brownies.collections.IList
addAll, addAll, addAll, addAll, addArray, addArray, addArray, addArray, addFirst, addIfAbsent, addLast, addMult, addMult, asDeque, asIListable, binarySearch, checkIndex, checkIndexAdd, checkLength, checkLengths, checkNonNull, checkRange, containsAll, containsAny, containsIf, copy, copy, countIf, descendingIterator, doAddAll, doClear, doCreateArray, doIndexOf, doIndexOfIf, doInitAll, doLastIndexOf, doLastIndexOfIf, doModify, doPutAll, doReplace, doRotate, doTransfer, doTransferSwap, drag, element, equals, equalsElem, extract, extractIf, fill, filter, filterMap, flatMap, getAll, getFirst, getFirstOrNull, getIf, getLast, getLastOrNull, getSingle, getSingleOrNull, hashCode, hashCodeElem, immutableList, indexOf, indexOfIf, indexOfIf, initAll, initAll, initArray, initMult, isEmpty, isReadOnly, iterator, lastIndexOf, lastIndexOf, lastIndexOfIf, lastIndexOfIf, listIterator, listIterator, map, mapFilter, move, offer, offerFirst, offerLast, peek, peekFirst, peekLast, poll, pollFirst, pollLast, pop, push, put, putAll, putAll, putArray, putMult, remove, remove, remove, removeAll, removeAll, removeFirst, removeFirstOccurrence, removeIf, removeLast, removeLastOccurrence, replace, replaceAll, replaceAll, replaceArray, replaceMult, resize, retain, retainAll, retainAll, retainIf, reverse, reverse, rotate, rotate, setAll, setAll, setArray, setArray, setMult, sort, swap, toArray, toArray, toArray, toArray, toArray, toArray, toArray, toString, transferCopy, transferMove, transferRemove, transferSwap, transform, transformedList, unmodifiableList
Methods inherited from class java.util.AbstractList
removeRange, subList
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, stream, toArray
Methods inherited from interface java.util.List
replaceAll, reversed, spliterator, subList
-
Field Details
-
keyColl
KeyCollectionImpl<E> keyCollKey collection used for key storage (never null). -
list
List where the list content of this KeyListImpl is stored (never null). If this is list sorted by element (key 0), keyColl.keyMaps[0].keysList will also reference this list. -
DEBUG_CHECK
private static final boolean DEBUG_CHECKIf true the invariants are checked for debugging- See Also:
-
-
Constructor Details
-
KeyListImpl
KeyListImpl()Constructor. -
KeyListImpl
-
-
Method Details
-
debugCheck
private void debugCheck()Private method to check invariant of KeyListImpl. It is only used for debugging. -
doAssign
Description copied from class:IList
Assign this list the content of the that list. This is done by bitwise copying so the that list should not be used afterwards. -
clone
Description copied from class:IList
Returns a shallow copy of this list. The new list will contain the same elements as the source list, i.e. the elements themselves are not copied. The capacity of the list will be set to the number of elements, i.e. size and capacity are equal. If the list is read-only, the same list is returned without change. UseIList.copy()
to . -
crop
Returns a copy this list but without elements. The new list will have the same type as this list and use the same comparator, ordering, etc. -
unwrap
Returns the elements stored in the KeyList as simple IList without constraints etc. The KeyList itself becomes empty, it is equal to a list created by calling crop(). This method is useful if you need performance or safety of a KeyList just while building the result, but you then want to return a simple IList without constraints and the KeyList is not needed any longer.- Returns:
- IList with elements
-
initCrop
Initialize object for crop() operation.- Parameters:
that
- source object
-
initCopy
Initialize object for copy() operation.- Parameters:
that
- source object
-
doClone
Description copied from class:IList
Initialize this object after the bitwise copy has been made by Object.clone(). -
asSet
Returns a Set view of the element set.- Returns:
- set view
- Throws:
IllegalArgumentException
- if the element set cannot be viewed as Set
-
isSorted
public boolean isSorted()Determines whether this list is sorted or not.- Returns:
- true if this a sorted list, false if not
-
capacity
public int capacity()Description copied from class:IList
Returns capacity of this list. Note that two lists are considered equal even if they have a distinct capacity. Also the capacity can be changed by operations like clone() etc. -
size
public int size()Description copied from interface:IListable
Return size of list -
get
Description copied from interface:IListable
Return element at specified position -
doGet
Description copied from class:IList
Helper method for getting an element from the list. This is the only method which really gets an element. Override if you need to validity checks before getting. -
doGetAll
protected <T> void doGetAll(T[] array, int index, int len) Description copied from class:IList
Helper method to fill the specified elements in an array. -
contains
If the list defines maps for the keys, they are used to speed up the lookup operation. So if the list defines a primary key, performance will be similar to a lookup in a set.
-
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
-
remove
If the list is sorted, this is used to speed up the remove operation. If the list defines unsorted map, this does not improve performance.
-
add
Note that the behavior of the operation depends on the defined constraints.
-
add
Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).Note that the behavior of the operation depends on the defined constraints.
-
set
Note that the behavior of the operation depends on the defined constraints.
-
clear
public void clear() -
ensureCapacity
public void ensureCapacity(int minCapacity) Description copied from class:IList
Increases the capacity of this instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.- Overrides:
ensureCapacity
in classIList<E>
- Parameters:
minCapacity
- the desired minimum capacity
-
doAdd
Description copied from class:IList
Helper method for adding an element to the list. This is the only method which really adds an element. Override if you need to validity checks before adding. -
doSet
Description copied from class:IList
Helper method for setting an element in the list. This is the only method which really sets an element. Override if you need to validity checks before setting. -
doRemove
Description copied from class:IList
Helper method to remove an element. This is the only method which really removes an element. Override if you need to validity checks before removing. -
doRemoveAll
protected void doRemoveAll(int index, int len) Description copied from class:IList
Remove specified range of elements from list.- Overrides:
doRemoveAll
in classIList<E>
- Parameters:
index
- index of first element to removelen
- number of elements to remove
-
doReSet
Description copied from class:IList
Sets an element at specified position. This method is used internally if existing elements will be moved etc. Override if you need to validity checks. -
indexOf
If the list is sorted, this is used to speed up the lookup operation. If the list defines unsorted map, this does not improve performance.
-
isIndexOfFast
boolean isIndexOfFast()Determines whether calling indexOf() will be fast, i.e. it can use some sort of key lookup instead of traversing through all elements.- Returns:
- true if calling indexOf() will be fast, otherwise false
-
indexOfKey
Find given key and return its index.- Parameters:
keyIndex
- key indexkey
- key to find- Returns:
- index of key or -1 if not found
-
indexOfKey
Find given key and return its index.- Parameters:
keyIndex
- key indexkey
- key to findstart
- start index for search- Returns:
- index of key or -1 if not found
-
containsKey
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
-
getKeyMapper
Returns mapper for specified key map.- Parameters:
keyIndex
- key index- Returns:
- mapper for specified key map
-
getByKey
Returns value for given 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
-
getAllByKey
Returns a list with all elements with the specified key.- Parameters:
keyIndex
- key indexkey
- key which elements must have- Returns:
- list 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
-
removeByKey
Removes element by key. If there are duplicates, only one element is removed.- Parameters:
keyIndex
- key indexkey
- key of element to remove- Returns:
- removed element or null if no element has been removed
-
putByKey
Put element by key into list.- Parameters:
keyIndex
- key indexelem
- element to putreplace
- true to replace an existing element with the same key, false to let the element unchanged- Returns:
- element with the same key (i.e. element which was replaced if replace is true / which was left unchanged if false), null if no element with the same key has been found
-
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
-
getAllKeys
Returns list containing all keys in element order.- Parameters:
keyIndex
- key index- Returns:
- list containing all keys
-
getDistinctKeys
Returns set containing all distinct keys.- Parameters:
keyIndex
- key index- Returns:
- list containing all distinct keys
-
binarySearch
Description copied from class:IList
Searches the specified range for an object using the binary search algorithm.Note that the method is defined to work with an arbitrary type <K>. This allows to search directly for a key field in the object without the need to construct an object containing the key:
persons.binarySearch("john", new SearchByName()); class SearchByName implements Comparator<Object> { public int compare(Object o1, Object o2) { String s1 = (o1 instanceof String) ? (String) o1 : ((Name) o1).getName(); String s2 = (o2 instanceof String) ? (String) o2 : ((Name) o2).getName(); return s1.compareTo(s2); } }
/- Specified by:
binarySearch
in classIList<E>
- Parameters:
index
- index of first element to searchlen
- number of elements to searchkey
- the value to be searched forcomparator
- the comparator by which the list is ordered. A null value indicates that the elements' natural ordering should be used.- Returns:
- index of the search key, if it is contained in the array; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the array: the index of the first element greater than the key, or a.length if all elements in the array are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.
- See Also:
-
sort
Description copied from class:IList
Sort specified elements in the list using the specified comparator. -
getAll
Description copied from class:IList
Returns all elements in the list equal to the specified element. -
count
Description copied from class:IList
Counts how many times the specified element is contained in the list. -
removeAll
Description copied from class:IList
Removes all equal elements. -
getDistinct
Description copied from class:IList
Returns distinct elements in the list.- Overrides:
getDistinct
in classIList<E>
- Returns:
- distinct elements in the list
-
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:if (contains(elem)) { 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
-
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)
-
getDefaultElem
- Specified by:
getDefaultElem
in classIList<E>
-
doEnsureCapacity
protected void doEnsureCapacity(int minCapacity) Description copied from class:IList
Increases the capacity of this list instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.- Specified by:
doEnsureCapacity
in classIList<E>
- Parameters:
minCapacity
- the desired minimum capacity
-
trimToSize
public void trimToSize()Description copied from class:IList
An application can use this operation to minimize the storage of an instance.- Specified by:
trimToSize
in classIList<E>
-
doCreate
Description copied from class:IList
Create list with specified capacity.
-