1 4 package prefuse.util; 5 6 import prefuse.data.Tuple; 7 import prefuse.data.expression.Expression; 8 import prefuse.data.expression.IfExpression; 9 import prefuse.data.expression.ObjectLiteral; 10 import prefuse.data.expression.Predicate; 11 12 18 public class PredicateChain { 19 20 private Expression m_head = new ObjectLiteral(null); 21 private IfExpression m_tail = null; 22 23 28 public Expression getExpression() { 29 return m_head; 30 } 31 32 38 public Object get(Tuple t) { 39 return m_head.get(t); 40 } 41 42 48 public void add(Predicate p, Object val) { 49 if ( m_tail == null ) { 50 m_tail = new IfExpression(p, new ObjectLiteral(val), m_head); 51 m_head = m_tail; 52 } else { 53 IfExpression ie = new IfExpression(p, new ObjectLiteral(val), 54 m_tail.getElseExpression()); 55 m_tail.setElseExpression(ie); 56 m_tail = ie; 57 } 58 } 59 60 68 public boolean remove(Predicate p) { 69 if ( p == null ) return false; 70 71 IfExpression prev = null; 72 Expression expr = m_head; 73 while ( expr instanceof IfExpression ) { 74 IfExpression ifex = (IfExpression)expr; 75 Predicate test = (Predicate)ifex.getTestPredicate(); 76 if ( p.equals(test) ) { 77 Expression elseex = ifex.getElseExpression(); 78 ifex.setElseExpression(new ObjectLiteral(null)); 79 if ( prev != null ) { 80 prev.setElseExpression(elseex); 81 if ( ifex == m_tail ) 82 m_tail = prev; 83 } else { 84 m_head = elseex; 85 if ( ifex == m_tail ) 86 m_tail = null; 87 } 88 return true; 89 } else { 90 prev = ifex; 91 expr = ifex.getElseExpression(); 92 } 93 } 94 return false; 95 } 96 97 100 public void clear() { 101 m_head = new ObjectLiteral(null); 102 m_tail = null; 103 } 104 105 } | Popular Tags |