package list.visitor; import list.*; public class LastElement implements IListAlgo { public final static LastElement Singleton = new LastElement(); private LastElement() { } /** * Throws an IllegalArgumentException * @return does not return. */ public Object forEmpty(AList host, Object input) { throw new IllegalArgumentException ("Empty List has no last element."); } /** * Returns the last element of a list. */ public Object forNonEmpty(AList host, Object input) { if (host.getRest() == ListFactory.Singleton.makeEmptyList()) return host.getFirst(); else return host.getRest().execute(this, null); } }