Class GapList<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:
GapList.ReadOnlyList

public class GapList<E> extends IList<E>
Class {link GapList} combines the strengths of both ArrayList and LinkedList. It is implemented to offer both efficient random access to elements by index (as ArrayList does) and at the same time efficient adding and removing elements to and from beginning and end (as LinkedList does). It also exploits the locality of reference often seen in applications to further improve performance, e.g. for iterating over the list.

The class can be used as drop-in replacement for ArrayList. It is also source compatible to LinkedList/Deque as it implements all needed methods. It cannot implement Deque however, use IList.asDeque() to get a view implementing it.

Note that this implementation is not synchronized.

Note that the iterators provided are not fail-fast.

Author:
Thomas Mauch
See Also:
  • Field Details

    • DEFAULT_CAPACITY

      public static final int DEFAULT_CAPACITY
      Default capacity for list
      See Also:
  • Constructor Details

    • GapList

      protected GapList(boolean copy, GapList<E> that)
      Constructor used internally, e.g. for ImmutableGapList.
      Parameters:
      copy - true to copy all instance values from source, if false nothing is done
      that - list to copy
    • GapList

      public GapList()
      Construct a list with the default initial capacity.
    • GapList

      public GapList(int capacity)
      Construct a list with specified initial capacity.
      Parameters:
      capacity - capacity
    • GapList

      public GapList(Collection<? extends E> coll)
      Construct a list to contain the specified elements. The list will have an initial capacity to hold these elements.
      Parameters:
      coll - collection with elements
  • Method Details

    • EMPTY

      public static <EE> GapList<EE> EMPTY()
      Returns:
      unmodifiable empty instance
    • create

      public static <E> GapList<E> create()
      Create new list.
      Type Parameters:
      E - type of elements stored in the list
      Returns:
      created list
    • create

      public static <E> GapList<E> create(Collection<? extends E> coll)
      Create new list with specified elements.
      Type Parameters:
      E - type of elements stored in the list
      Parameters:
      coll - collection with elements
      Returns:
      created list
    • create

      @SafeVarargs public static <E> GapList<E> create(E... elems)
      Create new list with specified elements.
      Type Parameters:
      E - type of elements stored in the list
      Parameters:
      elems - array with elements
      Returns:
      created list
    • immutable

      public static <E> GapList<E> immutable(Collection<? extends E> coll)
      Create new immutable list with specified elements. To reduce the needed memory, the list's capacity will be equal to its size.
      Type Parameters:
      E - type of elements stored in the list
      Parameters:
      coll - collection with elements
      Returns:
      created list
    • immutable

      @SafeVarargs public static <E> GapList<E> immutable(E... elems)
      Create new immutable list with specified elements. To reduce the needed memory, the list's capacity will be equal to its size.
      Type Parameters:
      E - type of elements stored in the list
      Parameters:
      elems - array with elements
      Returns:
      created list
    • 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
    • getDefaultElem

      public E getDefaultElem()
      Specified by:
      getDefaultElem in class IList<E>
    • crop

      public GapList<E> crop()
      Description copied from class: IList
      Returns a copy this list but without elements. The new list will have the same type as this list.
      Specified by:
      crop in interface ICollection<E>
      Overrides:
      crop in class IList<E>
      Returns:
      an empty copy of this list
    • copy

      public GapList<E> 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.
      Specified by:
      copy in interface ICollection<E>
      Specified by:
      copy in class IList<E>
      Returns:
      a modifiable copy of this list
    • clone

      public GapList<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
    • 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 class IList<E>
    • unmodifiableList

      public GapList<E> 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. See IList.immutableList() to get an immutable copy of a list.
      Specified by:
      unmodifiableList in class IList<E>
      Returns:
      an unmodifiable view of the specified list
    • immutableList

      public GapList<E> 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. See IList.unmodifiableList() to get unmodifiable view of a list.
      Specified by:
      immutableList in class IList<E>
      Returns:
      an immutable copy of the specified list
    • 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
    • doClear

      protected void doClear()
      Overrides:
      doClear in class IList<E>
    • 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>
    • 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
    • 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
    • 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
    • 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
    • add

      public boolean add(E elem)
      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)
      Description copied from class: IList
      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).
      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
    • getAll

      public GapList<E> getAll(int index, int len)
      Description copied from class: IList
      Returns specified range of elements from list.
      Overrides:
      getAll in class IList<E>
      Parameters:
      index - index of first element to retrieve
      len - number of elements to retrieve
      Returns:
      list containing the specified range of elements
    • getAll

      public GapList<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
    • map

      public <R> GapList<R> map(Function<E,R> mapper)
      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>
      Overrides:
      map in class IList<E>
      Parameters:
      mapper - mapping function
      Returns:
      created list
    • 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
    • remove

      public E remove(int index)
      Specified by:
      remove in interface List<E>
      Overrides:
      remove in class IList<E>
    • 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
    • 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()
      Trims the capacity of this GapList instance to be the list's current size. An application can use this operation to minimize the storage of an instance.
      Specified by:
      trimToSize in class IList<E>
    • 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
    • doCreate

      public GapList<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
    • 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
    • doRemoveAllFast

      protected boolean doRemoveAllFast(int index, int len)
      Remove specified range of elements from list as specialized fast operation.
      Parameters:
      index - index of first element to remove
      len - number of elements to remove
      Returns:
      true if removal could be done, false otherwise
    • 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:
    • 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: