KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > bpel > exe > InstanceIterator


1 package org.jbpm.bpel.exe;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.Map JavaDoc;
5 import java.util.NoSuchElementException JavaDoc;
6 import java.util.Stack JavaDoc;
7
8 import org.jbpm.graph.exe.Token;
9
10 /**
11  * @author Juan Cantu
12  * @version $Revision: 1.2 $ $Date: 2005/05/31 00:49:53 $
13  */

14 public abstract class InstanceIterator implements Iterator JavaDoc {
15   private Stack JavaDoc tokensToTraverse = new Stack JavaDoc();
16   private Object JavaDoc currentInstance;
17   private InstanceFilter filter;
18   
19   public InstanceIterator(Token parent, InstanceFilter filter) {
20     Map JavaDoc children = parent.getChildren();
21     if(children != null && !children.isEmpty()) {
22       tokensToTraverse.addAll(children.values());
23       this.filter = filter;
24       findNextInstance();
25     }
26   }
27   
28   /**{@inheritDoc}*/
29   public void remove() {
30     currentInstance = null;
31     findNextInstance();
32   }
33
34   /**{@inheritDoc}*/
35   public boolean hasNext() {
36     return currentInstance != null;
37   }
38
39   /**{@inheritDoc}*/
40   public Object JavaDoc next() {
41     if (!hasNext()) {
42       throw new NoSuchElementException JavaDoc();
43     }
44     
45     Object JavaDoc instanceToReturn = currentInstance;
46     findNextInstance();
47     return instanceToReturn;
48   }
49
50   public abstract Object JavaDoc getInstance(Token token);
51   
52   private void findNextInstance() {
53     currentInstance = null;
54     
55     do {
56       Object JavaDoc anInstance = hasNextToken() ? getInstance(nextToken()) : null;
57       if( anInstance != null && (filter == null || filter.evaluate(anInstance))) {
58         currentInstance = anInstance;
59       }
60     }while( currentInstance == null && hasNextToken() );
61   }
62   
63   private boolean hasNextToken() {
64     return !tokensToTraverse.isEmpty();
65   }
66
67   private Token nextToken() {
68     if (!hasNextToken()) {
69       throw new NoSuchElementException JavaDoc();
70     }
71
72     Token nextVertex = (Token) tokensToTraverse.pop();
73     addChildrenOf(nextVertex);
74     return nextVertex;
75   }
76
77   private void addChildrenOf(Token nextVertex) {
78     Map JavaDoc children = nextVertex.getChildren();
79     if(getInstance(nextVertex) == null && children != null)
80       tokensToTraverse.addAll(children.values());
81   }
82   
83  static abstract class InstanceFilter {
84      
85     public abstract boolean evaluate(Object JavaDoc instance);
86   }
87 }
88
Popular Tags