1 33 package net.sf.jga.fn.string; 34 35 import java.text.DateFormat ; 36 import java.text.Format ; 37 import java.text.MessageFormat ; 38 import java.text.NumberFormat ; 39 import java.text.ParseException ; 40 import java.util.Date ; 41 import net.sf.jga.fn.EvaluationException; 42 import net.sf.jga.fn.UnaryFunctor; 43 import net.sf.jga.fn.adaptor.Identity; 44 import net.sf.jga.fn.arithmetic.ValueOf; 45 import net.sf.jga.fn.property.Cast; 46 import net.sf.jga.fn.property.ConstructUnary; 47 import net.sf.jga.fn.property.GetProperty; 48 49 56 57 public class ParseFormat<R> extends UnaryFunctor<String ,R> { 58 59 static final long serialVersionUID = 6590747028326789147L; 60 61 private Format _format; 63 64 private UnaryFunctor<Object , R> _converter; 67 68 73 protected ParseFormat(Format format, UnaryFunctor<Object ,R> conv) { 74 if (format == null) { 75 String msg = "Format must be specified"; 76 throw new IllegalArgumentException (msg); 77 } 78 79 _format = format; 80 _converter = conv; 81 } 82 83 86 87 public Format getFormat() { 88 return _format; 89 } 90 91 93 102 103 public R fn(String arg) { 104 105 try { 106 Object obj = _format.parseObject(arg); 107 R val = (_converter == null) ? (R) obj : _converter.fn(obj); 114 return val; 115 } 116 catch (ParseException x) { 117 String msg = "Unable to parse \"{0}\""; 118 Object [] args = new Object []{ arg }; 119 throw new EvaluationException(MessageFormat.format(msg,args), x); 120 } 121 catch (ClassCastException x) { 122 String msg = "Unable to convert \"{0}\" to correct type"; 123 Object [] args = new Object []{ arg }; 124 throw new EvaluationException(MessageFormat.format(msg,args), x); 125 } 126 } 127 128 132 public void accept(net.sf.jga.fn.Visitor v) { 133 if (v instanceof ParseFormat.Visitor) 134 ((ParseFormat.Visitor)v).visit(this); 135 else 136 v.visit(this); 137 } 138 139 141 public String toString() { 142 return "ParseFormat["+_format+"]"; 143 } 144 145 147 151 public interface Visitor extends net.sf.jga.fn.Visitor { 152 public void visit(ParseFormat host); 153 } 154 155 160 161 static public class Date<T extends java.util.Date > extends ParseFormat<T> { 162 static final long serialVersionUID = 6959869794482290044L; 163 public Date(Class <T> type, DateFormat format) { 164 super(format, (UnaryFunctor<Object ,T>) 165 174 ((type.equals(java.util.Date .class)) 177 ? new Cast<Object ,java.util.Date >(java.util.Date .class) 178 : new ConstructUnary<Long ,T>(Long.TYPE, type) 179 .compose(new GetProperty<java.util.Date ,Long >(java.util.Date .class,"Time")) 180 .compose(new Cast<Object ,java.util.Date >(java.util.Date .class)))); 181 } 182 } 183 184 189 190 static public class Number<T extends java.lang.Number > extends ParseFormat<T> { 191 static final long serialVersionUID = -5365953713327551298L; 192 public Number(Class <T> type, NumberFormat format) { 193 super(format, new ValueOf<java.lang.Number ,T>(type) 195 .compose(new Cast<Object ,java.lang.Number >(java.lang.Number .class))); 196 } 197 } 198 } 199 | Popular Tags |