Class Key2List<E,K1,K2>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractList<E>
-
- org.magicwerk.brownies.collections.IList<E>
-
- org.magicwerk.brownies.collections.KeyListImpl<E>
-
- org.magicwerk.brownies.collections.Key2List<E,K1,K2>
-
- Type Parameters:
E
- type of elements stored in the listK1
- 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>
,java.util.List<E>
,java.util.RandomAccess
,ICollection<E>
,IListable<E>
- Direct Known Subclasses:
Key2List.ReadOnlyKey2List
public class Key2List<E,K1,K2> extends KeyListImpl<E>
Key2List implements a key list 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 list can be constraint (null/duplicate values).- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Key2List.Builder<E,K1,K2>
Builder to construct Key2List instances.protected static class
Key2List.ReadOnlyKey2List<E,K1,K2>
A read-only version ofKey2List
.-
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
-
Fields inherited from class org.magicwerk.brownies.collections.KeyListImpl
keyColl, list
-
-
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.Key2List<E,K1,K2>
clone()
Returns a shallow copy of this list.boolean
containsKey1(K1 key)
Checks whether an element with specified key exists.boolean
containsKey2(K2 key)
Checks whether an element with specified key exists.Key2List<E,K1,K2>
copy()
Returns a shallow copy of this list.int
count(E elem)
Counts how many times the specified element is contained in the list.Key2List<E,K1,K2>
crop()
Returns a copy this list but without elements.IList<E>
getAll(E elem)
Returns all elements in the list equal to the specified element.IList<E>
getAllByKey1(K1 key)
Returns all elements with specified key.IList<E>
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 Key2List.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<E>
getDistinct()
Returns distinct elements in the list.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.Key2List<E,K1,K2>
immutableList()
Returns an immutable copy of this list.int
indexOfKey1(K1 key)
Returns index of first element in list with specified key.int
indexOfKey2(K2 key)
Returns index of first element in list with specified key.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.boolean
isReadOnly()
Returns true if this list is either unmodifiable or immutable, false otherwise.E
put(E elem)
Adds or replaces element.E
putByKey1(E elem)
Adds or replaces element by key.E
putByKey2(E elem)
Adds or replaces element by key.E
putIfAbsentByKey1(E elem)
Adds element by key.E
putIfAbsentByKey2(E elem)
Adds element by key.IList<E>
removeAll(E elem)
Removes all equal elements.IList<E>
removeAllByKey1(K1 key)
Removes all elements with specified key.IList<E>
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.Key2List<E,K1,K2>
unmodifiableList()
Returns an unmodifiable view of this list.-
Methods inherited from class org.magicwerk.brownies.collections.KeyListImpl
add, add, asSet, binarySearch, capacity, clear, contains, containsKey, doAdd, doAssign, doClone, doCreate, doEnsureCapacity, doGet, doGetAll, doRemove, doRemoveAll, doReSet, doSet, ensureCapacity, get, getAllByKey, getAllKeys, getByKey, getCountByKey, getDefaultElem, getDistinctKeys, getKeyMapper, indexOf, indexOfKey, indexOfKey, initCopy, initCrop, invalidate, invalidateKey, isContainsFast, isIndexOfFast, isSorted, putByKey, remove, removeAllByKey, removeByKey, set, size, sort, trimToSize, unwrap
-
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, 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, indexOf, indexOfIf, indexOfIf, initAll, initAll, initArray, initMult, isEmpty, 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
-
-
-
-
Method Detail
-
getBuilder
protected Key2List.Builder<E,K1,K2> getBuilder()
- Returns:
- builder to use in extending classes
-
copy
public Key2List<E,K1,K2> copy()
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. This returned list will be modifiable, i.e. a read-only list will be copied and be modifiable again.
-
clone
public Key2List<E,K1,K2> 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 .- Overrides:
clone
in classKeyListImpl<E>
- Returns:
- a modifiable copy of this list
-
crop
public Key2List<E,K1,K2> crop()
Description copied from class:KeyListImpl
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.- Specified by:
crop
in interfaceICollection<E>
- Overrides:
crop
in classKeyListImpl<E>
- Returns:
- an empty copy of this list
-
getAll
public IList<E> getAll(E elem)
Description copied from class:IList
Returns all elements in the list equal to the specified element.- Overrides:
getAll
in classKeyListImpl<E>
- Parameters:
elem
- element to look for- Returns:
- all elements in the list equal to the specified element
-
count
public int count(E elem)
Description copied from class:IList
Counts how many times the specified element is contained in the list.- Overrides:
count
in classKeyListImpl<E>
- Parameters:
elem
- element to count- Returns:
- count how many times the specified element is contained in the list
-
removeAll
public IList<E> removeAll(E elem)
Description copied from class:IList
Removes all equal elements.- Overrides:
removeAll
in classKeyListImpl<E>
- Parameters:
elem
- element- Returns:
- removed equal elements (never null)
-
getDistinct
public java.util.Set<E> getDistinct()
Description copied from class:IList
Returns distinct elements in the list.- Overrides:
getDistinct
in classKeyListImpl<E>
- Returns:
- distinct elements in the list
-
put
public E put(E elem)
Description copied from class:KeyListImpl
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.- Overrides:
put
in classKeyListImpl<E>
- Parameters:
elem
- element- Returns:
- element which has been replaced or null otherwise
-
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. Note however that operations put() and remove() will be slow if the list is not sorted by the key and the element must therefore be searched. 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
-
indexOfKey1
public int indexOfKey1(K1 key)
Returns index of first element in list with specified key.- Parameters:
key
- key- Returns:
- index of first element, -1 if no such element exists
-
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 IList<E> 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 IList<E> 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 or replaces 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
-
putIfAbsentByKey1
public E putIfAbsentByKey1(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 left unchanged. So said simply, it is a shortcut for the following code:if (!containsKey1(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 left unchanged 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. Note however that operations put() and remove() will be slow if the list is not sorted by the key and the element must therefore be searched. 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
-
indexOfKey2
public int indexOfKey2(K2 key)
Returns index of first element in list with specified key.- Parameters:
key
- key- Returns:
- index of first element, -1 if no such element exists
-
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 IList<E> 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 IList<E> 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 or replaces 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
-
putIfAbsentByKey2
public E putIfAbsentByKey2(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 left unchanged. So said simply, it is a shortcut for the following code:if (!containsKey2(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 left unchanged 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)
-
isReadOnly
public boolean isReadOnly()
Description copied from class:IList
Returns true if this list is either unmodifiable or immutable, false otherwise.- Specified by:
isReadOnly
in classIList<E>
-
unmodifiableList
public Key2List<E,K1,K2> unmodifiableList()
Description copied from class:IList
Returns an unmodifiable view of this list. This method allows modules to provide users with "read-only" access to internal lists, where changes made to the original list are visible as query operations on the returned list "read through" to the specified list. Attempts to modify the returned list, whether direct or via its iterator, result in an UnsupportedOperationException. If this list is already unmodifiable, it is returned unchanged. SeeIList.immutableList()
to get an immutable copy of a list.- Specified by:
unmodifiableList
in classIList<E>
- Returns:
- an unmodifiable view of the specified list
-
immutableList
public Key2List<E,K1,K2> immutableList()
Description copied from class:IList
Returns an immutable copy of this list. The returned list is independent from the original list, i.e. changes done later are not seen. Attempts to modify the returned list, whether direct or via its iterator, result in an UnsupportedOperationException. If this list is already immutable, it is returned unchanged. SeeIList.unmodifiableList()
to get unmodifiable view of a list.- Specified by:
immutableList
in classIList<E>
- Returns:
- an immutable copy of the specified list
-
-