/*********************************************** * CS2510 Fall 2011 * Lecture #21 * More Loops and Exceptions ***********************************************/ import java.util.ArrayList; import tester.*; class Traversal { private ArrayList list; private int index; Traversal(ArrayList list) { this.list = list; this.index = 0; } private Traversal(ArrayList list, int index) { this.list = list; this.index = index; } // Returns the current element in our Traversal T first() { return list.get(index); } // Increments our Traversal Traversal rest() { if (isEmpty()) { throw new TraversalException("Index out of bounds"); } else { return new Traversal(list,index + 1); } } // isEmpty: Are we at the end of our Traversal boolean isEmpty() { return (index >= list.size()); } } // Some algorithms class Algos { // Count elements that match the selector static int countIf(ArrayList list, ISelector t) { int count = 0; Traversal travis = new Traversal(list); while(! travis.isEmpty()) { if (t.select(travis.first())) { count = count + 1; } travis = travis.rest(); } return count; } // Count elements that match the selector static int countIfFor(ArrayList list, ISelector t) { int count = 0; for(Traversal travis = new Traversal(list); ! travis.isEmpty(); travis = travis.rest()) { if (t.select(travis.first())) { count = count + 1; } } return count; } // findFirst : Returns the first instance in the list which matches the selector static T findFirst(ArrayList list, ISelector t) { for(Traversal travis = new Traversal(list); ! travis.isEmpty(); travis = travis.rest()) { if (t.select(travis.first())) { return travis.first(); } } throw new RuntimeException("No such luck, no elements found."); } static ArrayList map(ArrayList list, IMap mapfn) { ArrayList retlist = new ArrayList(); for(Traversal travis = new Traversal(list); ! travis.isEmpty(); travis = travis.rest()) { retlist.add(mapfn.apply(travis.first())); } return retlist; } } class TraversalException extends RuntimeException { TraversalException(String e) { super(e); } } // Selects an element based on a predicate interface ISelector { // Is this element selected? public boolean select(T t); } interface IMap { public Y apply(X x); } class Cowboy { boolean hat; int numbullets; Cowboy(boolean hat, int numbullets) { this.hat = hat; this.numbullets = numbullets; } } // Selector for finding Cowboys with hats class HasHat implements ISelector{ // Does this Cowboy have a hat? public boolean select(Cowboy t) { return t.hat; } } //Selector for finding Cowboys with less than 10 bullets class Less10Bullets implements ISelector{ // Does this Cowboy have less than 10 bullets? public boolean select(Cowboy t) { return t.numbullets < 10; } } class LectureExamples { Cowboy jwayne = new Cowboy(true,10000); Cowboy rango = new Cowboy(false,1); Cowboy erskin = new Cowboy(true, 6); ArrayList clist = new ArrayList(); ArrayList elist = new ArrayList(); HasHat hh = new HasHat(); Less10Bullets bsel = new Less10Bullets(); void reset() { clist.clear(); clist.add(jwayne); clist.add(rango); clist.add(erskin); elist.clear(); } void testCountIf(Tester t) { reset(); t.checkExpect(hh.select(jwayne),true); t.checkExpect(hh.select(rango),false); t.checkExpect(hh.select(erskin),true); t.checkExpect(Algos.countIf(clist, hh),2); t.checkExpect(Algos.countIf(elist, hh),0); elist.add(rango); t.checkExpect(Algos.countIf(elist, hh),0); t.checkExpect(bsel.select(jwayne),false); t.checkExpect(bsel.select(rango),true); t.checkExpect(bsel.select(erskin),true); t.checkExpect(Algos.countIf(clist, bsel),2); t.checkExpect(Algos.countIf(elist, bsel),1); t.checkExpect(Algos.countIfFor(clist, hh),2); t.checkExpect(Algos.countIfFor(clist, bsel),2); } void testFindFirst(Tester t) { reset(); t.checkExpect(Algos.findFirst(clist,hh),jwayne); t.checkExpect(Algos.findFirst(clist,bsel),rango); try { Algos.findFirst(elist,hh); t.checkExpect(true,false); } catch (RuntimeException e) { t.checkExpect(true,true); } } }