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>
Direct Known Subclasses:
Key1List, Key2List, KeyList

public abstract class KeyListImpl<E> extends IList<E>
A KeyList add key handling features to GapList.
See Also:
  • Field Details

    • keyColl

      Key collection used for key storage (never null).
    • list

      IList<E> 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_CHECK
      If true the invariants are checked for debugging
      See Also:
  • Constructor Details

    • KeyListImpl

      KeyListImpl()
      Constructor.
    • KeyListImpl

      protected KeyListImpl(boolean copy, KeyListImpl<E> that)
  • Method Details

    • debugCheck

      private void debugCheck()
      Private method to check invariant of KeyListImpl. It is only used for debugging.
    • doAssign

      protected void doAssign(IList<E> that)
      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.
      Specified by:
      doAssign in class IList<E>
      Parameters:
      that - list to copy content from
    • clone

      public KeyListImpl<E> 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. Use IList.copy() to .
      Overrides:
      clone in class IList<E>
      Returns:
      a modifiable copy of this list
    • crop

      public KeyListImpl<E> 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.
      Specified by:
      crop in interface ICollection<E>
      Overrides:
      crop in class IList<E>
      Returns:
      an empty copy of this list
    • unwrap

      public IList<E> 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

      void initCrop(KeyListImpl<E> that)
      Initialize object for crop() operation.
      Parameters:
      that - source object
    • initCopy

      void initCopy(KeyListImpl<E> that)
      Initialize object for copy() operation.
      Parameters:
      that - source object
    • doClone

      protected void doClone(IList<E> that)
      Description copied from class: IList
      Initialize this object after the bitwise copy has been made by Object.clone().
      Specified by:
      doClone in class IList<E>
      Parameters:
      that - source object
    • asSet

      public Set<E> 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.
      Specified by:
      capacity in class IList<E>
      Returns:
      capacity of this list
    • size

      public int size()
      Description copied from interface: IListable
      Return size of list
      Specified by:
      size in interface Collection<E>
      Specified by:
      size in interface IListable<E>
      Specified by:
      size in interface List<E>
      Specified by:
      size in class IList<E>
    • get

      public E get(int index)
      Description copied from interface: IListable
      Return element at specified position
      Specified by:
      get in interface IListable<E>
      Specified by:
      get in interface List<E>
      Overrides:
      get in class IList<E>
    • doGet

      protected E doGet(int index)
      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.
      Specified by:
      doGet in class IList<E>
      Parameters:
      index - index of element to return
      Returns:
      the element at the specified position in this list
    • 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.
      Overrides:
      doGetAll in class IList<E>
      Type Parameters:
      T - type of elements stored in the list
      Parameters:
      array - array to store the list elements
      index - index of first element to copy
      len - number of elements to copy
    • contains

      public boolean contains(Object elem)

      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.

      Specified by:
      contains in interface Collection<E>
      Specified by:
      contains in interface List<E>
      Overrides:
      contains in class IList<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
    • remove

      public boolean remove(Object elem)

      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.

      Specified by:
      remove in interface Collection<E>
      Specified by:
      remove in interface List<E>
      Overrides:
      remove in class IList<E>
    • add

      public boolean add(E elem)

      Note that the behavior of the operation depends on the defined constraints.

      Specified by:
      add in interface Collection<E>
      Specified by:
      add in interface List<E>
      Overrides:
      add in class IList<E>
    • add

      public void add(int index, E elem)
      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.

      Specified by:
      add in interface List<E>
      Overrides:
      add in class IList<E>
      Parameters:
      index - index at which the specified element is to be inserted
      elem - element to be inserted
    • set

      public E set(int index, E elem)

      Note that the behavior of the operation depends on the defined constraints.

      Specified by:
      set in interface List<E>
      Overrides:
      set in class IList<E>
    • clear

      public void clear()
      Specified by:
      clear in interface Collection<E>
      Specified by:
      clear in interface List<E>
      Overrides:
      clear in class IList<E>
    • 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 class IList<E>
      Parameters:
      minCapacity - the desired minimum capacity
    • doAdd

      protected boolean doAdd(int index, E elem)
      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.
      Specified by:
      doAdd in class IList<E>
      Parameters:
      index - index where element should be added (-1 means it is up to the implementation to choose the index)
      elem - element to add
      Returns:
      true if element has been added, false otherwise
    • doSet

      protected E doSet(int index, E elem)
      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.
      Specified by:
      doSet in class IList<E>
      Parameters:
      index - index where element will be placed
      elem - element to set
      Returns:
      old element which was at the position
    • doRemove

      protected E doRemove(int index)
      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.
      Specified by:
      doRemove in class IList<E>
      Parameters:
      index - index of element to remove
      Returns:
      removed element
    • doRemoveAll

      protected void doRemoveAll(int index, int len)
      Description copied from class: IList
      Remove specified range of elements from list.
      Overrides:
      doRemoveAll in class IList<E>
      Parameters:
      index - index of first element to remove
      len - number of elements to remove
    • doReSet

      protected E doReSet(int index, E elem)
      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.
      Specified by:
      doReSet in class IList<E>
      Parameters:
      index - index where element will be placed
      elem - element to set
      Returns:
      old element which was at the position
    • indexOf

      public int indexOf(Object elem)

      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.

      Specified by:
      indexOf in interface List<E>
      Overrides:
      indexOf in class IList<E>
    • 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

      public int indexOfKey(int keyIndex, Object key)
      Find given key and return its index.
      Parameters:
      keyIndex - key index
      key - key to find
      Returns:
      index of key or -1 if not found
    • indexOfKey

      public int indexOfKey(int keyIndex, Object key, int start)
      Find given key and return its index.
      Parameters:
      keyIndex - key index
      key - key to find
      start - start index for search
      Returns:
      index of key or -1 if not found
    • containsKey

      public boolean containsKey(int keyIndex, Object 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
    • getKeyMapper

      public Function<E,Object> getKeyMapper(int keyIndex)
      Returns mapper for specified key map.
      Parameters:
      keyIndex - key index
      Returns:
      mapper for specified key map
    • getByKey

      public E getByKey(int keyIndex, Object key)
      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 index
      key - key to find
      Returns:
      value of specified key or null
    • getAllByKey

      public IList<E> getAllByKey(int keyIndex, Object key)
      Returns a list with all elements with the specified key.
      Parameters:
      keyIndex - key index
      key - key which elements must have
      Returns:
      list with all elements
    • getCountByKey

      public int getCountByKey(int keyIndex, 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
    • removeByKey

      protected E removeByKey(int keyIndex, Object key)
      Removes element by key. If there are duplicates, only one element is removed.
      Parameters:
      keyIndex - key index
      key - key of element to remove
      Returns:
      removed element or null if no element has been removed
    • putByKey

      protected E putByKey(int keyIndex, E elem, boolean replace)
      Put element by key into list.
      Parameters:
      keyIndex - key index
      elem - element to put
      replace - 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

      protected IList<E> removeAllByKey(int keyIndex, 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
    • getAllKeys

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

      public Set<?> getDistinctKeys(int keyIndex)
      Returns set containing all distinct keys.
      Parameters:
      keyIndex - key index
      Returns:
      list containing all distinct keys
    • binarySearch

      public <K> int binarySearch(int index, int len, K key, Comparator<? super K> comparator)
      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 class IList<E>
      Parameters:
      index - index of first element to search
      len - number of elements to search
      key - the value to be searched for
      comparator - 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

      public void sort(int index, int len, Comparator<? super E> comparator)
      Description copied from class: IList
      Sort specified elements in the list using the specified comparator.
      Specified by:
      sort in class IList<E>
      Parameters:
      index - index of first element to sort
      len - number of elements to sort
      comparator - comparator to use for sorting (null means the elements natural ordering should be used)
      See Also:
    • 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 class IList<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 class IList<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 class IList<E>
      Parameters:
      elem - element
      Returns:
      removed equal elements (never null)
    • getDistinct

      public Set<E> getDistinct()
      Description copied from class: IList
      Returns distinct elements in the list.
      Overrides:
      getDistinct in class IList<E>
      Returns:
      distinct elements in the list
    • 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:
       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

      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, Object oldKey, 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)
    • getDefaultElem

      protected E getDefaultElem()
      Specified by:
      getDefaultElem in class IList<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 class IList<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 class IList<E>
    • doCreate

      protected IList<E> doCreate(int capacity)
      Description copied from class: IList
      Create list with specified capacity.
      Specified by:
      doCreate in class IList<E>
      Parameters:
      capacity - initial capacity (use -1 for default capacity)
      Returns:
      created list