- Timestamp:
- 06/28/13 11:56:03 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main/src/main/java/com/framsticks/params/ParamBuilder.java
r86 r87 4 4 import com.framsticks.params.annotations.ParamAnnotation; 5 5 import com.framsticks.params.types.*; 6 import com.framsticks.util.lang.Numbers; 6 import com.framsticks.util.FramsticksException; 7 import com.framsticks.util.lang.Strings; 7 8 8 9 import org.apache.log4j.Logger; 9 10 10 import java. util.ArrayList;11 import java.lang.reflect.InvocationTargetException; 11 12 import java.util.Arrays; 12 13 import java.util.List; 13 14 14 15 /** … … 56 57 private Class<? extends Param> paramType; 57 58 58 private Param param; 59 private PrimitiveParam primitiveParam; 60 private String typeString; 59 private Object min; 60 61 private Object max; 62 63 private Object def; 64 65 String containedTypeName; 66 67 protected FramsClassBuilder classBuilder; 61 68 62 69 public ParamBuilder() { 70 this(null); 71 } 72 73 74 /** 75 * @param classBuilder 76 */ 77 public ParamBuilder(FramsClassBuilder classBuilder) { 78 this.classBuilder = classBuilder; 79 } 80 81 82 /** 83 * @return the min 84 */ 85 public Object getMin() { 86 return min; 87 } 88 89 /** 90 * @return the max 91 */ 92 public Object getMax() { 93 return max; 94 } 95 96 /** 97 * @return the def 98 */ 99 public Object getDef() { 100 return def; 101 } 102 103 public String getContainedTypeName() { 104 return Strings.notEmpty(containedTypeName) ? containedTypeName : null; 105 } 106 107 /** 108 * @return the enumValues 109 */ 110 public List<String> getEnumValues() { 111 return enumValues; 112 } 113 114 /** 115 * @return the procedureSignature 116 */ 117 public String getProcedureSignature() { 118 return procedureSignature; 119 } 120 121 /** 122 * @return the uid 123 */ 124 public String getUid() { 125 return uid; 63 126 } 64 127 … … 70 133 * when Param getType is not defined 71 134 */ 72 public Param finish() { 73 assert param != null; 74 param.id = id; 75 param.name = name; 76 param.help = help; 77 param.group = group; 78 param.flags = flags; 79 param.internalId = internalId; 80 return param; 135 public Param finish() { 136 try { 137 return paramType.getConstructor(ParamBuilder.class).newInstance(this); 138 } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { 139 throw new FramsticksException().msg("failed to create param").cause(e).arg("name", name); 140 } 81 141 } 82 142 … … 87 147 } 88 148 89 protected <T extends Param> ParamBuilder internalSetType(Class<? extends Param> type, T param) {90 this.paramType = type;91 this.param = param;92 if (param instanceof PrimitiveParam) {93 primitiveParam = (PrimitiveParam) param;94 }95 typeString = param.getFramsTypeName();96 return this;97 }98 99 149 public <T extends Param> ParamBuilder type(Class<T> type) { 100 150 assert type != null; 101 try { 102 return internalSetType(type, type.newInstance()); 103 } catch (InstantiationException | IllegalAccessException e) { 104 e.printStackTrace(); 105 } 106 return this; 107 } 108 109 public <T extends Param> ParamBuilder type(T param) { 110 return internalSetType(param.getClass(), param); 111 } 151 this.paramType = type; 152 return this; 153 } 154 155 // public <T extends Param> ParamBuilder type(T param) { 156 // return internalSetType(param.getClass(), param); 157 // } 112 158 113 159 … … 120 166 } 121 167 168 /** 169 * @return the internalId 170 */ 171 public String getInternalId() { 172 return internalId; 173 } 174 122 175 public ParamBuilder setInternalId(String internalId) { 123 176 this.internalId = internalId; … … 144 197 145 198 protected <T extends Number> void parseMinMaxDefNumber(Class<T> type, String second, String third) { 146 if (primitiveParam == null) {147 log.warn("param is not a primitive param");148 return;149 }150 199 if (second != null) { 151 this.primitiveParam.min = Numbers.parse(second, type); 152 if (this.primitiveParam.min == null) { 153 log.warn("value of min attribute was invalid"); 154 } 200 min = second; 155 201 } 156 202 if (third != null) { 157 this.primitiveParam.max = Numbers.parse(third, type); 158 if (this.primitiveParam.max == null) { 159 log.warn("value of min attribute was invalid"); 160 } 161 } 162 } 203 max = third; 204 } 205 } 206 207 protected List<String> enumValues; 208 209 public ParamBuilder enums(List<String> values) { 210 enumValues = values; 211 return type(EnumParam.class); 212 } 213 214 protected String procedureSignature; 215 216 protected String uid; 163 217 164 218 @ParamAnnotation 165 219 public ParamBuilder type(String type) { 166 typeString = type;220 // typeString = type; 167 221 168 222 log.trace("parsing type: " + type); … … 175 229 switch (first.charAt(0)) { 176 230 case 'o': { 177 type(new ObjectParam(second != null ? second : first.substring(1))); 231 containedTypeName = second != null ? second : first.substring(1); 232 type(ObjectParam.class); 178 233 break; 179 234 } 180 235 case 'p': { 181 ProcedureParam procedureParam = new ProcedureParam(); 182 String signature = type.substring(1); 183 try { 184 procedureParam.parseSignature(signature); 185 } catch (Exception e) { 186 log.error("invalid procedure signature '" + signature + "': " + e); 187 } 188 type(procedureParam); 236 procedureSignature = type.substring(1); 237 type(ProcedureParam.class); 189 238 break; 190 239 } … … 193 242 int tildeIndex = type.indexOf("~"); 194 243 if (tildeIndex != -1) { 195 type(new EnumParam(new ArrayList<String>(Arrays.asList(type.substring(tildeIndex + 1).split("~")))));244 enums(Arrays.asList(type.substring(tildeIndex + 1).split("~"))); 196 245 } else { 197 246 if (first.length() >= 2) { … … 214 263 type(BooleanParam.class); 215 264 } 216 if (param == null) {265 if (paramType == null) { 217 266 type(DecimalParam.class); 218 267 } 219 268 } 220 if ( this.param instanceof DecimalParam) {269 if (DecimalParam.class.isAssignableFrom(this.paramType)) { 221 270 parseMinMaxDefNumber(Integer.class, second, third); 222 271 } … … 243 292 } 244 293 case 'l': { 245 type(third != null ? new UniqueListParam(second, third) : new ArrayListParam(second)); 294 containedTypeName = second; 295 if (third != null) { 296 type(UniqueListParam.class); 297 uid = third; 298 } else { 299 type(ArrayListParam.class); 300 } 246 301 break; 247 302 } … … 294 349 @ParamAnnotation 295 350 public String getType() { 296 return typeString;351 return "?"; 297 352 } 298 353 … … 304 359 } 305 360 306 public <T> ParamBuilder min(T min) { 307 if (primitiveParam != null) { 308 this.primitiveParam.min = min; 309 } 310 return this; 311 } 312 313 public <T> ParamBuilder max(T max) { 314 if (primitiveParam != null) { 315 this.primitiveParam.max = max; 316 } 317 return this; 318 } 319 320 public <T> ParamBuilder def(T def) { 321 if (def != null && primitiveParam != null) { 322 this.primitiveParam.def = def; 323 } 324 return this; 325 } 326 327 public Class<?> getStorageType() { 328 assert param != null; 329 return param.getStorageType(); 330 } 361 public ParamBuilder min(Object min) { 362 this.min = min; 363 return this; 364 } 365 366 public ParamBuilder max(Object max) { 367 this.max = max; 368 return this; 369 } 370 371 public ParamBuilder def(Object def) { 372 this.def = def; 373 return this; 374 } 375 376 // public Class<?> getStorageType() { 377 // assert param != null; 378 // return param.getStorageType(); 379 // } 331 380 332 381 public Param build(String line) throws Exception { … … 386 435 } 387 436 388 437 public ParamBuilder defaultDef(Object def) { 438 if (this.def == null) { 439 return def(def); 440 } 441 return this; 442 } 389 443 390 444 }
Note: See TracChangeset
for help on using the changeset viewer.