Class ImmutableList<A>

  • All Implemented Interfaces:
    java.lang.Iterable<A>, java.util.Collection<A>, java.util.List<A>

    public class ImmutableList<A>
    extends java.util.AbstractCollection<A>
    implements java.util.List<A>
    A class for generic linked lists. Links are supposed to be immutable, the only exception being the incremental construction of lists via ListBuffers. List is the main container class in GJC. Most data structures and algorithms in GJC use lists rather than arrays.

    Lists are always trailed by a sentinel element, whose head and tail are both null.

    This is NOT part of any supported API. If you write code that depends on this, you do so at your own risk. This code and its internal interfaces are subject to change or deletion without notice.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.util.Iterator<?> EMPTY_ITERATOR  
      private static ImmutableList<?> EMPTY_LIST  
      A head
      The first element of the list, supposed to be immutable.
      ImmutableList<A> tail
      The remainder of the list except for its first element, supposed to be immutable.
    • Constructor Summary

      Constructors 
      Constructor Description
      ImmutableList​(A head, ImmutableList<A> tail)
      Construct a list given its head and tail.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      void add​(int index, A element)  
      boolean addAll​(int index, java.util.Collection<? extends A> c)  
      ImmutableList<A> append​(A x)
      Append given element at length, forming and returning a new list.
      ImmutableList<A> appendList​(ImmutableList<A> x)
      Append given list at length, forming and returning a new list.
      ImmutableList<A> appendList​(ListBuffer<A> x)
      Append given list buffer at length, forming and returning a new list.
      boolean contains​(java.lang.Object x)
      Does the list contain the specified element?
      static <T> ImmutableList<T> convert​(java.lang.Class<T> type, ImmutableList<?> list)  
      static <A> ImmutableList<A> empty()
      Construct an empty list.
      private static <A> java.util.Iterator<A> emptyIterator()  
      static boolean equals​(ImmutableList<?> xs, ImmutableList<?> ys)
      Are the two lists the same?
      boolean equals​(java.lang.Object other)
      Is this list the same as other list?
      static <A> ImmutableList<A> fill​(int len, A init)
      Deprecated.
      static <A> ImmutableList<A> from​(A[] array)
      Construct a list consisting all elements of given array.
      A get​(int index)  
      int hashCode()
      Compute a hash code, overrides Object
      int indexOf​(java.lang.Object o)  
      boolean isEmpty()
      Does list have no elements?
      java.util.Iterator<A> iterator()  
      A last()
      The last element in the list, if any, or null.
      int lastIndexOf​(java.lang.Object o)  
      int length()
      Return the number of elements in this list.
      java.util.ListIterator<A> listIterator()  
      java.util.ListIterator<A> listIterator​(int index)  
      boolean nonEmpty()
      Does list have elements?
      static <A> ImmutableList<A> of​(A x1)
      Construct a list consisting of given element.
      static <A> ImmutableList<A> of​(A x1, A x2)
      Construct a list consisting of given elements.
      static <A> ImmutableList<A> of​(A x1, A... rest)
      Construct a list consisting of given elements.
      static <A> ImmutableList<A> of​(A x1, A x2, A x3)
      Construct a list consisting of given elements.
      static <A> ImmutableList<A> of​(A x1, A x2, A x3, A... rest)
      Construct a list consisting of given elements.
      ImmutableList<A> prepend​(A x)
      Prepend given element to front of list, forming and returning a new list.
      ImmutableList<A> prependList​(ImmutableList<A> xs)
      Prepend given list of elements to front of list, forming and returning a new list.
      A remove​(int index)  
      ImmutableList<A> reverse()
      Reverse list.
      A set​(int index, A element)  
      ImmutableList<A> setTail​(ImmutableList<A> tail)  
      int size()  
      java.util.List<A> subList​(int fromIndex, int toIndex)  
      java.lang.Object[] toArray()  
      <T> T[] toArray​(T[] vec)
      Copy successive elements of this list into given vector until list is exhausted or end of vector is reached.
      java.lang.String toString()
      Form a string listing all elements with comma as the separator character.
      java.lang.String toString​(java.lang.String sep)
      Form a string listing all elements with given separator character.
      • Methods inherited from class java.util.AbstractCollection

        add, addAll, clear, containsAll, remove, removeAll, retainAll
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        parallelStream, removeIf, stream, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
      • Methods inherited from interface java.util.List

        add, addAll, clear, containsAll, remove, removeAll, replaceAll, retainAll, sort, spliterator
    • Field Detail

      • head

        public A head
        The first element of the list, supposed to be immutable.
      • tail

        public ImmutableList<A> tail
        The remainder of the list except for its first element, supposed to be immutable.
      • EMPTY_ITERATOR

        private static final java.util.Iterator<?> EMPTY_ITERATOR
    • Constructor Detail

      • ImmutableList

        ImmutableList​(A head,
                      ImmutableList<A> tail)
        Construct a list given its head and tail.
    • Method Detail

      • empty

        public static <A> ImmutableList<A> empty()
        Construct an empty list.
      • of

        public static <A> ImmutableList<A> of​(A x1)
        Construct a list consisting of given element.
      • of

        @SafeVarargs
        public static <A> ImmutableList<A> of​(A x1,
                                              A... rest)
        Construct a list consisting of given elements.
      • of

        public static <A> ImmutableList<A> of​(A x1,
                                              A x2)
        Construct a list consisting of given elements.
      • of

        public static <A> ImmutableList<A> of​(A x1,
                                              A x2,
                                              A x3)
        Construct a list consisting of given elements.
      • of

        public static <A> ImmutableList<A> of​(A x1,
                                              A x2,
                                              A x3,
                                              A... rest)
        Construct a list consisting of given elements.
      • from

        public static <A> ImmutableList<A> from​(A[] array)
        Construct a list consisting all elements of given array.
        Parameters:
        array - an array; if null return an empty list
      • fill

        @Deprecated
        public static <A> ImmutableList<A> fill​(int len,
                                                A init)
        Deprecated.
        Construct a list consisting of a given number of identical elements.
        Parameters:
        len - The number of elements in the list.
        init - The value of each element.
      • isEmpty

        public boolean isEmpty()
        Does list have no elements?
        Specified by:
        isEmpty in interface java.util.Collection<A>
        Specified by:
        isEmpty in interface java.util.List<A>
        Overrides:
        isEmpty in class java.util.AbstractCollection<A>
      • nonEmpty

        public boolean nonEmpty()
        Does list have elements?
      • length

        public int length()
        Return the number of elements in this list.
      • size

        public int size()
        Specified by:
        size in interface java.util.Collection<A>
        Specified by:
        size in interface java.util.List<A>
        Specified by:
        size in class java.util.AbstractCollection<A>
      • prepend

        public ImmutableList<A> prepend​(A x)
        Prepend given element to front of list, forming and returning a new list.
      • prependList

        public ImmutableList<A> prependList​(ImmutableList<A> xs)
        Prepend given list of elements to front of list, forming and returning a new list.
      • reverse

        public ImmutableList<A> reverse()
        Reverse list. If the list is empty or a singleton, then the same list is returned. Otherwise a new list is formed.
      • append

        public ImmutableList<A> append​(A x)
        Append given element at length, forming and returning a new list.
      • appendList

        public ImmutableList<A> appendList​(ListBuffer<A> x)
        Append given list buffer at length, forming and returning a new list.
      • toArray

        @NotNull
        public <T> T[] toArray​(@NotNull
                               T[] vec)
        Copy successive elements of this list into given vector until list is exhausted or end of vector is reached.
        Specified by:
        toArray in interface java.util.Collection<A>
        Specified by:
        toArray in interface java.util.List<A>
        Overrides:
        toArray in class java.util.AbstractCollection<A>
      • toArray

        @NotNull
        public java.lang.Object[] toArray()
        Specified by:
        toArray in interface java.util.Collection<A>
        Specified by:
        toArray in interface java.util.List<A>
        Overrides:
        toArray in class java.util.AbstractCollection<A>
      • toString

        public java.lang.String toString​(java.lang.String sep)
        Form a string listing all elements with given separator character.
      • toString

        public java.lang.String toString()
        Form a string listing all elements with comma as the separator character.
        Overrides:
        toString in class java.util.AbstractCollection<A>
      • hashCode

        public int hashCode()
        Compute a hash code, overrides Object
        Specified by:
        hashCode in interface java.util.Collection<A>
        Specified by:
        hashCode in interface java.util.List<A>
        Overrides:
        hashCode in class java.lang.Object
        See Also:
        List.hashCode()
      • equals

        public boolean equals​(java.lang.Object other)
        Is this list the same as other list?
        Specified by:
        equals in interface java.util.Collection<A>
        Specified by:
        equals in interface java.util.List<A>
        Overrides:
        equals in class java.lang.Object
        See Also:
        List.equals(java.lang.Object)
      • contains

        public boolean contains​(java.lang.Object x)
        Does the list contain the specified element?
        Specified by:
        contains in interface java.util.Collection<A>
        Specified by:
        contains in interface java.util.List<A>
        Overrides:
        contains in class java.util.AbstractCollection<A>
      • last

        public A last()
        The last element in the list, if any, or null.
      • emptyIterator

        private static <A> java.util.Iterator<A> emptyIterator()
      • iterator

        @NotNull
        public java.util.Iterator<A> iterator()
        Specified by:
        iterator in interface java.util.Collection<A>
        Specified by:
        iterator in interface java.lang.Iterable<A>
        Specified by:
        iterator in interface java.util.List<A>
        Specified by:
        iterator in class java.util.AbstractCollection<A>
      • get

        public A get​(int index)
        Specified by:
        get in interface java.util.List<A>
      • addAll

        public boolean addAll​(int index,
                              @NotNull
                              java.util.Collection<? extends A> c)
        Specified by:
        addAll in interface java.util.List<A>
      • set

        public A set​(int index,
                     A element)
        Specified by:
        set in interface java.util.List<A>
      • add

        public void add​(int index,
                        A element)
        Specified by:
        add in interface java.util.List<A>
      • remove

        public A remove​(int index)
        Specified by:
        remove in interface java.util.List<A>
      • indexOf

        public int indexOf​(java.lang.Object o)
        Specified by:
        indexOf in interface java.util.List<A>
      • lastIndexOf

        public int lastIndexOf​(java.lang.Object o)
        Specified by:
        lastIndexOf in interface java.util.List<A>
      • listIterator

        @NotNull
        public java.util.ListIterator<A> listIterator()
        Specified by:
        listIterator in interface java.util.List<A>
      • listIterator

        @NotNull
        public java.util.ListIterator<A> listIterator​(int index)
        Specified by:
        listIterator in interface java.util.List<A>
      • subList

        @NotNull
        public java.util.List<A> subList​(int fromIndex,
                                         int toIndex)
        Specified by:
        subList in interface java.util.List<A>