KickJava   Java API By Example, From Geeks To Geeks.

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


1 package prefuse.data.expression;
2
3 import java.util.HashMap JavaDoc;
4
5 import prefuse.visual.expression.GroupSizeFunction;
6 import prefuse.visual.expression.HoverPredicate;
7 import prefuse.visual.expression.InGroupPredicate;
8 import prefuse.visual.expression.QueryExpression;
9 import prefuse.visual.expression.SearchPredicate;
10 import prefuse.visual.expression.ValidatedPredicate;
11 import prefuse.visual.expression.VisiblePredicate;
12
13 /**
14  * Function table that allows lookup of registered FunctionExpressions
15  * by their function name.
16  *
17  * @author <a HREF="http://jheer.org">jeffrey heer</a>
18  */

19 public class FunctionTable {
20     
21     private FunctionTable() {
22         // prevent instantiation
23
}
24     
25     private static HashMap JavaDoc s_functionTable;
26     static {
27         s_functionTable = new HashMap JavaDoc();
28         // tuple functions
29
addFunction("ROW", RowFunction.class);
30         addFunction("ISNODE", IsNodeFunction.class);
31         addFunction("ISEDGE", IsEdgeFunction.class);
32         addFunction("DEGREE", DegreeFunction.class);
33         addFunction("INDEGREE", InDegreeFunction.class);
34         addFunction("OUTDEGREE", OutDegreeFunction.class);
35         addFunction("CHILDCOUNT", ChildCountFunction.class);
36         addFunction("TREEDEPTH", TreeDepthFunction.class);
37         
38         // numeric functions
39
addFunction("ABS", AbsFunction.class);
40         addFunction("ACOS", AcosFunction.class);
41         addFunction("ASIN", AsinFunction.class);
42         addFunction("ATAN", AtanFunction.class);
43         addFunction("ATAN2", Atan2Function.class);
44         addFunction("CEIL", CeilFunction.class);
45         addFunction("CEILING", CeilFunction.class);
46         addFunction("COS", CosFunction.class);
47         addFunction("COT", CotFunction.class);
48         addFunction("DEGREES", DegreesFunction.class);
49         addFunction("E", EFunction.class);
50         addFunction("EXP", ExpFunction.class);
51         addFunction("FLOOR", FloorFunction.class);
52         addFunction("LOG", LogFunction.class);
53         addFunction("LOG2", Log2Function.class);
54         addFunction("LOG10", Log10Function.class);
55         addFunction("MAX", MaxFunction.class);
56         addFunction("MIN", MaxFunction.class);
57         addFunction("MOD", MaxFunction.class);
58         addFunction("PI", PiFunction.class);
59         addFunction("POW", PowFunction.class);
60         addFunction("POWER", PowFunction.class);
61         addFunction("RADIANS", RadiansFunction.class);
62         addFunction("RAND", RandFunction.class);
63         addFunction("ROUND", RoundFunction.class);
64         addFunction("SIGN", SignFunction.class);
65         addFunction("SIN", SinFunction.class);
66         addFunction("SQRT", SqrtFunction.class);
67         addFunction("SUM", SumFunction.class);
68         addFunction("TAN", TanFunction.class);
69         
70         addFunction("SAFELOG10", SafeLog10Function.class);
71         addFunction("SAFESQRT", SafeSqrtFunction.class);
72         
73         // string functions
74
addFunction("CAP", CapFunction.class);
75         addFunction("CONCAT", ConcatFunction.class);
76         addFunction("CONCAT_WS", ConcatWsFunction.class);
77         addFunction("FORMAT", FormatFunction.class);
78         addFunction("INSERT", RPadFunction.class);
79         addFunction("LENGTH", LengthFunction.class);
80         addFunction("LOWER", LowerFunction.class);
81         addFunction("LCASE", LowerFunction.class);
82         addFunction("LEFT", LeftFunction.class);
83         addFunction("LPAD", LPadFunction.class);
84         addFunction("MID", SubstringFunction.class);
85         addFunction("POSITION", PositionFunction.class);
86         addFunction("REVERSE", ReverseFunction.class);
87         addFunction("REPEAT", RepeatFunction.class);
88         addFunction("REPLACE", ReplaceFunction.class);
89         addFunction("RIGHT", RightFunction.class);
90         addFunction("RPAD", RPadFunction.class);
91         addFunction("SPACE", SpaceFunction.class);
92         addFunction("SUBSTRING", SubstringFunction.class);
93         addFunction("UPPER", UpperFunction.class);
94         addFunction("UCASE", UpperFunction.class);
95         
96         // color functions
97
addFunction("RGB", RGBFunction.class);
98         addFunction("RGBA", RGBAFunction.class);
99         addFunction("GRAY", GrayFunction.class);
100         addFunction("HEX", HexFunction.class);
101         addFunction("HSB", HSBFunction.class);
102         addFunction("HSBA", HSBAFunction.class);
103         addFunction("COLORINTERP", ColorInterpFunction.class);
104         
105         // visualization functions
106
addFunction("GROUPSIZE", GroupSizeFunction.class);
107         addFunction("HOVER", HoverPredicate.class);
108         addFunction("INGROUP", InGroupPredicate.class);
109         addFunction("MATCH", SearchPredicate.class);
110         addFunction("QUERY", QueryExpression.class);
111         addFunction("VISIBLE", VisiblePredicate.class);
112         addFunction("VALIDATED", ValidatedPredicate.class);
113     }
114     
115     /**
116      * Indicates if a function of the given name is included in the function
117      * table.
118      * @param name the function name
119      * @return true if the function is in the table, false otherwise
120      */

121     public static boolean hasFunction(String JavaDoc name) {
122         return s_functionTable.containsKey(name);
123     }
124     
125     /**
126      * Add a function to the function table. It will then become available
127      * for use with compiled statements of the prefuse expression language.
128      * @param name the name of the function. This name must not already
129      * be registered in the table, i.e. there is no function overloading.
130      * @param type the Class instance of the function itself
131      */

132     public static void addFunction(String JavaDoc name, Class JavaDoc type) {
133         if ( !Function.class.isAssignableFrom(type) ) {
134             throw new IllegalArgumentException JavaDoc(
135                 "Type argument must be a subclass of FunctionExpression.");
136         }
137         if ( hasFunction(name) ) {
138             throw new IllegalArgumentException JavaDoc(
139                 "Function with that name already exists");
140         }
141         String JavaDoc lo = name.toLowerCase();
142         String JavaDoc hi = name.toUpperCase();
143         if ( !name.equals(lo) && !name.equals(hi) )
144             throw new IllegalArgumentException JavaDoc(
145                 "Name can't have mixed case, try \""+hi+"\" instead.");
146         s_functionTable.put(lo, type);
147         s_functionTable.put(hi, type);
148     }
149     
150     /**
151      * Get a new Function instance for the function with the given name.
152      * @param name the name of the function to create
153      * @return the instantiated Function
154      */

155     public static Function createFunction(String JavaDoc name) {
156         Class JavaDoc type = (Class JavaDoc)s_functionTable.get(name);
157         if ( type == null ) {
158             throw new IllegalArgumentException JavaDoc(
159                     "Unrecognized function name");
160         }
161         try {
162             return (Function)type.newInstance();
163         } catch (InstantiationException JavaDoc e) {
164             throw new RuntimeException JavaDoc(e);
165         } catch (IllegalAccessException JavaDoc e) {
166             throw new RuntimeException JavaDoc(e);
167         }
168     }
169
170 } // end of class FunctionTable
171
Popular Tags