Package eu.bandm.tools.util.java
Class RecursiveList<A>
java.lang.Object
java.util.AbstractCollection<A>
java.util.AbstractList<A>
java.util.AbstractSequentialList<A>
eu.bandm.tools.util.java.RecursiveList<A>
- Type Parameters:
A- the type of list elements
- All Implemented Interfaces:
Nullfree,Iterable<A>,Collection<A>,List<A>,SequencedCollection<A>
An immutable singly-linked list implementation.
This implementation is generally less efficient than an array-based implementation. However, it has two major advantages:
- Computing the rest of a list (containing all elements except the first one) executes in constant time and consumes no space.
- Rest lists can be shared.
All usual list operations operate on constant stack space, even though their algebraic definitions may be recursive.
Since this implementation used singly-linked list cells, the operation
ListIterator.previous() is significantly more costly that any other,
and should be avoided. No penalty is incurred for list iterators where this
method is never called.
This implementation does not support null elements.
- Since:
- 1.4
-
Field Summary
Fields inherited from class java.util.AbstractList
modCount -
Method Summary
Modifier and TypeMethodDescriptionstatic <A> RecursiveList<A> append(RecursiveList<? extends A> left, RecursiveList<? extends A> right) Returns a list containing all elements of the given two lists in sequence.static <A> RecursiveList<A> cons(A first, RecursiveList<? extends A> rest) Returns a list that contains the given first element and rest list.static <A> RecursiveList<A> empty()Returns an empty list.get(int index) abstract AgetFirst()Returns the first element of this list if non-empty.abstract RecursiveList<? extends A> getRest()Returns the rest list of this list if non-empty.listIterator(int index) static <A> RecursiveList<A> of(A... elems) Returns a list containing the given elements.static <A> RecursiveList<A> singleton(A elem) Returns a list containing just the given element.Methods inherited from class java.util.AbstractList
add, clear, equals, hashCode, indexOf, lastIndexOf, listIterator, removeRange, subListMethods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, size, toArray, toArray, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.List
addAll, addFirst, addLast, contains, containsAll, getLast, isEmpty, remove, removeAll, removeFirst, removeLast, replaceAll, retainAll, reversed, size, sort, spliterator, toArray, toArray
-
Method Details
-
getFirst
Returns the first element of this list if non-empty.- Specified by:
getFirstin interfaceList<A>- Specified by:
getFirstin interfaceSequencedCollection<A>- Returns:
- the first element of this non-empty list
- Throws:
NoSuchElementException- if this list is empty
-
getRest
Returns the rest list of this list if non-empty.- Returns:
- a list that contains all elements of this non-empty list except the first one
- Throws:
NoSuchElementException- if this list is empty
-
cons
Returns a list that contains the given first element and rest list.- Type Parameters:
A- the type of list elements- Parameters:
first- the first elementrest- the rest list- Returns:
- a non-empty list that contains the element
firstfollowed by all of the listrest - Throws:
NullPointerException- iffirstorrestis null
-
empty
Returns an empty list.- Type Parameters:
A- the type of list elements- Returns:
- a list that contains no elements
-
singleton
Returns a list containing just the given element.- Type Parameters:
A- the type of the element- Parameters:
elem- the single element- Returns:
- a list containing just the element
elem - Throws:
NullPointerException- ifelemis null
-
listIterator
- Specified by:
listIteratorin interfaceList<A>- Specified by:
listIteratorin classAbstractSequentialList<A>
-
get
-
of
Returns a list containing the given elements.The resulting list forever contains the elements of the given varargs array at the invocation time of this factory method. Subsequent changes to the array are not reflected.
- Type Parameters:
A- the type of list elements- Parameters:
elems- the elements of the new list- Returns:
- a list containing the given elements
- Throws:
NullPointerException- ifelemsis or contains null
-
append
public static <A> RecursiveList<A> append(RecursiveList<? extends A> left, RecursiveList<? extends A> right) Returns a list containing all elements of the given two lists in sequence.- Type Parameters:
A- the type of elements- Parameters:
left- one list of elementsright- another list of elements- Returns:
- a list that contains all elements of
leftfollowed by all elements ofright - Throws:
NullPointerException- ifleftorrightis null
-