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 list
    ImmutableList<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.

    Methods inherited from class java.lang.Object

    getClass, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator
  • 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

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

    • equals

      public boolean equals​(java.lang.Object other)
      Overrides:
      equals in class java.lang.Object
    • hashCode

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

      public java.util.Iterator<E> iterator()
      Specified by:
      iterator in interface java.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

      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​(java.lang.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