Class IncrementalList<A>

java.lang.Object
java.util.AbstractCollection<A>
java.util.AbstractList<A>
eu.bandm.tools.util.java.IncrementalList<A>
Type Parameters:
A - the type of list elements
All Implemented Interfaces:
Iterable<A>, Collection<A>, List<A>, SequencedCollection<A>

public class IncrementalList<A> extends AbstractList<A>
An immutable list that supports efficient persistent updates.

Persistent Updates

This list implementation is immutable. The methods AbstractList.add(E), AbstractList.remove(int), AbstractCollection.remove(Object) and AbstractList.set(int, E) all throw UnsupportedOperationException.

Instead, there are methods append(A) and getFront() that return a new list that contains one element more or less, respectively, leaving the original unchanged. The implementation ensures that these operations does not involve unnecessary copying.

Since:
1.3
  • Method Details

    • empty

      public static <A> IncrementalList<A> empty()
      Returns an empty list.
      Type Parameters:
      A - the type of list elements
      Returns:
      a list containing no elements
    • get

      @Opt public A get(int i)
      Specified by:
      get in interface List<A>
      Specified by:
      get in class AbstractList<A>
    • size

      public int size()
      Specified by:
      size in interface Collection<A>
      Specified by:
      size in interface List<A>
      Specified by:
      size in class AbstractCollection<A>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Collection<A>
      Specified by:
      isEmpty in interface List<A>
      Overrides:
      isEmpty in class AbstractCollection<A>
    • getFront

      public IncrementalList<A> getFront()
      Returns the sublist without the last element, if any.

      This operation is very efficient.

      Returns:
      a list that contains all but the last element; or the empty list if there are no elements
    • getLast

      @Opt public A getLast()
      Returns the last element.
      Throws:
      NoSuchElementException - if this list is empty.
    • of

      public static <A> IncrementalList<A> of(List<? extends A> elems)
      Returns an incremental list copy of the given list.
      Type Parameters:
      A - the type of list elements
      Parameters:
      elems - the elements to copy
      Returns:
      a new incremental list that contains each of elems
      Throws:
      NullPointerException - if elems is null
      See Also:
    • of

      @SafeVarargs public static <A> IncrementalList<A> of(@Opt A... elems)
      Returns a incremental list copy of the given array.
      Type Parameters:
      A - the type of list elements
      Parameters:
      elems - the elements to copy
      Returns:
      a new incremental list that contains each of elems
      Throws:
      NullPointerException - if elems is null
      See Also:
    • append

      public IncrementalList<A> append(@Opt A last)
      Returns an extension of this list by a given element.

      This operation is very efficient.

      Parameters:
      last - the element to append
      Returns:
      a list that contains all elements of this list followed by last
    • snapshot

      public List<A> snapshot()
      Returns a copy of this list.

      The returned list is not guaranteed to be either modifiable or unmodifiable. Element access can be expected to be more efficient on the copy than on this list. In particular, the returned list will implement RandomAccess.

      Returns:
      a copy of this list
      See Also: