[77] | 1 | package com.framsticks.params; |
---|
| 2 | |
---|
[86] | 3 | import com.framsticks.params.annotations.FramsClassAnnotation; |
---|
| 4 | import com.framsticks.params.annotations.ParamAnnotation; |
---|
[77] | 5 | import com.framsticks.params.types.*; |
---|
[88] | 6 | import com.framsticks.util.Builder; |
---|
[87] | 7 | import com.framsticks.util.FramsticksException; |
---|
| 8 | import com.framsticks.util.lang.Strings; |
---|
[84] | 9 | |
---|
[77] | 10 | import org.apache.log4j.Logger; |
---|
| 11 | |
---|
[87] | 12 | import java.lang.reflect.InvocationTargetException; |
---|
[90] | 13 | import java.util.ArrayList; |
---|
[77] | 14 | import java.util.Arrays; |
---|
[87] | 15 | import java.util.List; |
---|
[90] | 16 | import java.util.regex.Matcher; |
---|
| 17 | import java.util.regex.Pattern; |
---|
[77] | 18 | |
---|
[90] | 19 | import javax.annotation.Nonnull; |
---|
| 20 | |
---|
[77] | 21 | /** |
---|
| 22 | * The class ParamBuilder helps building Param objects. |
---|
[84] | 23 | * |
---|
[77] | 24 | * @author Mateusz Jarus <name.surname@gmail.com> (please replace name and |
---|
| 25 | * surname with my personal data) |
---|
| 26 | * |
---|
| 27 | * @author Piotr Śniegowski |
---|
| 28 | */ |
---|
| 29 | |
---|
[86] | 30 | @FramsClassAnnotation(name = "prop", id = "prop") |
---|
[88] | 31 | public class ParamBuilder implements Builder<Param> { |
---|
[84] | 32 | private final static Logger log = Logger.getLogger(ParamBuilder.class.getName()); |
---|
[77] | 33 | |
---|
| 34 | private static final String ID_FIELD = "id"; |
---|
| 35 | private static final String NAME_FIELD = "name"; |
---|
| 36 | private static final String HELP_FIELD = "help"; |
---|
| 37 | private static final String GROUP_FIELD = "group"; |
---|
| 38 | private static final String TYPE_FIELD = "type"; |
---|
| 39 | private static final String FLAGS_FIELD = "flags"; |
---|
| 40 | |
---|
[86] | 41 | /** The parameter id. */ |
---|
[77] | 42 | private String id; |
---|
| 43 | |
---|
| 44 | /** The number of group, that parameter belongs to. */ |
---|
[90] | 45 | private Integer group; |
---|
[77] | 46 | |
---|
[86] | 47 | /** The flags stored as a bit sum. */ |
---|
[77] | 48 | private Integer flags = 0; |
---|
| 49 | |
---|
[86] | 50 | /** The parameter name. */ |
---|
[77] | 51 | private String name; |
---|
| 52 | |
---|
[86] | 53 | /** The help (description) concerning parameter. */ |
---|
[77] | 54 | private String help; |
---|
| 55 | |
---|
[86] | 56 | /** The type of parameter. */ |
---|
[84] | 57 | private Class<? extends Param> paramType; |
---|
[77] | 58 | |
---|
[87] | 59 | private Object min; |
---|
[77] | 60 | |
---|
[87] | 61 | private Object max; |
---|
| 62 | |
---|
| 63 | private Object def; |
---|
| 64 | |
---|
[88] | 65 | private Integer extra; |
---|
| 66 | |
---|
[87] | 67 | String containedTypeName; |
---|
| 68 | |
---|
[88] | 69 | protected Class<?> storageType; |
---|
| 70 | |
---|
[87] | 71 | protected FramsClassBuilder classBuilder; |
---|
| 72 | |
---|
[86] | 73 | public ParamBuilder() { |
---|
[87] | 74 | this(null); |
---|
[85] | 75 | } |
---|
| 76 | |
---|
[90] | 77 | protected ValueParam resultType; |
---|
[87] | 78 | |
---|
[90] | 79 | protected List<ValueParam> argumentsType; |
---|
| 80 | |
---|
[77] | 81 | /** |
---|
[87] | 82 | * @param classBuilder |
---|
| 83 | */ |
---|
| 84 | public ParamBuilder(FramsClassBuilder classBuilder) { |
---|
| 85 | this.classBuilder = classBuilder; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | |
---|
| 89 | /** |
---|
| 90 | * @return the min |
---|
| 91 | */ |
---|
[88] | 92 | @ParamAnnotation |
---|
[87] | 93 | public Object getMin() { |
---|
| 94 | return min; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | /** |
---|
| 98 | * @return the max |
---|
| 99 | */ |
---|
[88] | 100 | @ParamAnnotation |
---|
[87] | 101 | public Object getMax() { |
---|
| 102 | return max; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | /** |
---|
| 106 | * @return the def |
---|
| 107 | */ |
---|
[88] | 108 | @ParamAnnotation |
---|
[87] | 109 | public Object getDef() { |
---|
| 110 | return def; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | public String getContainedTypeName() { |
---|
| 114 | return Strings.notEmpty(containedTypeName) ? containedTypeName : null; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | /** |
---|
[90] | 118 | * @return the resultType |
---|
[87] | 119 | */ |
---|
[90] | 120 | public ValueParam getResultType() { |
---|
| 121 | return resultType; |
---|
[87] | 122 | } |
---|
| 123 | |
---|
[90] | 124 | |
---|
[87] | 125 | /** |
---|
[90] | 126 | * @param resultType the resultType to set |
---|
[87] | 127 | */ |
---|
[90] | 128 | public ParamBuilder resultType(ValueParam resultType) { |
---|
| 129 | this.resultType = resultType; |
---|
| 130 | return this; |
---|
[87] | 131 | } |
---|
| 132 | |
---|
| 133 | /** |
---|
[90] | 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 | /** |
---|
| 150 | * @return the enumValues |
---|
| 151 | */ |
---|
| 152 | public List<String> getEnumValues() { |
---|
| 153 | return enumValues; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | /** |
---|
[87] | 157 | * @return the uid |
---|
| 158 | */ |
---|
| 159 | public String getUid() { |
---|
| 160 | return uid; |
---|
| 161 | } |
---|
| 162 | |
---|
[90] | 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); |
---|
| 169 | } |
---|
| 170 | |
---|
[87] | 171 | /** |
---|
[77] | 172 | * Build Param based on provided data. |
---|
[84] | 173 | * |
---|
[77] | 174 | * @return Param object |
---|
| 175 | * @throws Exception |
---|
| 176 | * when Param getType is not defined |
---|
| 177 | */ |
---|
[90] | 178 | public @Nonnull Param finish() { |
---|
[87] | 179 | try { |
---|
[88] | 180 | if (paramType == null) { |
---|
| 181 | throw new FramsticksException().msg("trying to finish incomplete param"); |
---|
| 182 | } |
---|
[87] | 183 | return paramType.getConstructor(ParamBuilder.class).newInstance(this); |
---|
[88] | 184 | } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | FramsticksException e) { |
---|
[87] | 185 | throw new FramsticksException().msg("failed to create param").cause(e).arg("name", name); |
---|
| 186 | } |
---|
[77] | 187 | } |
---|
| 188 | |
---|
[86] | 189 | @ParamAnnotation |
---|
[85] | 190 | public ParamBuilder id(String id) { |
---|
[77] | 191 | this.id = id; |
---|
| 192 | return this; |
---|
| 193 | } |
---|
| 194 | |
---|
[85] | 195 | public <T extends Param> ParamBuilder type(Class<T> type) { |
---|
[86] | 196 | assert type != null; |
---|
[87] | 197 | this.paramType = type; |
---|
[77] | 198 | return this; |
---|
| 199 | } |
---|
| 200 | |
---|
[86] | 201 | /** |
---|
| 202 | * @return the id |
---|
| 203 | */ |
---|
| 204 | @ParamAnnotation |
---|
| 205 | public String getId() { |
---|
| 206 | return id; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | @ParamAnnotation |
---|
[85] | 210 | public ParamBuilder group(Integer group) { |
---|
[77] | 211 | this.group = group; |
---|
| 212 | return this; |
---|
| 213 | } |
---|
| 214 | |
---|
[86] | 215 | @ParamAnnotation |
---|
[85] | 216 | public ParamBuilder flags(Integer flags) { |
---|
[77] | 217 | this.flags = flags; |
---|
| 218 | return this; |
---|
| 219 | } |
---|
| 220 | |
---|
[86] | 221 | @ParamAnnotation |
---|
[85] | 222 | public ParamBuilder name(String name) { |
---|
[77] | 223 | this.name = name; |
---|
| 224 | return this; |
---|
| 225 | } |
---|
| 226 | |
---|
[90] | 227 | protected <T extends Number> void parseMinMaxDefNumber(Class<T> type, String second, String third, String fourth) { |
---|
[84] | 228 | if (second != null) { |
---|
[87] | 229 | min = second; |
---|
[84] | 230 | } |
---|
| 231 | if (third != null) { |
---|
[87] | 232 | max = third; |
---|
[84] | 233 | } |
---|
[90] | 234 | if (fourth != null) { |
---|
| 235 | def = fourth; |
---|
| 236 | } |
---|
[84] | 237 | } |
---|
| 238 | |
---|
[87] | 239 | protected List<String> enumValues; |
---|
| 240 | |
---|
| 241 | public ParamBuilder enums(List<String> values) { |
---|
| 242 | enumValues = values; |
---|
| 243 | return type(EnumParam.class); |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | protected String uid; |
---|
| 247 | |
---|
[86] | 248 | @ParamAnnotation |
---|
[85] | 249 | public ParamBuilder type(String type) { |
---|
[87] | 250 | // typeString = type; |
---|
[88] | 251 | assert type != null; |
---|
[77] | 252 | |
---|
[84] | 253 | log.trace("parsing type: " + type); |
---|
[77] | 254 | |
---|
| 255 | String[] typeSplitted = type.split(" "); |
---|
| 256 | String first = typeSplitted[0]; |
---|
| 257 | String second = typeSplitted.length > 1 ? typeSplitted[1] : null; |
---|
| 258 | String third = typeSplitted.length > 2 ? typeSplitted[2] : null; |
---|
[90] | 259 | String fourth = typeSplitted.length > 3 ? typeSplitted[3] : null; |
---|
[77] | 260 | |
---|
| 261 | switch (first.charAt(0)) { |
---|
| 262 | case 'o': { |
---|
[87] | 263 | containedTypeName = second != null ? second : first.substring(1); |
---|
| 264 | type(ObjectParam.class); |
---|
[77] | 265 | break; |
---|
| 266 | } |
---|
| 267 | case 'p': { |
---|
[87] | 268 | type(ProcedureParam.class); |
---|
[90] | 269 | signature(type.substring(1)); |
---|
[77] | 270 | break; |
---|
| 271 | } |
---|
| 272 | case 'd': { |
---|
| 273 | |
---|
| 274 | int tildeIndex = type.indexOf("~"); |
---|
| 275 | if (tildeIndex != -1) { |
---|
[87] | 276 | enums(Arrays.asList(type.substring(tildeIndex + 1).split("~"))); |
---|
[77] | 277 | } else { |
---|
| 278 | if (first.length() >= 2) { |
---|
[85] | 279 | switch (first.charAt(1)) { |
---|
[77] | 280 | case 'b': { |
---|
[85] | 281 | type(BinaryParam.class); |
---|
[77] | 282 | break; |
---|
| 283 | } |
---|
| 284 | case 'c': { |
---|
[85] | 285 | type(ColorParam.class); |
---|
[77] | 286 | break; |
---|
| 287 | } |
---|
[85] | 288 | default: { |
---|
| 289 | log.error("unknown type: " + first); |
---|
| 290 | return this; |
---|
| 291 | } |
---|
[77] | 292 | } |
---|
| 293 | } |
---|
[84] | 294 | if ("0".equals(second) && "1".equals(third)) { |
---|
[85] | 295 | type(BooleanParam.class); |
---|
[84] | 296 | } |
---|
[87] | 297 | if (paramType == null) { |
---|
[85] | 298 | type(DecimalParam.class); |
---|
[77] | 299 | } |
---|
| 300 | } |
---|
[87] | 301 | if (DecimalParam.class.isAssignableFrom(this.paramType)) { |
---|
[90] | 302 | parseMinMaxDefNumber(Integer.class, second, third, fourth); |
---|
[77] | 303 | } |
---|
| 304 | break; |
---|
| 305 | } |
---|
| 306 | case 'f': { |
---|
[85] | 307 | type(FloatParam.class); |
---|
[90] | 308 | parseMinMaxDefNumber(Double.class, second, third, fourth); |
---|
[77] | 309 | break; |
---|
| 310 | } |
---|
| 311 | case 'x': { |
---|
[85] | 312 | type(UniversalParam.class); |
---|
[77] | 313 | break; |
---|
| 314 | } |
---|
| 315 | case 's': { |
---|
[85] | 316 | type(StringParam.class); |
---|
| 317 | min(second); |
---|
| 318 | max(third); |
---|
[77] | 319 | break; |
---|
| 320 | } |
---|
| 321 | case 'e': { |
---|
[85] | 322 | type(EventParam.class); |
---|
[77] | 323 | break; |
---|
| 324 | } |
---|
| 325 | case 'l': { |
---|
[87] | 326 | containedTypeName = second; |
---|
| 327 | if (third != null) { |
---|
| 328 | type(UniqueListParam.class); |
---|
| 329 | uid = third; |
---|
| 330 | } else { |
---|
| 331 | type(ArrayListParam.class); |
---|
| 332 | } |
---|
[77] | 333 | break; |
---|
| 334 | } |
---|
[85] | 335 | default:{ |
---|
| 336 | log.error("unknown type: " + first); |
---|
| 337 | return this; |
---|
| 338 | } |
---|
[77] | 339 | } |
---|
| 340 | return this; |
---|
| 341 | } |
---|
| 342 | |
---|
[86] | 343 | @ParamAnnotation |
---|
[85] | 344 | public ParamBuilder help(String help) { |
---|
[77] | 345 | this.help = help; |
---|
| 346 | return this; |
---|
| 347 | } |
---|
| 348 | |
---|
[84] | 349 | /** |
---|
[86] | 350 | * @return the group |
---|
| 351 | */ |
---|
| 352 | @ParamAnnotation |
---|
| 353 | public Integer getGroup() { |
---|
| 354 | return group; |
---|
| 355 | } |
---|
| 356 | |
---|
| 357 | /** |
---|
| 358 | * @return the flags |
---|
| 359 | */ |
---|
| 360 | @ParamAnnotation |
---|
| 361 | public Integer getFlags() { |
---|
| 362 | return flags; |
---|
| 363 | } |
---|
| 364 | |
---|
| 365 | /** |
---|
| 366 | * @return the name |
---|
| 367 | */ |
---|
| 368 | @ParamAnnotation |
---|
| 369 | public String getName() { |
---|
| 370 | return name; |
---|
| 371 | } |
---|
| 372 | |
---|
| 373 | /** |
---|
| 374 | * @return the help |
---|
| 375 | */ |
---|
| 376 | @ParamAnnotation |
---|
| 377 | public String getHelp() { |
---|
| 378 | return help; |
---|
| 379 | } |
---|
| 380 | |
---|
| 381 | @ParamAnnotation |
---|
| 382 | public String getType() { |
---|
[87] | 383 | return "?"; |
---|
[86] | 384 | } |
---|
| 385 | |
---|
[88] | 386 | @ParamAnnotation(id = "xtra") |
---|
| 387 | public Integer getExtra() { |
---|
| 388 | return extra; |
---|
| 389 | } |
---|
| 390 | |
---|
[86] | 391 | /** |
---|
[84] | 392 | * @return the paramType |
---|
| 393 | */ |
---|
| 394 | public Class<? extends Param> getParamType() { |
---|
| 395 | return paramType; |
---|
| 396 | } |
---|
| 397 | |
---|
[88] | 398 | @ParamAnnotation(id = "xtra") |
---|
| 399 | public ParamBuilder extra(Integer extra) { |
---|
| 400 | this.extra = extra; |
---|
| 401 | return this; |
---|
| 402 | } |
---|
| 403 | |
---|
| 404 | @ParamAnnotation |
---|
[87] | 405 | public ParamBuilder min(Object min) { |
---|
| 406 | this.min = min; |
---|
[77] | 407 | return this; |
---|
| 408 | } |
---|
| 409 | |
---|
[88] | 410 | @ParamAnnotation |
---|
[87] | 411 | public ParamBuilder max(Object max) { |
---|
| 412 | this.max = max; |
---|
[77] | 413 | return this; |
---|
| 414 | } |
---|
| 415 | |
---|
[88] | 416 | @ParamAnnotation |
---|
[87] | 417 | public ParamBuilder def(Object def) { |
---|
| 418 | this.def = def; |
---|
[77] | 419 | return this; |
---|
| 420 | } |
---|
| 421 | |
---|
[84] | 422 | |
---|
[77] | 423 | public Param build(String line) throws Exception { |
---|
| 424 | String[] paramEntryValues = line.split(","); |
---|
| 425 | |
---|
| 426 | if (paramEntryValues.length == 0) { |
---|
[84] | 427 | log.warn("field empty or wrong format (" + line |
---|
[77] | 428 | + ") - omitting"); |
---|
| 429 | return null; |
---|
| 430 | } |
---|
| 431 | |
---|
| 432 | for (int i = 0; i < paramEntryValues.length; ++i) { |
---|
| 433 | paramEntryValues[i] = paramEntryValues[i].trim(); |
---|
| 434 | } |
---|
| 435 | |
---|
| 436 | try { |
---|
[85] | 437 | id(paramEntryValues[0]); |
---|
| 438 | group(Integer.valueOf(paramEntryValues[1])); |
---|
| 439 | flags(Flags.read(paramEntryValues[2])); |
---|
| 440 | name(paramEntryValues[3]); |
---|
| 441 | type(paramEntryValues[4]); |
---|
| 442 | help(paramEntryValues[6]); |
---|
[77] | 443 | } catch (IndexOutOfBoundsException e) { |
---|
| 444 | /** everything is ok, parameters have just finished*/ |
---|
| 445 | } catch (NumberFormatException ex) { |
---|
[84] | 446 | log.warn("wrong format of entry: " + line |
---|
[77] | 447 | + ", omitting"); |
---|
| 448 | return null; |
---|
| 449 | } |
---|
[85] | 450 | return finish(); |
---|
[77] | 451 | } |
---|
| 452 | |
---|
| 453 | public void setField(String key, String value) { |
---|
[85] | 454 | switch (key) { |
---|
| 455 | case ID_FIELD: |
---|
| 456 | id(value); |
---|
| 457 | break; |
---|
| 458 | case NAME_FIELD: |
---|
| 459 | name(value); |
---|
| 460 | break; |
---|
| 461 | case TYPE_FIELD: |
---|
| 462 | type(value); |
---|
| 463 | break; |
---|
| 464 | case FLAGS_FIELD: |
---|
| 465 | flags(Flags.read(value)); |
---|
| 466 | break; |
---|
| 467 | case HELP_FIELD: |
---|
| 468 | help(value); |
---|
| 469 | break; |
---|
| 470 | case GROUP_FIELD: |
---|
| 471 | group(Integer.valueOf(value)); |
---|
| 472 | break; |
---|
| 473 | default: |
---|
| 474 | log.error("unknown field for Param: " + key); |
---|
| 475 | break; |
---|
[77] | 476 | } |
---|
| 477 | } |
---|
| 478 | |
---|
[88] | 479 | public ParamBuilder fillDef(Object def) { |
---|
[87] | 480 | if (this.def == null) { |
---|
| 481 | return def(def); |
---|
| 482 | } |
---|
| 483 | return this; |
---|
| 484 | } |
---|
[77] | 485 | |
---|
[88] | 486 | public ParamBuilder fillStorageType(Class<?> storageType) { |
---|
| 487 | if (this.storageType == null) { |
---|
| 488 | this.storageType = storageType; |
---|
| 489 | } |
---|
| 490 | return this; |
---|
| 491 | } |
---|
| 492 | |
---|
| 493 | public Class<?> getStorageType() { |
---|
| 494 | return storageType; |
---|
| 495 | } |
---|
[90] | 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 | } |
---|
[77] | 548 | } |
---|
[88] | 549 | |
---|