Module hw06

Class NEList

java.lang.Object
provided.listFW.NEList
All Implemented Interfaces:
IList

public class NEList extends Object implements IList
Represents non-empty lists. Contains two pieces of data:
  • first: an Object representing the first data element
  • rest: an IList object representing the rest of this non-empty list.

When a class contains other objects that are isomorphic to itself, this class is called a composite.

Provides concrete code for
  • a constructor to initialize this NEList to a given first and rest,
  • the getFirst() method for accessing the first data element,
  • the getRest() method for accessing the rest of the list,
  • the execute method: simply calls the nonEmptyCase method of the IListAlgo parameter, passing to this method itself as the host and the given input Object as the input.
Author:
D. X. Nguyen, S. B. Wong
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private Object
    The first data element.
    private IList
    The rest or "tail" of this NEList.
  • Constructor Summary

    Constructors
    Constructor
    Description
    NEList​(Object f, IList r)
    Initializes this NEList to a given first and a given rest.
  • Method Summary

    Modifier and Type
    Method
    Description
    execute​(IListAlgo algo, Object... inp)
    Calls the nonEmptyCase method of the IListAlgo parameter, passing to this method itself as the host parameter and the given input as the input parameter.
    Returns the first data element of this NEList.
    Returns the first data element of this NEList.
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • _first

      private Object _first
      The first data element.
    • _rest

      private IList _rest
      The rest or "tail" of this NEList. Data Invariant: _rest != null;
  • Constructor Details

    • NEList

      public NEList(Object f, IList r)
      Initializes this NEList to a given first and a given rest.
      Parameters:
      f - the first element of this NEList.
      r - != null, the rest of this NEList.
  • Method Details

    • getFirst

      public final Object getFirst()
      Returns the first data element of this NEList. This method is marked as final to prevent all subclasses from overriding it. Finalizing a method also allows the compiler to generate more efficient calling code.
      Returns:
      The first data element of this list.
    • getRest

      public final IList getRest()
      Returns the first data element of this NEList. This method is marked as final to prevent all subclasses from overriding it. Finalizing a method also allows the compiler to generate more efficient calling code.
      Returns:
      The rest of this list.
    • execute

      public final Object execute(IListAlgo algo, Object... inp)
      Calls the nonEmptyCase method of the IListAlgo parameter, passing to this method itself as the host parameter and the given input as the input parameter. This method is marked as final to prevent all subclasses from overriding it. Finalizing a method also allows the compiler to generate more efficient calling code.
      Specified by:
      execute in interface IList
      Parameters:
      algo - an extrinsic operation (algorithm) on this IList.
      inp - a variable number of input parameters needed by algo to perform the computation.
      Returns:
      an Object representing the output of the computation.
    • toString

      public String toString()
      Overrides:
      toString in class Object