// holding/NonCollectionSequence32.java // TIJ4 Chapter Holding, Exercise 32, page 437 /* Following the example of MultiIterableClass, add reversed() and randomized() * methods to NonCollectionSequence.java, as well as making NonCollectionSequence.java * implement Iterable and show that all the approaches * work in foreach statements. */ import typeinfo.pets.*; import java.util.*; import static org.greggordon.tools.Print.*; class PetSequence { protected Pet[] pets = Pets.createArray(8); } public class NonCollectionSequence32 extends PetSequence implements Iterable { public Iterator iterator() { return new Iterator() { private int index = 0; public boolean hasNext() { return index < pets.length; } public Pet next() { return pets[index++]; } public void remove() { throw new UnsupportedOperationException(); } }; } public Iterable reversed() { return new Iterable() { public Iterator iterator() { return new Iterator() { int current = pets.length - 1; public boolean hasNext() { return current > -1; } public Pet next() { return pets[current--]; } public void remove() { throw new UnsupportedOperationException(); } }; } }; } public Iterable randomized() { return new Iterable() { public Iterator iterator() { List shuffled = new ArrayList(Arrays.asList(pets)); Collections.shuffle(shuffled, new Random()); return shuffled.iterator(); } }; } public static void main(String[] args) { NonCollectionSequence32 nc = new NonCollectionSequence32(); print("pets: "); for(Pet p : nc.pets) print(p + " "); println(); print("reversed: "); for(Pet p : nc.reversed()) print(p + " "); println(); print("randomized: "); for(Pet p : nc.randomized()) print(p + " "); } }