Class ArrayDeque<T>

java.lang.Object
org.javimmutable.collections.deque.ArrayDeque<T>
All Implemented Interfaces:
Serializable, Iterable<T>, ICollection<T>, IDeque<T>, Indexed<T>, InvariantCheckable, IStreamable<T>, SplitableIterable<T>

public class ArrayDeque<T> extends Object implements IDeque<T>
IDeque implementation using 32-way trees. The underlying trees only allow values to be inserted or deleted from the head or tail.
See Also:
  • Method Details

    • of

      @Nonnull public static <T> ArrayDeque<T> of()
    • of

      @Nonnull public static <T> IDeque<T> of(Indexed<? extends T> source)
    • builder

      @Nonnull public static <T> ArrayDeque.Builder<T> builder()
    • collector

      @Nonnull public static <T> java.util.stream.Collector<T,?,IDeque<T>> collector()
    • size

      public int size()
      Description copied from interface: Indexed
      Retrieve the number of values available in the container.
      Specified by:
      size in interface ICollection<T>
      Specified by:
      size in interface Indexed<T>
      Returns:
      number of values in the collection
    • get

      public T get(int index)
      Description copied from interface: IDeque
      Retrieves the value at the specified index (which must be within the bounds of the list).
      Specified by:
      get in interface IDeque<T>
      Specified by:
      get in interface Indexed<T>
    • find

      @Nonnull public Maybe<T> find(int index)
      Description copied from interface: Indexed
      Retrieves a Holder containing the (possibly null) value at the specified index if it exists. If no such value exists returns an empty Holder.
      Specified by:
      find in interface Indexed<T>
    • assign

      @Nonnull public ArrayDeque<T> assign(int index, @Nullable T value)
      Description copied from interface: IDeque
      Replaces the value at the specified index (which must be within current bounds of the list) with the new value.
      Specified by:
      assign in interface IDeque<T>
    • insert

      @Nonnull public ArrayDeque<T> insert(@Nullable T value)
      Description copied from interface: IDeque
      Adds a value to the end of the list. May be invoked on an empty list.
      Specified by:
      insert in interface ICollection<T>
      Specified by:
      insert in interface IDeque<T>
    • select

      @Nonnull public ArrayDeque<T> select(@Nonnull java.util.function.Predicate<T> predicate)
      Description copied from interface: IDeque
      Returns a list of the same type as this containing only those elements for which predicate returns true. Implementations are optimized assuming predicate will return false more often than true.
      Specified by:
      select in interface IDeque<T>
      Parameters:
      predicate - decides whether to include an element
      Returns:
      list of same type as this containing only those elements for which predicate returns true
    • reject

      @Nonnull public ArrayDeque<T> reject(@Nonnull java.util.function.Predicate<T> predicate)
      Description copied from interface: IDeque
      Returns a list of the same type as this containing all those elements for which predicate returns false. Implementations can be optimized assuming predicate will return false more often than true.
      Specified by:
      reject in interface IDeque<T>
      Parameters:
      predicate - decides whether to include an element
      Returns:
      list of same type as this containing only those elements for which predicate returns false
    • insertFirst

      @Nonnull public ArrayDeque<T> insertFirst(@Nullable T value)
      Description copied from interface: IDeque
      Adds a value to the front of the list. May be invoked on an empty list. Synonym for insert()
      Specified by:
      insertFirst in interface IDeque<T>
    • insertLast

      @Nonnull public ArrayDeque<T> insertLast(@Nullable T value)
      Description copied from interface: IDeque
      Adds a value to the end of the list. May be invoked on an empty list. Synonym for insert().
      Specified by:
      insertLast in interface IDeque<T>
    • insertAll

      @Nonnull public ArrayDeque<T> insertAll(@Nonnull Iterable<? extends T> values)
      Description copied from interface: IDeque
      Adds the values to the end of the list in the same order they appear in the Iterable. May be invoked on an empty list.
      Specified by:
      insertAll in interface ICollection<T>
      Specified by:
      insertAll in interface IDeque<T>
      Returns:
      instance of list containing the collection
    • insertAll

      @Nonnull public ArrayDeque<T> insertAll(@Nonnull Iterator<? extends T> values)
      Description copied from interface: IDeque
      Adds the values to the end of the list in the same order they appear in the Iterable. May be invoked on an empty list.
      Specified by:
      insertAll in interface ICollection<T>
      Specified by:
      insertAll in interface IDeque<T>
      Returns:
      instance of list containing the collection
    • insertAllFirst

      @Nonnull public ArrayDeque<T> insertAllFirst(@Nonnull Iterable<? extends T> values)
      Description copied from interface: IDeque
      Adds the values to the beginning of the list in the same order they appear in the Iterable. May be invoked on an empty list.
      Specified by:
      insertAllFirst in interface IDeque<T>
      Returns:
      instance of list containing the collection
    • insertAllFirst

      @Nonnull public ArrayDeque<T> insertAllFirst(@Nonnull Iterator<? extends T> values)
      Description copied from interface: IDeque
      Adds the values to the beginning of the list in the same order they appear in the Iterable. May be invoked on an empty list.
      Specified by:
      insertAllFirst in interface IDeque<T>
      Returns:
      instance of list containing the collection
    • insertAllLast

      @Nonnull public ArrayDeque<T> insertAllLast(@Nonnull Iterable<? extends T> values)
      Description copied from interface: IDeque
      Adds the values to the end of the list in the same order they appear in the Iterable. May be invoked on an empty list. Synonym for insertAll()
      Specified by:
      insertAllLast in interface IDeque<T>
      Returns:
      instance of list containing the collection
    • insertAllLast

      @Nonnull public ArrayDeque<T> insertAllLast(@Nonnull Iterator<? extends T> values)
      Description copied from interface: IDeque
      Adds the values to the end of the list in the same order they appear in the Iterable. May be invoked on an empty list. Synonym for insertAll()
      Specified by:
      insertAllLast in interface IDeque<T>
      Returns:
      instance of list containing the collection
    • deleteFirst

      @Nonnull public ArrayDeque<T> deleteFirst()
      Description copied from interface: IDeque
      Removes the first value from the list and reduces size by 1. size() must be greater than zero
      Specified by:
      deleteFirst in interface IDeque<T>
      Returns:
      new IList without last value
    • deleteLast

      @Nonnull public ArrayDeque<T> deleteLast()
      Description copied from interface: IDeque
      Removes the last value from the list and reduces size by 1. size() must be greater than zero
      Specified by:
      deleteLast in interface IDeque<T>
      Returns:
      new IList without last value
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface ICollection<T>
      Returns:
      true only if collection contains no values
    • isNonEmpty

      public boolean isNonEmpty()
      Specified by:
      isNonEmpty in interface ICollection<T>
      Returns:
      false only if collection contains no values
    • deleteAll

      @Nonnull public ArrayDeque<T> deleteAll()
      Specified by:
      deleteAll in interface ICollection<T>
      Specified by:
      deleteAll in interface IDeque<T>
      Returns:
      an equivalent collection with no values
    • getList

      @Nonnull public List<T> getList()
      Description copied from interface: IDeque
      Returns an unmodifiable List implementation backed by this list.
      Specified by:
      getList in interface IDeque<T>
    • reverse

      @Nonnull public IDeque<T> reverse()
      Description copied from interface: IDeque
      Returns a list containing the same elements as this list but with their order reversed so that first in this list is last in returned list etc.
      Specified by:
      reverse in interface IDeque<T>
    • iterator

      @Nonnull public SplitableIterator<T> iterator()
      Description copied from interface: IStreamable
      Overridden here to require implementations to return a SplitableIterator rather than a basic Iterator. This is necessary to allow composition of new objects from methods like keys() and values().
      Specified by:
      iterator in interface IStreamable<T>
      Specified by:
      iterator in interface Iterable<T>
      Specified by:
      iterator in interface SplitableIterable<T>
    • transform

      public <A> ArrayDeque<A> transform(@Nonnull Func1<T,A> transform)
      Description copied from interface: IDeque
      Apply the transform function to all elements in iterator order and add each transformed value to a new collection of this type.
      Specified by:
      transform in interface IDeque<T>
      Parameters:
      transform - transformation applied to each element
      Returns:
      the collection after all elements have been processed
    • transformSome

      public <A> ArrayDeque<A> transformSome(@Nonnull Func1<T,Maybe<A>> transform)
      Description copied from interface: IDeque
      Apply the transform function to all elements in iterator order and add the contents of non-empty Holders to a new collection of this type.
      Specified by:
      transformSome in interface IDeque<T>
      Parameters:
      transform - transformation applied to each element
      Returns:
      the collection after all elements have been processed
    • prefix

      @Nonnull public IDeque<T> prefix(int limit)
      Description copied from interface: IDeque
      Return the (possibly empty) list containing the first limit values.
      Specified by:
      prefix in interface IDeque<T>
      Specified by:
      prefix in interface Indexed<T>
      Parameters:
      limit - last index (exclusive) of values to include
      Returns:
      a possibly empty list containing the values
    • suffix

      @Nonnull public IDeque<T> suffix(int offset)
      Description copied from interface: IDeque
      Return the (possibly empty) list containing the values starting at offset (inclusive) and including all remaining items.
      Specified by:
      suffix in interface IDeque<T>
      Parameters:
      offset - first index (inclusive) of values to include
      Returns:
      a possibly empty list containing the values
    • middle

      @Nonnull public IDeque<T> middle(int offset, int limit)
      Description copied from interface: IDeque
      Return the (possibly empty) list containing the values starting at offset (inclusive) and including all remaining items up to but excluding the value at index limit.
      Specified by:
      middle in interface IDeque<T>
      Parameters:
      offset - first index (inclusive) of values to include
      limit - last index (exclusive) of values to include
      Returns:
      a possibly empty list containing the values
    • getSpliteratorCharacteristics

      public int getSpliteratorCharacteristics()
      Specified by:
      getSpliteratorCharacteristics in interface IStreamable<T>
      Returns:
      characteristics value used when creating Spliterators
    • checkInvariants

      public void checkInvariants()
      Description copied from interface: InvariantCheckable
      Checks invariants of implementing class.
      Specified by:
      checkInvariants in interface InvariantCheckable
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object