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