[77] | 1 | package com.framsticks.params; |
---|
| 2 | |
---|
| 3 | import com.framsticks.params.types.*; |
---|
[84] | 4 | import com.framsticks.util.lang.Numbers; |
---|
| 5 | |
---|
[77] | 6 | import org.apache.log4j.Logger; |
---|
| 7 | |
---|
| 8 | import java.util.ArrayList; |
---|
| 9 | import java.util.Arrays; |
---|
| 10 | |
---|
| 11 | /** |
---|
| 12 | * The class ParamBuilder helps building Param objects. |
---|
[84] | 13 | * |
---|
[77] | 14 | * @author Mateusz Jarus <name.surname@gmail.com> (please replace name and |
---|
| 15 | * surname with my personal data) |
---|
| 16 | * |
---|
| 17 | * @author Piotr Śniegowski |
---|
| 18 | */ |
---|
| 19 | |
---|
| 20 | public class ParamBuilder { |
---|
[84] | 21 | private final static Logger log = Logger.getLogger(ParamBuilder.class.getName()); |
---|
[77] | 22 | |
---|
| 23 | private static final String ID_FIELD = "id"; |
---|
| 24 | private static final String NAME_FIELD = "name"; |
---|
| 25 | private static final String HELP_FIELD = "help"; |
---|
| 26 | private static final String GROUP_FIELD = "group"; |
---|
| 27 | private static final String TYPE_FIELD = "type"; |
---|
| 28 | private static final String FLAGS_FIELD = "flags"; |
---|
| 29 | |
---|
| 30 | /** The parameter getId. */ |
---|
| 31 | private String id; |
---|
| 32 | |
---|
| 33 | /** |
---|
| 34 | * The parameter internal getId. It's set by a user to use user's own getId in |
---|
| 35 | * code |
---|
| 36 | */ |
---|
| 37 | private String internalId; |
---|
| 38 | |
---|
| 39 | /** The number of group, that parameter belongs to. */ |
---|
| 40 | private Integer group = 0; |
---|
| 41 | |
---|
| 42 | /** The getFlags stored as a bit sum. */ |
---|
| 43 | private Integer flags = 0; |
---|
| 44 | |
---|
| 45 | /** The parameter getName. */ |
---|
| 46 | private String name; |
---|
| 47 | |
---|
| 48 | /** The getHelp (description) concerning parameter. */ |
---|
| 49 | private String help; |
---|
| 50 | |
---|
| 51 | /** The getType of parameter. */ |
---|
[84] | 52 | private Class<? extends Param> paramType; |
---|
[77] | 53 | |
---|
| 54 | private Param param; |
---|
[84] | 55 | private PrimitiveParam primitiveParam; |
---|
[77] | 56 | |
---|
[85] | 57 | ParamBuilder() { |
---|
| 58 | } |
---|
| 59 | |
---|
[77] | 60 | /** |
---|
| 61 | * Build Param based on provided data. |
---|
[84] | 62 | * |
---|
[77] | 63 | * @return Param object |
---|
| 64 | * @throws Exception |
---|
| 65 | * when Param getType is not defined |
---|
| 66 | */ |
---|
[85] | 67 | public Param finish() { |
---|
[84] | 68 | assert param != null; |
---|
[77] | 69 | param.id = id; |
---|
| 70 | param.name = name; |
---|
| 71 | param.help = help; |
---|
| 72 | param.group = group; |
---|
| 73 | param.flags = flags; |
---|
| 74 | param.internalId = internalId; |
---|
| 75 | return param; |
---|
| 76 | } |
---|
| 77 | |
---|
[85] | 78 | public ParamBuilder id(String id) { |
---|
[77] | 79 | this.id = id; |
---|
| 80 | return this; |
---|
| 81 | } |
---|
| 82 | |
---|
[84] | 83 | protected <T extends Param> ParamBuilder internalSetType(Class<? extends Param> type, T param) { |
---|
| 84 | this.paramType = type; |
---|
| 85 | this.param = param; |
---|
| 86 | if (param instanceof PrimitiveParam) { |
---|
| 87 | primitiveParam = (PrimitiveParam) param; |
---|
| 88 | } |
---|
| 89 | return this; |
---|
| 90 | } |
---|
| 91 | |
---|
[85] | 92 | public <T extends Param> ParamBuilder type(Class<T> type) { |
---|
[77] | 93 | try { |
---|
[84] | 94 | return internalSetType(type, type.newInstance()); |
---|
[85] | 95 | } catch (InstantiationException | IllegalAccessException e) { |
---|
[77] | 96 | e.printStackTrace(); |
---|
| 97 | } |
---|
| 98 | return this; |
---|
| 99 | } |
---|
| 100 | |
---|
[85] | 101 | public <T extends Param> ParamBuilder type(T param) { |
---|
[84] | 102 | return internalSetType(param.getClass(), param); |
---|
[77] | 103 | } |
---|
| 104 | |
---|
| 105 | |
---|
| 106 | public ParamBuilder setInternalId(String internalId) { |
---|
| 107 | this.internalId = internalId; |
---|
| 108 | return this; |
---|
| 109 | } |
---|
| 110 | |
---|
[85] | 111 | public ParamBuilder group(Integer group) { |
---|
[77] | 112 | this.group = group; |
---|
| 113 | return this; |
---|
| 114 | } |
---|
| 115 | |
---|
[85] | 116 | public ParamBuilder flags(Integer flags) { |
---|
[77] | 117 | this.flags = flags; |
---|
| 118 | return this; |
---|
| 119 | } |
---|
| 120 | |
---|
[85] | 121 | public ParamBuilder name(String name) { |
---|
[77] | 122 | this.name = name; |
---|
| 123 | return this; |
---|
| 124 | } |
---|
| 125 | |
---|
[84] | 126 | protected <T extends Number> void parseMinMaxDefNumber(Class<T> type, String second, String third) { |
---|
| 127 | if (primitiveParam == null) { |
---|
| 128 | log.warn("param is not a primitive param"); |
---|
| 129 | return; |
---|
| 130 | } |
---|
| 131 | if (second != null) { |
---|
| 132 | this.primitiveParam.min = Numbers.parse(second, type); |
---|
| 133 | if (this.primitiveParam.min == null) { |
---|
| 134 | log.warn("value of min attribute was invalid"); |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | if (third != null) { |
---|
| 138 | this.primitiveParam.max = Numbers.parse(third, type); |
---|
| 139 | if (this.primitiveParam.max == null) { |
---|
| 140 | log.warn("value of min attribute was invalid"); |
---|
| 141 | } |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | |
---|
[85] | 145 | public ParamBuilder type(String type) { |
---|
[77] | 146 | |
---|
[84] | 147 | log.trace("parsing type: " + type); |
---|
[77] | 148 | |
---|
| 149 | String[] typeSplitted = type.split(" "); |
---|
| 150 | String first = typeSplitted[0]; |
---|
| 151 | String second = typeSplitted.length > 1 ? typeSplitted[1] : null; |
---|
| 152 | String third = typeSplitted.length > 2 ? typeSplitted[2] : null; |
---|
| 153 | |
---|
| 154 | switch (first.charAt(0)) { |
---|
| 155 | case 'o': { |
---|
[85] | 156 | type(new ObjectParam(second != null ? second : first.substring(1))); |
---|
[77] | 157 | break; |
---|
| 158 | } |
---|
| 159 | case 'p': { |
---|
| 160 | ProcedureParam procedureParam = new ProcedureParam(); |
---|
| 161 | String signature = type.substring(1); |
---|
| 162 | try { |
---|
| 163 | procedureParam.parseSignature(signature); |
---|
| 164 | } catch (Exception e) { |
---|
[84] | 165 | log.error("invalid procedure signature '" + signature + "': " + e); |
---|
[77] | 166 | } |
---|
[85] | 167 | type(procedureParam); |
---|
[77] | 168 | break; |
---|
| 169 | } |
---|
| 170 | case 'd': { |
---|
| 171 | |
---|
| 172 | int tildeIndex = type.indexOf("~"); |
---|
| 173 | if (tildeIndex != -1) { |
---|
[85] | 174 | type(new EnumParam(new ArrayList<String>(Arrays.asList(type.substring(tildeIndex + 1).split("~"))))); |
---|
[77] | 175 | } else { |
---|
| 176 | if (first.length() >= 2) { |
---|
[85] | 177 | switch (first.charAt(1)) { |
---|
[77] | 178 | case 'b': { |
---|
[85] | 179 | type(BinaryParam.class); |
---|
[77] | 180 | break; |
---|
| 181 | } |
---|
| 182 | case 'c': { |
---|
[85] | 183 | type(ColorParam.class); |
---|
[77] | 184 | break; |
---|
| 185 | } |
---|
[85] | 186 | default: { |
---|
| 187 | log.error("unknown type: " + first); |
---|
| 188 | return this; |
---|
| 189 | } |
---|
[77] | 190 | } |
---|
| 191 | } |
---|
[84] | 192 | if ("0".equals(second) && "1".equals(third)) { |
---|
[85] | 193 | type(BooleanParam.class); |
---|
[84] | 194 | } |
---|
[77] | 195 | if (param == null) { |
---|
[85] | 196 | type(DecimalParam.class); |
---|
[77] | 197 | } |
---|
| 198 | } |
---|
| 199 | if (this.param instanceof DecimalParam) { |
---|
[84] | 200 | parseMinMaxDefNumber(Integer.class, second, third); |
---|
[77] | 201 | } |
---|
| 202 | break; |
---|
| 203 | } |
---|
| 204 | case 'f': { |
---|
[85] | 205 | type(FloatParam.class); |
---|
[84] | 206 | parseMinMaxDefNumber(Double.class, second, third); |
---|
[77] | 207 | break; |
---|
| 208 | } |
---|
| 209 | case 'x': { |
---|
[85] | 210 | type(UniversalParam.class); |
---|
[77] | 211 | break; |
---|
| 212 | } |
---|
| 213 | case 's': { |
---|
[85] | 214 | type(StringParam.class); |
---|
| 215 | min(second); |
---|
| 216 | max(third); |
---|
[77] | 217 | break; |
---|
| 218 | } |
---|
| 219 | case 'e': { |
---|
[85] | 220 | type(EventParam.class); |
---|
[77] | 221 | break; |
---|
| 222 | } |
---|
| 223 | case 'l': { |
---|
[85] | 224 | type(third != null ? new UniqueListParam(second, third) : new ArrayListParam(second)); |
---|
[77] | 225 | break; |
---|
| 226 | } |
---|
[85] | 227 | default:{ |
---|
| 228 | log.error("unknown type: " + first); |
---|
| 229 | return this; |
---|
| 230 | } |
---|
[77] | 231 | } |
---|
| 232 | return this; |
---|
| 233 | } |
---|
| 234 | |
---|
[85] | 235 | public ParamBuilder help(String help) { |
---|
[77] | 236 | this.help = help; |
---|
| 237 | return this; |
---|
| 238 | } |
---|
| 239 | |
---|
[84] | 240 | /** |
---|
| 241 | * @return the paramType |
---|
| 242 | */ |
---|
| 243 | public Class<? extends Param> getParamType() { |
---|
| 244 | return paramType; |
---|
| 245 | } |
---|
| 246 | |
---|
[85] | 247 | public <T> ParamBuilder min(T min) { |
---|
[84] | 248 | if (primitiveParam != null) { |
---|
| 249 | this.primitiveParam.min = min; |
---|
| 250 | } |
---|
[77] | 251 | return this; |
---|
| 252 | } |
---|
| 253 | |
---|
[85] | 254 | public <T> ParamBuilder max(T max) { |
---|
[84] | 255 | if (primitiveParam != null) { |
---|
| 256 | this.primitiveParam.max = max; |
---|
| 257 | } |
---|
[77] | 258 | return this; |
---|
| 259 | } |
---|
| 260 | |
---|
[85] | 261 | public <T> ParamBuilder def(T def) { |
---|
[84] | 262 | if (def != null && primitiveParam != null) { |
---|
| 263 | this.primitiveParam.def = def; |
---|
| 264 | } |
---|
[77] | 265 | return this; |
---|
| 266 | } |
---|
| 267 | |
---|
[84] | 268 | public Class<?> getStorageType() { |
---|
| 269 | assert param != null; |
---|
| 270 | return param.getStorageType(); |
---|
| 271 | } |
---|
| 272 | |
---|
[77] | 273 | public Param build(String line) throws Exception { |
---|
| 274 | String[] paramEntryValues = line.split(","); |
---|
| 275 | |
---|
| 276 | if (paramEntryValues.length == 0) { |
---|
[84] | 277 | log.warn("field empty or wrong format (" + line |
---|
[77] | 278 | + ") - omitting"); |
---|
| 279 | return null; |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | for (int i = 0; i < paramEntryValues.length; ++i) { |
---|
| 283 | paramEntryValues[i] = paramEntryValues[i].trim(); |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | try { |
---|
[85] | 287 | id(paramEntryValues[0]); |
---|
| 288 | group(Integer.valueOf(paramEntryValues[1])); |
---|
| 289 | flags(Flags.read(paramEntryValues[2])); |
---|
| 290 | name(paramEntryValues[3]); |
---|
| 291 | type(paramEntryValues[4]); |
---|
| 292 | help(paramEntryValues[6]); |
---|
[77] | 293 | } catch (IndexOutOfBoundsException e) { |
---|
| 294 | /** everything is ok, parameters have just finished*/ |
---|
| 295 | } catch (NumberFormatException ex) { |
---|
[84] | 296 | log.warn("wrong format of entry: " + line |
---|
[77] | 297 | + ", omitting"); |
---|
| 298 | return null; |
---|
| 299 | } |
---|
[85] | 300 | return finish(); |
---|
[77] | 301 | } |
---|
| 302 | |
---|
| 303 | public void setField(String key, String value) { |
---|
[85] | 304 | switch (key) { |
---|
| 305 | case ID_FIELD: |
---|
| 306 | id(value); |
---|
| 307 | break; |
---|
| 308 | case NAME_FIELD: |
---|
| 309 | name(value); |
---|
| 310 | break; |
---|
| 311 | case TYPE_FIELD: |
---|
| 312 | type(value); |
---|
| 313 | break; |
---|
| 314 | case FLAGS_FIELD: |
---|
| 315 | flags(Flags.read(value)); |
---|
| 316 | break; |
---|
| 317 | case HELP_FIELD: |
---|
| 318 | help(value); |
---|
| 319 | break; |
---|
| 320 | case GROUP_FIELD: |
---|
| 321 | group(Integer.valueOf(value)); |
---|
| 322 | break; |
---|
| 323 | default: |
---|
| 324 | log.error("unknown field for Param: " + key); |
---|
| 325 | break; |
---|
[77] | 326 | } |
---|
| 327 | } |
---|
| 328 | |
---|
| 329 | |
---|
| 330 | public static FramsClass getFramsClass() { |
---|
| 331 | return new FramsClass("prop", "prop", null) |
---|
[85] | 332 | .append(Param.build().id("name").name("Name").type(StringParam.class)) |
---|
| 333 | .append(Param.build().id("id").name("Id").type(StringParam.class)) |
---|
| 334 | .append(Param.build().id("type").name("Type").type(StringParam.class)) |
---|
| 335 | .append(Param.build().id("help").name("Help").type(StringParam.class)) |
---|
| 336 | .append(Param.build().id("group").name("Group").type(DecimalParam.class)) |
---|
| 337 | .append(Param.build().id("flags").name("Flags").type(DecimalParam.class)); |
---|
[77] | 338 | } |
---|
| 339 | |
---|
| 340 | } |
---|