Created
November 9, 2014 05:47
-
-
Save sheimi/1894f3b0cbd2d4293cb7 to your computer and use it in GitHub Desktop.
code in blog.sheimi.me: 2012-06-20-software-architecture-review-4 (1)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Iter { | |
public static void main(String[] args) { | |
Aggregate<Integer, Integer, Integer> ag = new DataList<Integer, Integer, Integer>(); | |
((DataList)ag).add(new ThreeD<Integer, Integer, Integer>(1, 2, 3)); | |
((DataList)ag).add(new ThreeD<Integer, Integer, Integer>(1, 2, 3)); | |
((DataList)ag).add(new ThreeD<Integer, Integer, Integer>(1, 2, 3)); | |
Iterator<Integer> ia = ag.createIteratorA(); | |
while (ia.next() != null) { | |
System.out.println(ia.currentItem()); | |
} | |
Iterator<Integer> ib = ag.createIteratorB(); | |
while (ib.next() != null) { | |
System.out.println(ib.currentItem()); | |
} | |
Iterator<Integer> ic = ag.createIteratorC(); | |
while (ic.next() != null) { | |
System.out.println(ic.currentItem()); | |
} | |
} | |
} | |
class ThreeD<T, V, W> { | |
private T t; | |
private V v; | |
private W w; | |
public ThreeD(T t, V v, W w) { | |
this.t = t; | |
this.v = v; | |
this.w = w; | |
} | |
public T getA() { return t;} | |
public V getB() { return v;} | |
public W getC() { return w;} | |
} | |
interface Aggregate<T, V, W> { | |
public Iterator<T> createIteratorA(); | |
public Iterator<V> createIteratorB(); | |
public Iterator<W> createIteratorC(); | |
} | |
interface Iterator<T> { | |
public T first(); | |
public T next(); | |
public boolean isDone(); | |
public T currentItem(); | |
} | |
abstract class IteratorABC<T> implements Iterator<T>{ | |
protected DataList ag; | |
protected int i; | |
public IteratorABC(DataList ag) { | |
this.ag = ag; | |
i = -1; | |
} | |
public T first() { i = 0; return currentItem();} | |
public T next() {i += 1; return currentItem();} | |
public boolean isDone() {return i >= ag.size() ;} | |
public abstract T currentItem(); | |
} | |
class IteratorA<T> extends IteratorABC<T>{ | |
public IteratorA(DataList ag) { | |
super(ag); | |
} | |
public T currentItem() { if (isDone()) return null; return (T)ag.get(i).getA();} | |
} | |
class IteratorB<T> extends IteratorABC<T>{ | |
public IteratorB(DataList ag) { | |
super(ag); | |
} | |
public T currentItem() { if (isDone()) return null; return (T)ag.get(i).getB();} | |
} | |
class IteratorC<T> extends IteratorABC<T>{ | |
public IteratorC(DataList ag) { | |
super(ag); | |
} | |
public T currentItem() { if (isDone()) return null; return (T)ag.get(i).getC();} | |
} | |
class DataList<T, V, W> implements Aggregate<T, V, W> { | |
private ArrayList<ThreeD<T, V, W>> list = new ArrayList<ThreeD<T, V, W>>(); | |
private IteratorA<T> ia = new IteratorA<T>(this); | |
private IteratorB<V> ib = new IteratorB<V>(this); | |
private IteratorC<W> ic = new IteratorC<W>(this); | |
public void add(ThreeD<T, V, W> d) { list.add(d); } | |
public ThreeD<T, V, W> get(int index) { return list.get(index); } | |
public int size() {return list.size();} | |
public Iterator<T> createIteratorA() { return ia; } | |
public Iterator<V> createIteratorB() { return ib; } | |
public Iterator<W> createIteratorC() { return ic; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment