/* Composition/Adaption Making an integer list from a generic list without using inheritance. NOTE: we define IntList using List of objects and must now let all the methods we wish to use "pass through" the class definition. In essence we have an instance variable of class List and then redefine/specialise the methods to operate on that instance variable. One advantage is we may restrict/limit the methods that may be applied to a specialised/adapted class. */ public class IntList { private List L; // an instance variable, a List! private List getList(){ return L; } // Four constructors follow public IntList() { L = new List(); } public IntList(int e) { L = new List (new Integer(e)); } public IntList(int e, IntList tail) { L = new List(new Integer(e), tail.getList()); } public IntList(List l) { L = l; } // Boy! Does this method make life easy. public IntList add(int e) { return new IntList(e,this); } public String toString() { return L.toString(); } // If I didn't have this I couldn't print! public int head() { return ((Integer)L.head()).intValue(); }/* get the head of the list L, cast it to Integer and then get it's integer value */ public IntList tail() { return new IntList(L.tail()); }/* deliver a new IntList that has a L that is the tail of the L. Note that this is a pattern that is repeated in reverse, delete, ... below */ public int cons() { return L.cons(); } public int length() { return L.length(); } public IntList reverse() throws ListException { return new IntList(L.reverse()); } public IntList insert(int e) { return new IntList(L.insert(new Integer(e), new IntComp())); }/* deliver a new IntList with e deleted Integer(e) makes an integer object, IntComp() constructs an integer comparator. */ public boolean isPresent(int e) { return L.isPresent(new IntPred(e)); }/* IntPred(e) constructs a new integer predicate that tests if something is the same as e */ public IntList delete(int e) { return new IntList(L.delete(new Integer(e))); } }