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>

public class ImmutableList<E> extends Object implements 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 Details

    • first

      public final E first
      The first (most recently added) element in the list
    • rest

      public final ImmutableList<E> rest
      The other elements in the list (elements 2 to n)
  • Constructor Details

    • ImmutableList

      protected ImmutableList(E first, ImmutableList<E> rest)
      Constructs a new immutable list with a given first element and a given rest of the list.
      Parameters:
      first - the first element in the list
      rest - the rest of the elements in the list
    • ImmutableList

      public ImmutableList()
      Constructs a new, empty immutable list.
  • Method Details

    • equals

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • iterator

      public Iterator<E> iterator()
      Specified by:
      iterator in interface Iterable<E>
    • size

      public int size()
      Returns the number of elements in the list.
      Returns:
      the number of elements
    • contains

      public boolean contains(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

      public ImmutableList<E> add(E element)
      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

      public ImmutableList<E> remove(Object element)
      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