1
2
3
4
5
6 package net.sf.beanrunner.factory.impl;
7
8
9 import java.util.Iterator;
10 import java.util.NoSuchElementException;
11
12 import net.sf.beanrunner.factory.InstanceFactory;
13
14 public class ChainedInstanceFactory
15 implements InstanceFactory
16 {
17 private static class ChainedIterator
18 implements Iterator
19 {
20
21 private void alignIndex()
22 {
23 for(; index < iters.length && !iters[index].hasNext(); index++);
24 }
25
26 public boolean hasNext()
27 {
28 boolean rtnval = false;
29 if(index < iters.length)
30 rtnval = iters[index].hasNext();
31 return rtnval;
32 }
33
34 public Object next()
35 {
36 Object rtnval;
37 if(index < iters.length)
38 {
39 rtnval = iters[index].next();
40 alignIndex();
41 } else
42 {
43 throw new NoSuchElementException();
44 }
45 return rtnval;
46 }
47
48 public void remove()
49 {
50 throw new UnsupportedOperationException();
51 }
52
53 private Iterator iters[];
54 private int index;
55
56 public ChainedIterator(InstanceFactory a, InstanceFactory b)
57 throws Exception
58 {
59 index = 0;
60 iters = new Iterator[2];
61 iters[0] = a.iterator();
62 iters[1] = b.iterator();
63 alignIndex();
64 }
65 }
66
67 private static class IteratorFactory
68 {
69
70 public Iterator iterator()
71 throws Exception
72 {
73 return new ChainedIterator(a, b);
74 }
75
76 private InstanceFactory a;
77 private InstanceFactory b;
78
79 public IteratorFactory(InstanceFactory first, InstanceFactory second)
80 {
81 a = first;
82 b = second;
83 }
84 }
85
86
87 public ChainedInstanceFactory(InstanceFactory first, InstanceFactory second)
88 {
89 factory = new IteratorFactory(first, second);
90 }
91
92 public Iterator iterator()
93 throws Exception
94 {
95 return factory.iterator();
96 }
97
98 private IteratorFactory factory;
99 }