1 package prefuse.data.expression; 2 3 import java.util.Collections ; 4 import java.util.HashSet ; 5 import java.util.Set ; 6 7 13 public class ExpressionAnalyzer { 14 15 21 public static boolean hasDependency(Expression expr) { 22 ColumnCollector cc = new ColumnCollector(false); 23 expr.visit(cc); 24 return cc.getColumnCount() > 0; 25 } 26 27 32 public static Set getReferencedColumns(Expression expr) { 33 ColumnCollector cc = new ColumnCollector(true); 34 expr.visit(cc); 35 return cc.getColumnSet(); 36 } 37 38 42 private static class ColumnCollector implements ExpressionVisitor { 43 private boolean store; 44 private Set m_cols; 45 private int m_count; 46 47 public ColumnCollector(boolean store) { 48 this.store = store; 49 } 50 public int getColumnCount() { 51 return m_count; 52 } 53 public Set getColumnSet() { 54 if ( m_cols == null ) { 55 return Collections.EMPTY_SET; 56 } else { 57 return m_cols; 58 } 59 } 60 public void visitExpression(Expression expr) { 61 if ( expr instanceof ColumnExpression ) { 62 ++m_count; 63 if ( store ) { 64 String field = ((ColumnExpression)expr).getColumnName(); 65 if ( m_cols == null ) 66 m_cols = new HashSet (); 67 m_cols.add(field); 68 } 69 70 } 71 } 72 public void down() { 73 } 75 public void up() { 76 } 78 } 79 80 } | Popular Tags |