Class TreeList<T>

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

@Immutable public class TreeList<T> extends Object implements IList<T>, Serializable
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
     
  • Method Summary

    Modifier and Type
    Method
    Description
    assign(int index, T value)
    Replaces the value at the specified index (which must be within current bounds of the list) with the new value.
    void
    Checks invariants of implementing class.
    static <T> java.util.stream.Collector<T,?,IList<T>>
     
    delete(int index)
    Delete value at index (which must be within the current bounds of the list).
     
    Removes the first value from the list and reduces size by 1.
    Removes the last value from the list and reduces size by 1.
    boolean
     
    find(int index)
    Retrieves a Holder containing the (possibly null) value at the specified index if it exists.
    void
    forEach(java.util.function.Consumer<? super T> action)
     
    <E extends Exception>
    void
    Processes every value using the provided function.
    get(int index)
    Retrieves the value at the specified index (which must be within the bounds of the list).
    Returns an unmodifiable List implementation backed by this list.
    int
     
    int
     
    insert(int index, T value)
    Insert value at index (which must be within 0 to size).
    insert(T value)
    Adds a value to the end of the list.
    insertAll(int index, Iterable<? extends T> values)
    Inserts all elements at index (which must be within 0 to size) in the same order they appear in the Iterable.
    insertAll(int index, Iterator<? extends T> values)
    Inserts all elements at index (which must be within 0 to size) in the same order they appear in the Iterable.
    insertAll(Iterable<? extends T> values)
    Adds the values to the end of the list in the same order they appear in the Iterable.
    insertAll(Iterator<? extends T> values)
    Adds the values to the end of the list in the same order they appear in the Iterable.
    insertAllFirst(Iterable<? extends T> values)
    Adds the values to the beginning of the list in the same order they appear in the Iterable.
    insertAllFirst(Iterator<? extends T> values)
    Adds the values to the beginning of the list in the same order they appear in the Iterable.
    insertAllLast(Iterable<? extends T> values)
    Adds the values to the end of the list in the same order they appear in the Iterable.
    insertAllLast(Iterator<? extends T> values)
    Adds the values to the end of the list in the same order they appear in the Iterable.
    insertFirst(T value)
    Adds a value to the front of the list.
    insertLast(T value)
    Adds a value to the end of the list.
    boolean
     
    boolean
     
    Overridden here to require implementations to return a SplitableIterator rather than a basic Iterator.
    static <T> TreeList.ListBuilder<T>
     
    middle(int offset, int limit)
    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.
    static <T> TreeList<T>
    of()
     
    static <T> TreeList<T>
    of(Iterator<? extends T> values)
     
    static <T> TreeList<T>
    of(Indexed<? extends T> values)
     
    static <T> TreeList<T>
    of(Indexed<? extends T> values, int offset, int limit)
     
    prefix(int limit)
    Return the (possibly empty) list containing the first limit values.
    <V> V
    reduce(V initialValue, Func2<V,T,V> accumulator)
    Apply the specified accumulator to all elements in iterator order calling the accumulator function for each element.
    <V, E extends Exception>
    V
    reduceThrows(V initialValue, Sum1Throws<T,V,E> accumulator)
     
    reject(java.util.function.Predicate<T> predicate)
    Returns a list of the same type as this containing all those elements for which predicate returns false.
    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.
    select(java.util.function.Predicate<T> predicate)
    Returns a list of the same type as this containing only those elements for which predicate returns true.
    int
    Retrieve the number of values available in the container.
    slice(int offset, int limit)
    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.
    suffix(int offset)
    Return the (possibly empty) list containing the values starting at offset (inclusive) and including all remaining items.
     
    <A> TreeList<A>
    transform(Func1<T,A> transform)
    Apply the transform function to all elements in iterator order and add each transformed value to a new collection of this type.
    <A> TreeList<A>
    transformSome(Func1<T,Maybe<A>> transform)
    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.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface org.javimmutable.collections.IDeque

    dequeCollector, single

    Methods inherited from interface org.javimmutable.collections.IList

    listCollector

    Methods inherited from interface org.javimmutable.collections.Indexed

    reversed, transformed

    Methods inherited from interface org.javimmutable.collections.IStreamable

    parallelStream, spliterator, stream

    Methods inherited from interface org.javimmutable.collections.SplitableIterable

    indexedForEach, indexedForEachThrows
  • Method Details

    • of

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

      @Nonnull public static <T> TreeList<T> of(@Nonnull Indexed<? extends T> values)
    • of

      @Nonnull public static <T> TreeList<T> of(@Nonnull Indexed<? extends T> values, int offset, int limit)
    • of

      @Nonnull public static <T> TreeList<T> of(@Nonnull Iterator<? extends T> values)
    • listBuilder

      @Nonnull public static <T> TreeList.ListBuilder<T> listBuilder()
    • createListCollector

      @Nonnull public static <T> java.util.stream.Collector<T,?,IList<T>> createListCollector()
    • assign

      @Nonnull public TreeList<T> assign(int index, @Nullable T value)
      Description copied from interface: IList
      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>
      Specified by:
      assign in interface IList<T>
    • insert

      @Nonnull public TreeList<T> insert(@Nullable T value)
      Description copied from interface: IList
      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>
      Specified by:
      insert in interface IList<T>
    • insert

      @Nonnull public TreeList<T> insert(int index, @Nullable T value)
      Description copied from interface: IList
      Insert value at index (which must be within 0 to size). Shifts all values at and after index one position to the right and adds 1 to size of the list.
      Specified by:
      insert in interface IList<T>
    • insertFirst

      @Nonnull public TreeList<T> insertFirst(@Nullable T value)
      Description copied from interface: IList
      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>
      Specified by:
      insertFirst in interface IList<T>
    • insertLast

      @Nonnull public TreeList<T> insertLast(@Nullable T value)
      Description copied from interface: IList
      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>
      Specified by:
      insertLast in interface IList<T>
    • insertAll

      @Nonnull public TreeList<T> insertAll(@Nonnull Iterable<? extends T> values)
      Description copied from interface: IList
      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>
      Specified by:
      insertAll in interface IList<T>
      Returns:
      instance of list containing the collection
    • insertAll

      @Nonnull public TreeList<T> insertAll(@Nonnull Iterator<? extends T> values)
      Description copied from interface: IList
      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>
      Specified by:
      insertAll in interface IList<T>
      Returns:
      instance of list containing the collection
    • insertAll

      @Nonnull public TreeList<T> insertAll(int index, @Nonnull Iterable<? extends T> values)
      Description copied from interface: IList
      Inserts all elements at index (which must be within 0 to size) in the same order they appear in the Iterable. Shifts all values at and after index x positions to the right and adds x to size of the list, where x is the number of elements being inserted.
      Specified by:
      insertAll in interface IList<T>
      Returns:
      instance of list containing the collection
    • insertAll

      @Nonnull public TreeList<T> insertAll(int index, @Nonnull Iterator<? extends T> values)
      Description copied from interface: IList
      Inserts all elements at index (which must be within 0 to size) in the same order they appear in the Iterable. Shifts all values at and after index x positions to the right and adds x to size of the list, where x is the number of elements being inserted.
      Specified by:
      insertAll in interface IList<T>
      Returns:
      instance of list containing the collection
    • insertAllFirst

      @Nonnull public TreeList<T> insertAllFirst(@Nonnull Iterable<? extends T> values)
      Description copied from interface: IList
      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>
      Specified by:
      insertAllFirst in interface IList<T>
      Returns:
      instance of list containing the collection
    • insertAllFirst

      @Nonnull public TreeList<T> insertAllFirst(@Nonnull Iterator<? extends T> values)
      Description copied from interface: IList
      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>
      Specified by:
      insertAllFirst in interface IList<T>
      Returns:
      instance of list containing the collection
    • insertAllLast

      @Nonnull public TreeList<T> insertAllLast(@Nonnull Iterable<? extends T> values)
      Description copied from interface: IList
      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>
      Specified by:
      insertAllLast in interface IList<T>
      Returns:
      instance of list containing the collection
    • insertAllLast

      @Nonnull public TreeList<T> insertAllLast(@Nonnull Iterator<? extends T> values)
      Description copied from interface: IList
      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>
      Specified by:
      insertAllLast in interface IList<T>
      Returns:
      instance of list containing the collection
    • deleteFirst

      @Nonnull public TreeList<T> deleteFirst()
      Description copied from interface: IList
      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>
      Specified by:
      deleteFirst in interface IList<T>
      Returns:
      new IList without last value
    • deleteLast

      @Nonnull public TreeList<T> deleteLast()
      Description copied from interface: IList
      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>
      Specified by:
      deleteLast in interface IList<T>
      Returns:
      new IList without last value
    • delete

      @Nonnull public TreeList<T> delete(int index)
      Description copied from interface: IList
      Delete value at index (which must be within the current bounds of the list). Shifts all values at and after index one position to the left and subtracts 1 from size of the list.
      Specified by:
      delete in interface IList<T>
    • deleteAll

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

      @Nonnull public IList<T> reverse()
      Description copied from interface: IList
      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>
      Specified by:
      reverse in interface IList<T>
    • transform

      public <A> TreeList<A> transform(@Nonnull Func1<T,A> transform)
      Description copied from interface: IList
      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>
      Specified by:
      transform in interface IList<T>
      Parameters:
      transform - transformation applied to each element
      Returns:
      the collection after all elements have been processed
    • transformSome

      public <A> TreeList<A> transformSome(@Nonnull Func1<T,Maybe<A>> transform)
      Description copied from interface: IList
      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>
      Specified by:
      transformSome in interface IList<T>
      Parameters:
      transform - transformation applied to each element
      Returns:
      the collection after all elements have been processed
    • 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>
    • 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
    • select

      @Nonnull public TreeList<T> select(@Nonnull java.util.function.Predicate<T> predicate)
      Description copied from interface: IList
      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>
      Specified by:
      select in interface IList<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 TreeList<T> reject(@Nonnull java.util.function.Predicate<T> predicate)
      Description copied from interface: IList
      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>
      Specified by:
      reject in interface IList<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
    • prefix

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

      @Nonnull public TreeList<T> middle(int offset, int limit)
      Description copied from interface: IList
      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>
      Specified by:
      middle in interface IList<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
    • slice

      @Nonnull public IList<T> slice(int offset, int limit)
      Description copied from interface: IList
      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. Similar to middle() but accepts a more permissive set of values. Negative values are interpreted relative to the end of the list and are inclusive when used as limit. This (-5,-1) would return the last 5 elements of the list. Bounds checking is removed and values past the end of the list are simply set to the end of the list. So (0,10000) for a list with five elements would be equivalent to (0,5).
      Specified by:
      slice in interface IList<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
    • 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>
    • checkInvariants

      public void checkInvariants()
      Description copied from interface: InvariantCheckable
      Checks invariants of implementing class.
      Specified by:
      checkInvariants in interface InvariantCheckable
    • 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>
    • getSpliteratorCharacteristics

      public int getSpliteratorCharacteristics()
      Specified by:
      getSpliteratorCharacteristics in interface IStreamable<T>
      Returns:
      characteristics value used when creating Spliterators
    • 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
    • forEach

      public void forEach(java.util.function.Consumer<? super T> action)
      Specified by:
      forEach in interface Iterable<T>
    • forEachThrows

      public <E extends Exception> void forEachThrows(@Nonnull Proc1Throws<T,E> proc) throws E
      Description copied from interface: SplitableIterable
      Processes every value using the provided function.
      Specified by:
      forEachThrows in interface SplitableIterable<T>
      Throws:
      E
    • reduce

      public <V> V reduce(V initialValue, Func2<V,T,V> accumulator)
      Description copied from interface: SplitableIterable
      Apply the specified accumulator to all elements in iterator order calling the accumulator function for each element. The first call to accumulator is passed initialValue and first element in the sequence. All remaining calls to accumulator are passed the result from the previous call and next element in the sequence.
      Specified by:
      reduce in interface SplitableIterable<T>
      Parameters:
      initialValue - value passed to accumulator on first call
      accumulator - method called to compute result
      Returns:
      result from last call to accumulator
    • reduceThrows

      public <V, E extends Exception> V reduceThrows(V initialValue, Sum1Throws<T,V,E> accumulator) throws E
      Specified by:
      reduceThrows in interface SplitableIterable<T>
      Throws:
      E