Class KeyListImpl<E>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
org.magicwerk.brownies.collections.IList<E>
org.magicwerk.brownies.collections.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>
A KeyList add key handling features to GapList.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class IList
IList.IListableFromArray<E>, IList.IListableFromCollection<E>, IList.IListableFromList<E>, IList.IListableFromMult<E>, IList.Iter, IList.ListIter -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final booleanIf 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 AbstractList
modCount -
Constructor Summary
ConstructorsModifierConstructorDescription(package private)Constructor.protectedKeyListImpl(boolean copy, KeyListImpl<E> that) -
Method Summary
Modifier and TypeMethodDescriptionvoidInserts the specified element at the specified position in this list.booleanasSet()Returns a Set view of the element set.<K> intbinarySearch(int index, int len, K key, Comparator<? super K> comparator) Searches the specified range for an object using the binary search algorithm.intcapacity()Returns capacity of this list.voidclear()clone()Returns a shallow copy of this list.booleanbooleancontainsKey(int keyIndex, Object key) Checks whether the specified key exists in this list.intCounts how many times the specified element is contained in the list.crop()Returns a copy this list but without elements.private voidPrivate method to check invariant of KeyListImpl.protected booleanHelper method for adding an element to the list.protected voidAssign this list the content of the that list.protected voidInitialize this object after the bitwise copy has been made by Object.clone().doCreate(int capacity) Create list with specified capacity.protected voiddoEnsureCapacity(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 EdoGet(int index) Helper method for getting an element from the list.protected <T> voiddoGetAll(T[] array, int index, int len) Helper method to fill the specified elements in an array.protected EdoRemove(int index) Helper method to remove an element.protected voiddoRemoveAll(int index, int len) Remove specified range of elements from list.protected ESets an element at specified position.protected EHelper method for setting an element in the list.voidensureCapacity(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.intgetCountByKey(int keyIndex, Object key) Returns number of elements with specified key.protected EReturns distinct elements in the list.Set<?> getDistinctKeys(int keyIndex) Returns set containing all distinct keys.getKeyMapper(int keyIndex) Returns mapper for specified key map.intintindexOfKey(int keyIndex, Object key) Find given key and return its index.intindexOfKey(int keyIndex, Object key, int start) Find given key and return its index.(package private) voidinitCopy(KeyListImpl<E> that) Initialize object for copy() operation.(package private) voidinitCrop(KeyListImpl<E> that) Initialize object for crop() operation.protected voidinvalidate(E elem) Invalidate element, i.e.protected voidinvalidateKey(int keyIndex, Object oldKey, Object newKey, E elem) Invalidate key value of element.(package private) booleanDetermines whether calling contains() will be fast, i.e.(package private) booleanDetermines whether calling indexOf() will be fast, i.e.booleanisSorted()Determines whether this list is sorted or not.protected EAdds or replaces element.protected EPut element by key into list.booleanRemoves all equal elements.removeAllByKey(int keyIndex, Object key) Removes element by key.protected EremoveByKey(int keyIndex, Object key) Removes element by key.intsize()Return size of listvoidsort(int index, int len, Comparator<? super E> comparator) Sort specified elements in the list using the specified comparator.voidAn 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 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, unmodifiableListMethods inherited from class AbstractList
removeRange, subListMethods inherited from interface Collection
parallelStream, stream, toArrayMethods inherited from interface List
replaceAll, reversed, spliterator, subList
-
Field Details
-
keyColl
KeyCollectionImpl<E> keyCollKey collection used for key storage (never null). -
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
-
clone
Description copied from class:IListReturns 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
-
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:IListReturns 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
-
get
-
doGet
Description copied from class:IListHelper 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:IListHelper 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
-
add
-
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
-
clear
-
ensureCapacity
public void ensureCapacity(int minCapacity) Description copied from class:IListIncreases 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:
ensureCapacityin classIList<E>- Parameters:
minCapacity- the desired minimum capacity
-
doAdd
Description copied from class:IListHelper 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:IListHelper 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:IListHelper 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:IListRemove specified range of elements from list.- Overrides:
doRemoveAllin classIList<E>- Parameters:
index- index of first element to removelen- number of elements to remove
-
doReSet
Description copied from class:IListSets 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
-
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
-
getByKey
-
getAllByKey
-
getCountByKey
Returns number of elements with specified key.- Parameters:
keyIndex- key indexkey- key which elements must have- Returns:
- number of elements with key
-
removeByKey
-
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
-
getAllKeys
-
getDistinctKeys
Returns set containing all distinct keys.- Parameters:
keyIndex- key index- Returns:
- list containing all distinct keys
-
binarySearch
Description copied from class:IListSearches 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:
binarySearchin 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:IListSort specified elements in the list using the specified comparator. -
getAll
-
count
-
removeAll
-
getDistinct
-
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:
getDefaultElemin classIList<E>
-
doEnsureCapacity
protected void doEnsureCapacity(int minCapacity) Description copied from class:IListIncreases 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:
doEnsureCapacityin classIList<E>- Parameters:
minCapacity- the desired minimum capacity
-
trimToSize
public void trimToSize()Description copied from class:IListAn application can use this operation to minimize the storage of an instance.- Specified by:
trimToSizein classIList<E>
-
doCreate
-