1 package org.jbpm.bpel.exe; 2 3 import java.util.Iterator ; 4 import java.util.Map ; 5 import java.util.NoSuchElementException ; 6 import java.util.Stack ; 7 8 import org.jbpm.graph.exe.Token; 9 10 14 public abstract class InstanceIterator implements Iterator { 15 private Stack tokensToTraverse = new Stack (); 16 private Object currentInstance; 17 private InstanceFilter filter; 18 19 public InstanceIterator(Token parent, InstanceFilter filter) { 20 Map children = parent.getChildren(); 21 if(children != null && !children.isEmpty()) { 22 tokensToTraverse.addAll(children.values()); 23 this.filter = filter; 24 findNextInstance(); 25 } 26 } 27 28 29 public void remove() { 30 currentInstance = null; 31 findNextInstance(); 32 } 33 34 35 public boolean hasNext() { 36 return currentInstance != null; 37 } 38 39 40 public Object next() { 41 if (!hasNext()) { 42 throw new NoSuchElementException (); 43 } 44 45 Object instanceToReturn = currentInstance; 46 findNextInstance(); 47 return instanceToReturn; 48 } 49 50 public abstract Object getInstance(Token token); 51 52 private void findNextInstance() { 53 currentInstance = null; 54 55 do { 56 Object 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 (); 70 } 71 72 Token nextVertex = (Token) tokensToTraverse.pop(); 73 addChildrenOf(nextVertex); 74 return nextVertex; 75 } 76 77 private void addChildrenOf(Token nextVertex) { 78 Map 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 instance); 86 } 87 } 88 | Popular Tags |