import java.util.NoSuchElementException; /* An instance of this class represents an empty recursive list. ** ** Author: R. McCloskey, November 2019 */ public class RecursiveListEmpty implements RecursiveList { // static variable (to implement singleton pattern) // --------------- private static RecursiveListEmpty loner; // constructor // ----------- public RecursiveListEmpty() { } //private RecursiveListEmpty() { } // to force singleton // observers // --------- public boolean isEmpty() { return true; } public T headOf() { throw new NoSuchElementException("Empty list has no head"); } public RecursiveList tailOf() { throw new NoSuchElementException("Empty list has no tail"); } public String toString() { return "[]"; } // generators // ---------- public RecursiveList cons(T item) { return new RecursiveListNonEmpty(item, this); } public RecursiveList emptyList() { return this; } public static RecursiveList emptyListFactory() { if (loner == null) { loner = new RecursiveListEmpty(); } return loner; } }