Package edu.uky.ai.util
Class ImmutableList<E>
java.lang.Object
edu.uky.ai.util.ImmutableList<E>
- Type Parameters:
E
- the type of object kept in the list
- All Implemented Interfaces:
Iterable<E>
A list of objects which cannot be modified. Methods which would normally
modify the list return new lists that reflect the modification without
changing the original list object.
- Author:
- Stephen G. Ware
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal E
The first (most recently added) element in the listfinal ImmutableList<E>
The other elements in the list (elements 2 to n) -
Constructor Summary
ConstructorsModifierConstructorDescriptionConstructs a new, empty immutable list.protected
ImmutableList
(E first, ImmutableList<E> rest) Constructs a new immutable list with a given first element and a given rest of the list. -
Method Summary
Modifier and TypeMethodDescriptionReturns a new list with the given element added as the first element.boolean
Indicates whether or not the list contains a given element.boolean
int
hashCode()
iterator()
Returns a list with the first occurrence of the given element removed.int
size()
Returns the number of elements in the list.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
first
The first (most recently added) element in the list -
rest
The other elements in the list (elements 2 to n)
-
-
Constructor Details
-
ImmutableList
Constructs a new immutable list with a given first element and a given rest of the list.- Parameters:
first
- the first element in the listrest
- the rest of the elements in the list
-
ImmutableList
public ImmutableList()Constructs a new, empty immutable list.
-
-
Method Details
-
equals
-
hashCode
public int hashCode() -
iterator
-
size
public int size()Returns the number of elements in the list.- Returns:
- the number of elements
-
contains
Indicates whether or not the list contains a given element.- Parameters:
element
- the element to search for- Returns:
- true if the list contains an object
Object.equals(Object)
to that element, false otherwise
-
add
Returns a new list with the given element added as the first element.- Parameters:
element
- the element to add to this list- Returns:
- a new list with that elements as the first element
-
remove
Returns a list with the first occurrence of the given element removed.- Parameters:
element
- the element to remove from this list- Returns:
- a new list with the first occurrence of that element removed, or the same list if that element was not found
-