Package com.strobel.collections
Class ImmutableList<A>
- java.lang.Object
-
- java.util.AbstractCollection<A>
-
- com.strobel.collections.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 Objectint
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
-
-
-
-
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_LIST
private static final ImmutableList<?> EMPTY_LIST
-
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; ifnull
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?
-
nonEmpty
public boolean nonEmpty()
Does list have elements?
-
length
public int length()
Return the number of elements in this list.
-
size
public int size()
-
setTail
public ImmutableList<A> setTail(ImmutableList<A> tail)
-
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(ImmutableList<A> x)
Append given list 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.
-
toArray
@NotNull public java.lang.Object[] toArray()
-
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 classjava.util.AbstractCollection<A>
-
hashCode
public int hashCode()
Compute a hash code, overrides Object
-
equals
public boolean equals(java.lang.Object other)
Is this list the same as other list?
-
equals
public static boolean equals(ImmutableList<?> xs, ImmutableList<?> ys)
Are the two lists the same?
-
contains
public boolean contains(java.lang.Object x)
Does the list contain the specified element?
-
last
public A last()
The last element in the list, if any, or null.
-
convert
public static <T> ImmutableList<T> convert(java.lang.Class<T> type, ImmutableList<?> list)
-
emptyIterator
private static <A> java.util.Iterator<A> emptyIterator()
-
addAll
public boolean addAll(int index, @NotNull java.util.Collection<? extends A> c)
- Specified by:
addAll
in interfacejava.util.List<A>
-
indexOf
public int indexOf(java.lang.Object o)
- Specified by:
indexOf
in interfacejava.util.List<A>
-
lastIndexOf
public int lastIndexOf(java.lang.Object o)
- Specified by:
lastIndexOf
in interfacejava.util.List<A>
-
listIterator
@NotNull public java.util.ListIterator<A> listIterator()
- Specified by:
listIterator
in interfacejava.util.List<A>
-
listIterator
@NotNull public java.util.ListIterator<A> listIterator(int index)
- Specified by:
listIterator
in interfacejava.util.List<A>
-
-