Class ImmutableList<T>

  • Type Parameters:
    T - the type of the elements in the list
    All Implemented Interfaces:
    java.lang.Iterable<T>

    public abstract class ImmutableList<T>
    extends java.lang.Object
    implements java.lang.Iterable<T>
    An immutable list implementation that only supports sequential traversal using an iterator, prepending an item to the start, and extraction of the head()/tail() of the list. It is optimized for sequential access rather than direct access.
    • Constructor Summary

      Constructors 
      Constructor Description
      ImmutableList()  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> ImmutableList<T> empty()
      Return an empty list
      boolean equals​(java.lang.Object o)
      Test whether two lists are equal
      abstract T head()
      Get the first item in the list
      abstract boolean isEmpty()
      Ask whether the list is empty
      java.util.Iterator<T> iterator()
      Get an iterator over the elements of the list
      ImmutableList<T> prepend​(T element)
      Return a list with a new item added at the start
      ImmutableList<T> reverse()
      Return a list with the contents of this list in reverse order
      int size()
      Get the size of the list (the number of items).
      abstract ImmutableList<T> tail()
      Get all items in the list other than the first
      java.lang.String toString()
      Return a string representation of the list contents
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Constructor Detail

      • ImmutableList

        public ImmutableList()
    • Method Detail

      • empty

        public static <T> ImmutableList<T> empty()
        Return an empty list
        Type Parameters:
        T - the nominal item type of the list elements
        Returns:
        an empty list
      • head

        public abstract T head()
        Get the first item in the list
        Returns:
        the first item in the list
      • tail

        public abstract ImmutableList<T> tail()
        Get all items in the list other than the first
        Returns:
        a list containing all items except the first
      • isEmpty

        public abstract boolean isEmpty()
        Ask whether the list is empty
        Returns:
        true if and only if the list contains no items
      • size

        public final int size()
        Get the size of the list (the number of items). Note that this is an O(n) operation.
        Returns:
        the size of the list.
      • prepend

        public ImmutableList<T> prepend​(T element)
        Return a list with a new item added at the start
        Parameters:
        element - the item to be added at the start
        Returns:
        a new list
      • equals

        public boolean equals​(java.lang.Object o)
        Test whether two lists are equal
        Overrides:
        equals in class java.lang.Object
        Parameters:
        o - the other list
        Returns:
        true if the other object is an instance of this ImmutableList class, and the elements of the two lists are pairwise equal.
      • iterator

        public java.util.Iterator<T> iterator()
        Get an iterator over the elements of the list
        Specified by:
        iterator in interface java.lang.Iterable<T>
        Returns:
        an iterator over the list
      • toString

        public java.lang.String toString()
        Return a string representation of the list contents
        Overrides:
        toString in class java.lang.Object
        Returns:
        a string in the form "[item1, item2, ...]"
      • reverse

        public ImmutableList<T> reverse()
        Return a list with the contents of this list in reverse order
        Returns:
        the reversed list