KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > data > expression > ExpressionAnalyzer


1 package prefuse.data.expression;
2
3 import java.util.Collections JavaDoc;
4 import java.util.HashSet JavaDoc;
5 import java.util.Set JavaDoc;
6
7 /**
8  * Library class that computes some simple analyses of an expression. Each
9  * analysis is computed using a visitor instance.
10  *
11  * @author <a HREF="http://jheer.org">jeffrey heer</a>
12  */

13 public class ExpressionAnalyzer {
14     
15     /**
16      * Determine if an expression has a dependency on a data field.
17      * @param expr the expression to analyze
18      * @return true if the expression has at least one tuple data
19      * field dependency.
20      */

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     /**
28      * Get the set of data fields the expression is dependent upon.
29      * @param expr the expression to analyze
30      * @return a set of all data field names the expression references
31      */

32     public static Set JavaDoc getReferencedColumns(Expression expr) {
33         ColumnCollector cc = new ColumnCollector(true);
34         expr.visit(cc);
35         return cc.getColumnSet();
36     }
37     
38     /**
39      * ExpressionVisitor that collects all referenced columns / data fields
40      * in an Expression.
41      */

42     private static class ColumnCollector implements ExpressionVisitor {
43         private boolean store;
44         private Set JavaDoc 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 JavaDoc 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 JavaDoc field = ((ColumnExpression)expr).getColumnName();
65                     if ( m_cols == null )
66                         m_cols = new HashSet JavaDoc();
67                     m_cols.add(field);
68                 }
69                 
70             }
71         }
72         public void down() {
73             // do nothing
74
}
75         public void up() {
76             // do nothing
77
}
78     }
79     
80 } // end of class ExpressionAnalyzer
81
Popular Tags