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:
java.lang.Iterable<E>
public class ImmutableList<E>
extends java.lang.Object
implements java.lang.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
Fields Modifier and Type Field Description E
first
The first (most recently added) element in the listImmutableList<E>
rest
The other elements in the list (elements 2 to n) -
Constructor Summary
Constructors Constructor Description ImmutableList()
Constructs a new, empty immutable list. -
Method Summary
Modifier and Type Method Description ImmutableList<E>
add(E element)
Returns a new list with the given element added as the first element.boolean
contains(java.lang.Object element)
Indicates whether or not the list contains a given element.boolean
equals(java.lang.Object other)
int
hashCode()
java.util.Iterator<E>
iterator()
ImmutableList<E>
remove(java.lang.Object element)
Returns a list with the first occurrence of the given element removed.int
size()
Returns the number of elements in the list.
-
Field Details
-
Constructor Details
-
ImmutableList
public ImmutableList()Constructs a new, empty immutable list.
-
-
Method Details
-
equals
public boolean equals(java.lang.Object other)- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()- Overrides:
hashCode
in classjava.lang.Object
-
iterator
- Specified by:
iterator
in interfacejava.lang.Iterable<E>
-
size
public int size()Returns the number of elements in the list.- Returns:
- the number of elements
-
contains
public boolean contains(java.lang.Object element)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
-