- Timestamp:
- 07/02/13 16:20:07 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main/src/main/java/com/framsticks/params/ParamBuilder.java
r88 r90 11 11 12 12 import java.lang.reflect.InvocationTargetException; 13 import java.util.ArrayList; 13 14 import java.util.Arrays; 14 15 import java.util.List; 16 import java.util.regex.Matcher; 17 import java.util.regex.Pattern; 18 19 import javax.annotation.Nonnull; 15 20 16 21 /** … … 37 42 private String id; 38 43 39 /**40 * The parameter internal id. It's set by a user to use user's own id in41 * code42 */43 private String internalId;44 45 44 /** The number of group, that parameter belongs to. */ 46 private Integer group = 0;45 private Integer group; 47 46 48 47 /** The flags stored as a bit sum. */ … … 76 75 } 77 76 77 protected ValueParam resultType; 78 79 protected List<ValueParam> argumentsType; 78 80 79 81 /** … … 114 116 115 117 /** 118 * @return the resultType 119 */ 120 public ValueParam getResultType() { 121 return resultType; 122 } 123 124 125 /** 126 * @param resultType the resultType to set 127 */ 128 public ParamBuilder resultType(ValueParam resultType) { 129 this.resultType = resultType; 130 return this; 131 } 132 133 /** 134 * @return the argumentsType 135 */ 136 public List<ValueParam> getArgumentsType() { 137 return argumentsType; 138 } 139 140 141 /** 142 * @param argumentsType the argumentsType to set 143 */ 144 public ParamBuilder argumentsType(List<ValueParam> argumentsType) { 145 this.argumentsType = argumentsType; 146 return this; 147 } 148 149 /** 116 150 * @return the enumValues 117 151 */ … … 121 155 122 156 /** 123 * @return the procedureSignature124 */125 public String getProcedureSignature() {126 return procedureSignature;127 }128 129 /**130 157 * @return the uid 131 158 */ 132 159 public String getUid() { 133 160 return uid; 161 } 162 163 public @Nonnull <T extends Param> T finish(Class<T> requested) { 164 Param param = finish(); 165 if (!requested.isInstance(param)) { 166 throw new FramsticksException().msg("param is of wrong type").arg("requested", requested).arg("actual", param.getClass()); 167 } 168 return requested.cast(param); 134 169 } 135 170 … … 141 176 * when Param getType is not defined 142 177 */ 143 public Param finish() {178 public @Nonnull Param finish() { 144 179 try { 145 180 if (paramType == null) { 146 181 throw new FramsticksException().msg("trying to finish incomplete param"); 147 182 } 148 149 183 return paramType.getConstructor(ParamBuilder.class).newInstance(this); 150 184 } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | FramsticksException e) { … … 165 199 } 166 200 167 // public <T extends Param> ParamBuilder type(T param) {168 // return internalSetType(param.getClass(), param);169 // }170 171 172 201 /** 173 202 * @return the id … … 178 207 } 179 208 180 /**181 * @return the internalId182 */183 public String getInternalId() {184 return internalId;185 }186 187 public ParamBuilder setInternalId(String internalId) {188 this.internalId = internalId;189 return this;190 }191 192 209 @ParamAnnotation 193 210 public ParamBuilder group(Integer group) { … … 208 225 } 209 226 210 protected <T extends Number> void parseMinMaxDefNumber(Class<T> type, String second, String third ) {227 protected <T extends Number> void parseMinMaxDefNumber(Class<T> type, String second, String third, String fourth) { 211 228 if (second != null) { 212 229 min = second; … … 214 231 if (third != null) { 215 232 max = third; 233 } 234 if (fourth != null) { 235 def = fourth; 216 236 } 217 237 } … … 223 243 return type(EnumParam.class); 224 244 } 225 226 protected String procedureSignature;227 245 228 246 protected String uid; … … 239 257 String second = typeSplitted.length > 1 ? typeSplitted[1] : null; 240 258 String third = typeSplitted.length > 2 ? typeSplitted[2] : null; 259 String fourth = typeSplitted.length > 3 ? typeSplitted[3] : null; 241 260 242 261 switch (first.charAt(0)) { … … 247 266 } 248 267 case 'p': { 249 procedureSignature = type.substring(1);250 268 type(ProcedureParam.class); 269 signature(type.substring(1)); 251 270 break; 252 271 } … … 281 300 } 282 301 if (DecimalParam.class.isAssignableFrom(this.paramType)) { 283 parseMinMaxDefNumber(Integer.class, second, third );302 parseMinMaxDefNumber(Integer.class, second, third, fourth); 284 303 } 285 304 break; … … 287 306 case 'f': { 288 307 type(FloatParam.class); 289 parseMinMaxDefNumber(Double.class, second, third );308 parseMinMaxDefNumber(Double.class, second, third, fourth); 290 309 break; 291 310 } … … 472 491 } 473 492 474 475 493 public Class<?> getStorageType() { 476 494 return storageType; 477 495 } 496 497 public ParamBuilder forAccess(AccessInterface access) { 498 return name(access.getId()).type("o " + access.getId()); 499 } 500 501 protected static ValueParam parseProcedureTypePart(String type, String name) { 502 return Param.build().type(type).name(name).id(name).finish(ValueParam.class); 503 } 504 505 private static Pattern addressPattern = Pattern.compile("^([^\\(]+)?\\(([^\\)]*)\\)$"); 506 507 public ParamBuilder signature(String signature) { 508 argumentsType = new ArrayList<>(); 509 510 if (!Strings.notEmpty(signature)) { 511 resultType = null; 512 return this; 513 } 514 Matcher matcher = addressPattern.matcher(signature); 515 if (!matcher.matches()) { 516 throw new FramsticksException().msg("invalid signature"); 517 } 518 String result = Strings.collapse(matcher.group(1)); 519 resultType = (result != null) ? parseProcedureTypePart(result, null) : null; 520 String arguments = matcher.group(2); 521 if (!Strings.notEmpty(arguments)) { 522 return this; 523 } 524 int number = 0; 525 for (String a : arguments.split(",")) { 526 int space = a.indexOf(' '); 527 String type; 528 String name; 529 if (space == -1) { 530 type = a; 531 name = "arg" + number; 532 } else { 533 type = a.substring(0, space); 534 name = a.substring(space + 1); 535 } 536 argumentsType.add(parseProcedureTypePart(type, name)); 537 ++number; 538 } 539 return this; 540 } 541 542 543 public ParamBuilder idAndName(String name) { 544 id(name); 545 name(name); 546 return this; 547 } 478 548 } 479 549
Note: See TracChangeset
for help on using the changeset viewer.