001package factory.factorymethod; 002import java.util.Iterator; 003import java.util.ArrayList; 004interface Triple<E> extends Iterable<E> { } 005class FieldTriple<E> implements Triple<E> { 006 private E one; E two; E three; 007 public FieldTriple(E one, E two, E three) { 008 this.one = one; this.two = two; this.three = three; 009 } 010 public Iterator<E> iterator() { return new TheIterator(); } 011 012 private class TheIterator implements Iterator<E> { 013 private int i; 014 public boolean hasNext() { return i < 3; } 015 public E next() { 016 i++; 017 switch (i) { 018 case 1: return one; 019 case 2: return two; 020 case 3: return three; 021 } 022 throw new java.util.NoSuchElementException(); 023 } 024 public void remove() { throw new UnsupportedOperationException(); } 025 } 026} 027// Arrays do not play nicely with generics, so use ArrayList 028class ArrayTriple<E> implements Triple<E> { 029 private ArrayList<E> a = new ArrayList<E>(); 030 public ArrayTriple(E one, E two, E three) { 031 a.add(0,one); a.add(1,two); a.add(2,three); 032 } 033 public Iterator<E> iterator() { return new TheIterator(); } 034 035 private class TheIterator implements Iterator<E> { 036 private int i = -1; 037 public boolean hasNext() { return i < 2; } 038 public E next() { 039 i++; 040 if (i <= 2) 041 return a.get(i); 042 else 043 throw new java.util.NoSuchElementException(); 044 } 045 public void remove() { throw new UnsupportedOperationException(); } 046 } 047}