1 24 25 package com.mckoi.database; 26 27 import java.lang.reflect.Constructor ; 28 import java.lang.reflect.InvocationTargetException ; 29 import java.util.Iterator ; 30 import java.util.HashMap ; 31 import java.util.Set ; 32 import java.text.*; 33 34 44 45 public abstract class FunctionFactory implements FunctionLookup { 46 47 private static final Expression GLOB_EXPRESSION; 48 49 static { 50 GLOB_EXPRESSION = new Expression(); 51 GLOB_EXPRESSION.addElement(TObject.stringVal("*")); 52 GLOB_EXPRESSION.text().append("*"); 53 } 54 55 58 public static final Expression[] GLOB_LIST = 59 new Expression[] { GLOB_EXPRESSION }; 60 61 65 private HashMap fun_class_mapping; 66 67 70 private Class [] construct_proto; 71 72 73 76 public FunctionFactory() { 77 fun_class_mapping = new HashMap (); 78 construct_proto = new Class [1]; 80 Object exp_arr_ob = 81 java.lang.reflect.Array.newInstance(new Expression().getClass(), 0); 82 construct_proto[0] = exp_arr_ob.getClass(); 83 } 84 85 96 protected void addFunction(String fun_name, Class fun_class, int fun_type) { 97 try { 98 String lf_name = fun_name.toLowerCase(); 99 if (fun_class_mapping.get(lf_name) == null) { 100 FF_FunctionInfo ff_info = new FF_FunctionInfo(fun_name, fun_type, 101 fun_class.getConstructor(construct_proto)); 102 fun_class_mapping.put(lf_name, ff_info); 103 } 104 else { 105 throw new Error ("Function '" + fun_name + 106 "' already defined in factory."); 107 } 108 } 109 catch (NoSuchMethodException e) { 110 throw new RuntimeException (e.getMessage()); 111 } 112 } 113 114 117 protected void addFunction(String fun_name, Class fun_class) { 118 addFunction(fun_name, fun_class, FunctionInfo.STATIC); 119 } 120 121 124 protected void removeFunction(String fun_name) { 125 String lf_name = fun_name.toLowerCase(); 126 if (fun_class_mapping.get(lf_name) != null) { 127 fun_class_mapping.remove(fun_name.toLowerCase()); 128 } 129 else { 130 throw new Error ("Function '" + lf_name + 131 "' is not defined in this factory."); 132 } 133 } 134 135 138 protected boolean functionDefined(String fun_name) { 139 String lf_name = fun_name.toLowerCase(); 140 return fun_class_mapping.get(lf_name) != null; 141 } 142 143 148 public abstract void init(); 149 150 155 public Function generateFunction(FunctionDef function_def) { 156 158 String func_name = function_def.getName(); 159 Expression[] params = function_def.getParameters(); 160 161 164 FF_FunctionInfo ff_info = (FF_FunctionInfo) 165 fun_class_mapping.get(func_name.toLowerCase()); 166 if (ff_info == null) { 167 return null; 169 } 170 else { 171 Constructor fun_constructor = (Constructor ) ff_info.getConstructor(); 172 Object [] args = new Object [] { params }; 173 try { 174 return (Function) fun_constructor.newInstance(args); 175 } 176 catch (InvocationTargetException e) { 177 throw new RuntimeException (e.getTargetException().getMessage()); 178 } 179 catch (Exception e) { 180 throw new RuntimeException (e.getMessage()); 181 } 182 } 183 } 184 185 189 public boolean isAggregate(FunctionDef function_def) { 190 FunctionInfo f_info = getFunctionInfo(function_def.getName()); 191 if (f_info == null) { 192 return false; 194 } 195 else { 196 return (f_info.getType() == FunctionInfo.AGGREGATE); 197 } 198 } 199 200 205 public FunctionInfo getFunctionInfo(String fun_name) { 206 FF_FunctionInfo ff_info = (FF_FunctionInfo) 207 fun_class_mapping.get(fun_name.toLowerCase()); 208 return ff_info; 209 } 210 211 217 public FunctionInfo[] getAllFunctionInfo() { 218 Set keys = fun_class_mapping.keySet(); 219 int list_size = keys.size(); 220 FunctionInfo[] list = new FunctionInfo[list_size]; 221 Iterator i = keys.iterator(); 222 int n = 0; 223 while (i.hasNext()) { 224 String fun_name = (String ) i.next(); 225 list[n] = getFunctionInfo(fun_name); 226 ++n; 227 } 228 return list; 229 } 230 231 234 protected class FF_FunctionInfo implements FunctionInfo { 235 236 private String name; 237 private int type; 238 private Constructor constructor; 239 240 public FF_FunctionInfo(String name, int type, Constructor constructor) { 241 this.name = name; 242 this.type = type; 243 this.constructor = constructor; 244 } 245 246 public String getName() { 247 return name; 248 } 249 250 public int getType() { 251 return type; 252 } 253 254 public Constructor getConstructor() { 255 return constructor; 256 } 257 258 public String getFunctionFactoryName() { 259 return FunctionFactory.this.getClass().toString(); 260 } 261 262 }; 263 264 } 265 | Popular Tags |