Class KeyCollectionImpl<E>

  • Type Parameters:
    E - type of elements stored in the list
    All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable, java.lang.Iterable<E>, java.util.Collection<E>, ICollection<E>
    Direct Known Subclasses:
    Key1Collection, Key2Collection, KeyCollection, KeyCollections.KeyNCollection

    public class KeyCollectionImpl<E>
    extends java.lang.Object
    implements ICollection<E>, java.io.Serializable, java.lang.Cloneable
    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:
    GapList, Serialized Form
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(E elem)  
      boolean addAll​(java.util.Collection<? extends E> c)  
      java.util.Set<E> asSet()
      Returns a Set view of the element set.
      void clear()  
      protected java.lang.Object clone()  
      boolean contains​(java.lang.Object o)  
      boolean containsAll​(java.util.Collection<?> c)  
      protected <K> boolean containsKey​(int keyIndex, K key)
      Checks whether the specified key exists in this list.
      KeyCollectionImpl copy()
      Returns a copy of this collection with all its elements.
      int count​(E elem)
      Counts how many times the element appears in the collection.
      int countDistinct()
      Returns count for all distinct elements.
      protected int countDistinctKeys​(int keyIndex)
      Returns count of all distinct keys.
      KeyCollectionImpl crop()
      Returns a copy of this collection but without elements.
      protected boolean doAdd​(E elem)
      Helper method for adding an element to the collection.
      protected Option<E> doRemoveByKey​(int keyIndex, java.lang.Object key)
      Removes element by key.
      boolean equals​(java.lang.Object o)  
      KeyCollectionImpl filter​(java.util.function.Predicate<? super E> predicate)
      Create a new collection by applying the specified filter to all elements.
      <R> IList<R> filterMap​(java.util.function.Predicate<E> filter, java.util.function.Function<E,​R> func)
      Create a new list by applying the specified filter first and then the mapping function to all elements selected.
      <R,​C extends java.util.Collection<R>>
      IList<R>
      flatMap​(java.util.function.Function<E,​C> func)
      Create a new list by applying the specified mapping function to all elements.
      java.util.Collection<E> getAll​(E elem)
      Returns all equal elements.
      protected java.util.Collection<E> getAllByKey​(int keyIndex, java.lang.Object key)
      Returns a collection with all elements with the specified key.
      protected void getAllByKey​(int keyIndex, java.lang.Object key, java.util.Collection<E> coll)
      Fill the collection with all elements with the specified key.
      protected java.util.Collection<E> getAllByKeys​(int keyIndex1, java.lang.Object key1, int keyIndex2, java.lang.Object key2)
      Returns all elements with specified keys.
      protected IList<?> getAllKeys​(int keyIndex)
      Returns list containing all keys in element order.
      protected E getByKey​(int keyIndex, java.lang.Object key)
      Returns value for specified key.
      protected int getCountByKey​(int keyIndex, java.lang.Object key)
      Returns number of elements with specified key.
      java.util.Set<E> getDistinct()
      Returns all distinct elements in the same order as in the collection.
      protected java.util.Set<?> getDistinctKeys​(int keyIndex)
      Returns set containing all distinct keys.
      protected java.util.function.Function<E,​java.lang.Object> getKeyMapper​(int keyIndex)
      Returns mapper for specified key map.
      int getSortedByKey()
      Returns index of the key controlling the sorting of the collection (with 0 for the element).
      int hashCode()  
      protected void initClone​(java.lang.Object that)
      Initialize this object after the bitwise copy has been made by Object.clone().
      protected void invalidate​(E elem)
      Invalidate element, i.e. all keys of the element are extracted again and stored in the key maps.
      protected void invalidateKey​(int keyIndex, java.lang.Object oldKey, java.lang.Object newKey, E elem)
      Invalidate key value of element.
      boolean isEmpty()  
      boolean isSorted()
      Determines whether this collection is sorted or not.
      java.util.Iterator<E> iterator()  
      java.util.Iterator<E> iteratorDistinct()
      Returns iterator for all distinct elements in the same order as in the collection.
      protected java.util.Iterator<?> iteratorDistinctKeys​(int keyIndex)
      Returns iterator for all distinct keys.
      <R> IList<R> map​(java.util.function.Function<E,​R> func)
      Create a new list by applying the specified mapping function to all elements.
      <R> IList<R> mapFilter​(java.util.function.Function<E,​R> func, java.util.function.Predicate<R> filter)
      Create a new list by applying the specified mapping function to all elements and then filtering it.
      protected E put​(E elem)
      Adds or replaces element.
      protected E putByKey​(int keyIndex, E elem, boolean replace)  
      boolean remove​(java.lang.Object elem)  
      java.util.Collection<E> removeAll​(E elem)
      Removes all equal elements.
      boolean removeAll​(java.util.Collection<?> c)  
      protected java.util.Collection<E> removeAllByKey​(int keyIndex, java.lang.Object key)
      Removes element by key.
      protected void removeAllByKey​(int keyIndex, java.lang.Object key, java.util.Collection<E> removeds)  
      protected E removeByKey​(int keyIndex, java.lang.Object key)  
      boolean retainAll​(java.util.Collection<?> c)  
      int size()  
      java.lang.Object[] toArray()  
      <T> T[] toArray​(T[] a)  
      GapList<E> toList()
      Returns all elements contained in this collection as list.
      java.lang.String 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 java.lang.Iterable

        forEach
    • Method Detail

      • isSorted

        public boolean isSorted()
        Determines whether this collection is sorted or not.
        Returns:
        true if this a collection list, false if not
      • getSortedByKey

        public int getSortedByKey()
        Returns index of the key controlling the sorting of the collection (with 0 for the element).
      • add

        public boolean add​(E elem)
        Specified by:
        add in interface java.util.Collection<E>
      • doAdd

        protected boolean doAdd​(E elem)
        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

        public boolean remove​(java.lang.Object elem)
        Specified by:
        remove in interface java.util.Collection<E>
      • put

        protected E put​(E elem)
        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 interface java.util.Collection<E>
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Collection<E>
      • contains

        public boolean contains​(java.lang.Object o)
        Specified by:
        contains in interface java.util.Collection<E>
      • iterator

        public java.util.Iterator<E> iterator()
        Specified by:
        iterator in interface java.util.Collection<E>
        Specified by:
        iterator in interface java.lang.Iterable<E>
      • addAll

        public boolean addAll​(java.util.Collection<? extends E> c)
        Specified by:
        addAll in interface java.util.Collection<E>
      • toList

        public GapList<E> toList()
        Returns all elements contained in this collection as list.
        Returns:
        all elements contained in this collection as list
      • toArray

        public java.lang.Object[] toArray()
        Specified by:
        toArray in interface java.util.Collection<E>
      • toArray

        public <T> T[] toArray​(T[] a)
        Specified by:
        toArray in interface java.util.Collection<E>
      • containsAll

        public boolean containsAll​(java.util.Collection<?> c)
        Specified by:
        containsAll in interface java.util.Collection<E>
      • removeAll

        public boolean removeAll​(java.util.Collection<?> c)
        Specified by:
        removeAll in interface java.util.Collection<E>
      • retainAll

        public boolean retainAll​(java.util.Collection<?> c)
        Specified by:
        retainAll in interface java.util.Collection<E>
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • copy

        public KeyCollectionImpl 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 interface ICollection<E>
        Returns:
        a copy of this collection
      • crop

        public KeyCollectionImpl crop()
        Returns a copy of this collection but without elements. The new collection will use the same comparator, ordering, etc.
        Specified by:
        crop in interface ICollection<E>
        Returns:
        an empty copy of this collection
      • clone

        protected java.lang.Object clone()
        Overrides:
        clone in class java.lang.Object
      • initClone

        protected void initClone​(java.lang.Object that)
        Initialize this object after the bitwise copy has been made by Object.clone().
        Parameters:
        that - source object
      • asSet

        public java.util.Set<E> asSet()
        Returns a Set view of the element set.
        Returns:
        set view
        Throws:
        java.lang.IllegalArgumentException - if the element set cannot be viewed as Set
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Collection<E>
      • containsKey

        protected <K> boolean containsKey​(int keyIndex,
                                          K key)
        Checks whether the specified key exists in this list.
        Parameters:
        keyIndex - key index
        key - key to look for
        Returns:
        true if the key exists, otherwise false
      • getDistinctKeys

        protected java.util.Set<?> getDistinctKeys​(int keyIndex)
        Returns set containing all distinct keys.
        Parameters:
        keyIndex - key index
        Returns:
        list containing all distinct keys
      • iteratorDistinctKeys

        protected java.util.Iterator<?> iteratorDistinctKeys​(int keyIndex)
        Returns iterator for all distinct keys.
        Parameters:
        keyIndex - key index
        Returns:
        iterator for all distinct keys
      • countDistinctKeys

        protected int countDistinctKeys​(int keyIndex)
        Returns count of all distinct keys.
        Parameters:
        keyIndex - key index
        Returns:
        count of all distinct keys
      • getAllKeys

        protected IList<?> getAllKeys​(int keyIndex)
        Returns list containing all keys in element order.
        Parameters:
        keyIndex - key index
        Returns:
        list containing all keys
      • getKeyMapper

        protected java.util.function.Function<E,​java.lang.Object> getKeyMapper​(int keyIndex)
        Returns mapper for specified key map.
        Parameters:
        keyIndex - key index
        Returns:
        mapper for specified key map
      • getByKey

        protected E getByKey​(int keyIndex,
                             java.lang.Object key)
        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 index
        key - key to find
        Returns:
        value of specified key or null
      • getAllByKeys

        protected java.util.Collection<E> getAllByKeys​(int keyIndex1,
                                                       java.lang.Object key1,
                                                       int keyIndex2,
                                                       java.lang.Object key2)
        Returns all elements with specified keys.
        Parameters:
        key1 - key 1
        key2 - key 2
        Returns:
        all elements with specified keys (never null)
      • getAllByKey

        protected java.util.Collection<E> getAllByKey​(int keyIndex,
                                                      java.lang.Object key)
        Returns a collection with all elements with the specified key.
        Parameters:
        keyIndex - key index
        key - key which elements must have
        Returns:
        list with all elements
      • getAllByKey

        protected void getAllByKey​(int keyIndex,
                                   java.lang.Object key,
                                   java.util.Collection<E> coll)
        Fill the collection with all elements with the specified key.
        Parameters:
        keyIndex - key index
        key - key which elements must have
        coll - collection with all elements
      • getCountByKey

        protected int getCountByKey​(int keyIndex,
                                    java.lang.Object key)
        Returns number of elements with specified key.
        Parameters:
        keyIndex - key index
        key - key which elements must have
        Returns:
        number of elements with key
      • invalidate

        protected void invalidate​(E elem)
        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

        protected void invalidateKey​(int keyIndex,
                                     java.lang.Object oldKey,
                                     java.lang.Object 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:
        keyIndex - key index
        oldKey - old key value
        newKey - new key value
        elem - element to invalidate (can be null if there are no duplicates with this key)
      • removeByKey

        protected E removeByKey​(int keyIndex,
                                java.lang.Object key)
      • doRemoveByKey

        protected Option<E> doRemoveByKey​(int keyIndex,
                                          java.lang.Object key)
        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 map
        key - key of element to remove
        Returns:
        option with removed element as value or option without value if no element has been removed
      • removeAllByKey

        protected java.util.Collection<E> removeAllByKey​(int keyIndex,
                                                         java.lang.Object key)
        Removes element by key. If there are duplicates, all elements are removed.
        Parameters:
        keyIndex - key index
        key - key of element to remove
        Returns:
        true if elements have been removed, false otherwise
      • removeAllByKey

        protected void removeAllByKey​(int keyIndex,
                                      java.lang.Object key,
                                      java.util.Collection<E> removeds)
      • putByKey

        protected E putByKey​(int keyIndex,
                             E elem,
                             boolean replace)
      • equals

        public boolean equals​(java.lang.Object o)
        Specified by:
        equals in interface java.util.Collection<E>
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Specified by:
        hashCode in interface java.util.Collection<E>
        Overrides:
        hashCode in class java.lang.Object
      • getAll

        public java.util.Collection<E> getAll​(E elem)
        Returns all equal elements.
        Parameters:
        elem - element
        Returns:
        all equal elements (never null)
      • count

        public int count​(E elem)
        Description copied from interface: ICollection
        Counts how many times the element appears in the collection.
        Specified by:
        count in interface ICollection<E>
        Returns:
        count how many times the element appears in the collection
      • removeAll

        public java.util.Collection<E> removeAll​(E elem)
        Removes all equal elements.
        Parameters:
        elem - element
        Returns:
        removed equal elements (never null)
      • getDistinct

        public java.util.Set<E> getDistinct()
        Returns all distinct elements in the same order as in the collection.
        Returns:
        set with distinct elements
      • iteratorDistinct

        public java.util.Iterator<E> iteratorDistinct()
        Returns iterator for all distinct elements in the same order as in the collection.
        Returns:
        iterator with distinct elements
      • countDistinct

        public int countDistinct()
        Returns count for all distinct elements.
        Returns:
        count for all distinct elements
      • filter

        public KeyCollectionImpl filter​(java.util.function.Predicate<? super E> predicate)
        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 interface ICollection<E>
        Parameters:
        predicate - filter predicate
        Returns:
        created collection
      • map

        public <R> IList<R> map​(java.util.function.Function<E,​R> func)
        Description copied from interface: ICollection
        Create a new list by applying the specified mapping function to all elements. The returned list is of type IList, typically GapList unless the original type is BigList.
        Specified by:
        map in interface ICollection<E>
        Parameters:
        func - mapping function
        Returns:
        created list
      • flatMap

        public <R,​C extends java.util.Collection<R>> IList<R> flatMap​(java.util.function.Function<E,​C> func)
        Description copied from interface: ICollection
        Create a new list by applying the specified mapping function to all elements. The returned list is of type IList, typically GapList unless the original type is BigList.
        Specified by:
        flatMap in interface ICollection<E>
        Parameters:
        func - mapping function
        Returns:
        created list
      • mapFilter

        public <R> IList<R> mapFilter​(java.util.function.Function<E,​R> func,
                                      java.util.function.Predicate<R> filter)
        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 type IList, typically GapList unless the original type is BigList.
        Specified by:
        mapFilter in interface ICollection<E>
        Parameters:
        func - mapping function
        filter - filter predicate
        Returns:
        created list
      • filterMap

        public <R> IList<R> filterMap​(java.util.function.Predicate<E> filter,
                                      java.util.function.Function<E,​R> func)
        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 type IList, typically GapList unless the original type is BigList.
        Specified by:
        filterMap in interface ICollection<E>
        Parameters:
        filter - filter predicate
        func - mapping function
        Returns:
        created list