Class ImmutableList<T>

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

    public abstract class ImmutableList<T>
    extends Object
    implements 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. Unlike net.sf.saxon.ma.parray.ImmList, it is optimized for sequential access rather than direct access.
    • 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​(Object o)
        Test whether two lists are equal
        Overrides:
        equals in class 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 Iterator<T> iterator()
        Get an iterator over the elements of the list
        Specified by:
        iterator in interface Iterable<T>
        Returns:
        an iterator over the list
      • toString

        public String toString()
        Return a string representation of the list contents
        Overrides:
        toString in class 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