1 33 package net.sf.jga.parser; 34 35 import java.text.MessageFormat ; 36 import net.sf.jga.fn.BinaryFunctor; 37 import net.sf.jga.fn.Generator; 38 import net.sf.jga.fn.UnaryFunctor; 39 40 50 51 public class GenericParser implements IParser { 52 53 static private GenericParser _instance; 55 56 59 static public synchronized GenericParser getInstance() { 60 if (_instance == null) { 61 _instance = new GenericParser(FunctorParser.getInstance()); 62 } 63 64 return _instance; 65 } 66 67 68 private IParser _delegate; 69 70 public GenericParser() { _delegate = new JFXGParser(); } 71 public GenericParser(IParser delegate) { _delegate = delegate; } 72 73 77 80 public <R> Generator<R> parseGenerator(String str, Class <R> returnType) 81 throws ClassCastException , ParseException 82 { 83 Generator gen = parseGenerator(str); 84 if (returnType.isAssignableFrom(getReturnType())) 85 return (Generator<R>) gen; 86 87 String err = "Expected Generator<{0}>, but expression returns {1}"; 88 String msg= MessageFormat.format(err, new Object [] {returnType.getName(), 89 getReturnType().getName()}); 90 throw new ClassCastException (msg); 91 } 92 93 97 public <T,R> UnaryFunctor<T,R> parseUnary(String str, Class <T> argType, Class <R> returnType) 98 throws ClassCastException , ParseException 99 { 100 UnaryFunctor uf = parseUnary(str, argType); 101 if (returnType.isAssignableFrom(getReturnType())) 102 return (UnaryFunctor<T,R>) uf; 103 104 String err = "Expected UnaryFunctor<{0},{1}>, but expression returns {2}"; 105 String msg= MessageFormat.format(err, new Object [] {argType.getName(), 106 returnType.getName(), 107 getReturnType().getName()}); 108 throw new ClassCastException (msg); 109 } 110 111 115 public <T1,T2,R> BinaryFunctor<T1,T2,R> 116 parseBinary(String str, Class <T1> arg1Type, Class <T2> arg2Type, Class <R> returnType) 117 throws ClassCastException , ParseException 118 { 119 BinaryFunctor bf = parseBinary(str, arg1Type, arg2Type); 120 if (returnType.isAssignableFrom(getReturnType())) 121 return (BinaryFunctor<T1,T2,R>) bf; 122 123 String err = "Expected BinaryFunctor<{0},{1},{2}>, but expression returns {3}"; 124 String msg= MessageFormat.format(err, new Object [] {arg1Type.getName(), 125 arg2Type.getName(), 126 returnType.getName(), 127 getReturnType().getName()}); 128 throw new ClassCastException (msg); 129 } 130 131 132 136 139 public Generator parseGenerator(String str) throws ParseException { 140 return _delegate.parseGenerator(str); 141 } 142 143 147 public UnaryFunctor parseUnary(String str, Class argType) throws ParseException { 148 return _delegate.parseUnary(str, argType); 149 } 150 151 155 public BinaryFunctor parseBinary(String str, Class arg1Type, Class arg2Type) 156 throws ParseException 157 { 158 return _delegate.parseBinary(str, arg1Type, arg2Type); 159 } 160 161 166 public Class getReturnType() { 167 return _delegate.getReturnType(); 168 } 169 170 174 177 static public <R> Generator<R> parse(String str, Class <R> returnType) 178 throws UncheckedParseException, ClassCastException 179 { 180 try { 181 return getInstance().parseGenerator(str); 182 } 183 catch(ParseException x) { 184 throw new UncheckedParseException(x); 185 } 186 } 187 188 189 193 static public <T,R> UnaryFunctor<T,R> parse(String str, Class <T> argType, Class <R> returnType) 194 throws UncheckedParseException, ClassCastException 195 { 196 try { 197 return getInstance().parseUnary(str, argType, returnType); 198 } 199 catch(ParseException x) { 200 throw new UncheckedParseException(x); 201 } 202 } 203 204 205 209 static public <T1,T2,R> BinaryFunctor<T1,T2,R> 210 parse(String str, Class <T1> arg1Type, Class <T2> arg2Type, Class <R> returnType) 211 throws UncheckedParseException, ClassCastException 212 { 213 try { 214 return getInstance().parseBinary(str, arg1Type, arg2Type, returnType); 215 } 216 catch(ParseException x) { 217 throw new UncheckedParseException(x); 218 } 219 } 220 221 225 230 233 236 public void importClass(Class clasz) { 237 ((JFXGParser) _delegate).importClass(clasz); 238 } 239 240 241 244 public void importClass(String alias, Class clasz) { 245 ((JFXGParser) _delegate).importClass(alias, clasz); 246 } 247 248 249 252 public void deportClass(String alias) { 253 ((JFXGParser) _delegate).deportClass(alias); 254 } 255 256 257 260 public void importStatics(Class clasz) { 261 ((JFXGParser) _delegate).importStatics(clasz); 262 } 263 264 265 268 public void importField(Class clasz, String name) throws NoSuchFieldException { 269 ((JFXGParser) _delegate).importField(clasz, name); 270 } 271 272 275 public void importField(java.lang.reflect.Field field) throws IllegalArgumentException { 276 ((JFXGParser) _delegate).importField(field); 277 } 278 279 282 public java.lang.reflect.Field getImportedField(String name) { 283 return ((JFXGParser) _delegate).getImportedField(name); 284 } 285 286 289 public void importMethod(Class clasz, String name) throws NoSuchMethodException { 290 ((JFXGParser) _delegate).importMethod(clasz, name); 291 } 292 293 296 public void importMethod(java.lang.reflect.Method meth) { 297 ((JFXGParser) _delegate).importMethod(meth); 298 } 299 300 303 public void importMethod(String name, java.lang.reflect.Method meth) { 304 ((JFXGParser) _delegate).importMethod(name, meth); 305 } 306 307 308 311 public java.lang.reflect.Method [] getImportedMethods(String name) { 312 return ((JFXGParser) _delegate).getImportedMethods(name); 313 } 314 315 316 319 public void bindThis(Object thisBinding) { 320 ((JFXGParser) _delegate).bindThis(thisBinding); 321 } 322 323 324 327 protected Object getBoundObject() { 328 return ((JFXGParser) _delegate).getBoundObject(); 329 } 330 331 332 335 public void setUndecoratedDecimal(boolean flag) { 336 ((JFXGParser) _delegate).setUndecoratedDecimal(flag); 337 } 338 339 340 343 public boolean isUndecoratedDecimal() { 344 return ((JFXGParser) _delegate).isUndecoratedDecimal(); 345 } 346 347 } 348 | Popular Tags |