1 | package com.framsticks.params; |
---|
2 | |
---|
3 | import com.framsticks.params.types.*; |
---|
4 | import com.framsticks.util.lang.Numbers; |
---|
5 | |
---|
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. |
---|
13 | * |
---|
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 { |
---|
21 | private final static Logger log = Logger.getLogger(ParamBuilder.class.getName()); |
---|
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. */ |
---|
52 | private Class<? extends Param> paramType; |
---|
53 | |
---|
54 | private Param param; |
---|
55 | private PrimitiveParam primitiveParam; |
---|
56 | |
---|
57 | ParamBuilder() { |
---|
58 | } |
---|
59 | |
---|
60 | /** |
---|
61 | * Build Param based on provided data. |
---|
62 | * |
---|
63 | * @return Param object |
---|
64 | * @throws Exception |
---|
65 | * when Param getType is not defined |
---|
66 | */ |
---|
67 | public Param finish() { |
---|
68 | assert param != null; |
---|
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 | |
---|
78 | public ParamBuilder id(String id) { |
---|
79 | this.id = id; |
---|
80 | return this; |
---|
81 | } |
---|
82 | |
---|
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 | |
---|
92 | public <T extends Param> ParamBuilder type(Class<T> type) { |
---|
93 | try { |
---|
94 | return internalSetType(type, type.newInstance()); |
---|
95 | } catch (InstantiationException | IllegalAccessException e) { |
---|
96 | e.printStackTrace(); |
---|
97 | } |
---|
98 | return this; |
---|
99 | } |
---|
100 | |
---|
101 | public <T extends Param> ParamBuilder type(T param) { |
---|
102 | return internalSetType(param.getClass(), param); |
---|
103 | } |
---|
104 | |
---|
105 | |
---|
106 | public ParamBuilder setInternalId(String internalId) { |
---|
107 | this.internalId = internalId; |
---|
108 | return this; |
---|
109 | } |
---|
110 | |
---|
111 | public ParamBuilder group(Integer group) { |
---|
112 | this.group = group; |
---|
113 | return this; |
---|
114 | } |
---|
115 | |
---|
116 | public ParamBuilder flags(Integer flags) { |
---|
117 | this.flags = flags; |
---|
118 | return this; |
---|
119 | } |
---|
120 | |
---|
121 | public ParamBuilder name(String name) { |
---|
122 | this.name = name; |
---|
123 | return this; |
---|
124 | } |
---|
125 | |
---|
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 | |
---|
145 | public ParamBuilder type(String type) { |
---|
146 | |
---|
147 | log.trace("parsing type: " + type); |
---|
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': { |
---|
156 | type(new ObjectParam(second != null ? second : first.substring(1))); |
---|
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) { |
---|
165 | log.error("invalid procedure signature '" + signature + "': " + e); |
---|
166 | } |
---|
167 | type(procedureParam); |
---|
168 | break; |
---|
169 | } |
---|
170 | case 'd': { |
---|
171 | |
---|
172 | int tildeIndex = type.indexOf("~"); |
---|
173 | if (tildeIndex != -1) { |
---|
174 | type(new EnumParam(new ArrayList<String>(Arrays.asList(type.substring(tildeIndex + 1).split("~"))))); |
---|
175 | } else { |
---|
176 | if (first.length() >= 2) { |
---|
177 | switch (first.charAt(1)) { |
---|
178 | case 'b': { |
---|
179 | type(BinaryParam.class); |
---|
180 | break; |
---|
181 | } |
---|
182 | case 'c': { |
---|
183 | type(ColorParam.class); |
---|
184 | break; |
---|
185 | } |
---|
186 | default: { |
---|
187 | log.error("unknown type: " + first); |
---|
188 | return this; |
---|
189 | } |
---|
190 | } |
---|
191 | } |
---|
192 | if ("0".equals(second) && "1".equals(third)) { |
---|
193 | type(BooleanParam.class); |
---|
194 | } |
---|
195 | if (param == null) { |
---|
196 | type(DecimalParam.class); |
---|
197 | } |
---|
198 | } |
---|
199 | if (this.param instanceof DecimalParam) { |
---|
200 | parseMinMaxDefNumber(Integer.class, second, third); |
---|
201 | } |
---|
202 | break; |
---|
203 | } |
---|
204 | case 'f': { |
---|
205 | type(FloatParam.class); |
---|
206 | parseMinMaxDefNumber(Double.class, second, third); |
---|
207 | break; |
---|
208 | } |
---|
209 | case 'x': { |
---|
210 | type(UniversalParam.class); |
---|
211 | break; |
---|
212 | } |
---|
213 | case 's': { |
---|
214 | type(StringParam.class); |
---|
215 | min(second); |
---|
216 | max(third); |
---|
217 | break; |
---|
218 | } |
---|
219 | case 'e': { |
---|
220 | type(EventParam.class); |
---|
221 | break; |
---|
222 | } |
---|
223 | case 'l': { |
---|
224 | type(third != null ? new UniqueListParam(second, third) : new ArrayListParam(second)); |
---|
225 | break; |
---|
226 | } |
---|
227 | default:{ |
---|
228 | log.error("unknown type: " + first); |
---|
229 | return this; |
---|
230 | } |
---|
231 | } |
---|
232 | return this; |
---|
233 | } |
---|
234 | |
---|
235 | public ParamBuilder help(String help) { |
---|
236 | this.help = help; |
---|
237 | return this; |
---|
238 | } |
---|
239 | |
---|
240 | /** |
---|
241 | * @return the paramType |
---|
242 | */ |
---|
243 | public Class<? extends Param> getParamType() { |
---|
244 | return paramType; |
---|
245 | } |
---|
246 | |
---|
247 | public <T> ParamBuilder min(T min) { |
---|
248 | if (primitiveParam != null) { |
---|
249 | this.primitiveParam.min = min; |
---|
250 | } |
---|
251 | return this; |
---|
252 | } |
---|
253 | |
---|
254 | public <T> ParamBuilder max(T max) { |
---|
255 | if (primitiveParam != null) { |
---|
256 | this.primitiveParam.max = max; |
---|
257 | } |
---|
258 | return this; |
---|
259 | } |
---|
260 | |
---|
261 | public <T> ParamBuilder def(T def) { |
---|
262 | if (def != null && primitiveParam != null) { |
---|
263 | this.primitiveParam.def = def; |
---|
264 | } |
---|
265 | return this; |
---|
266 | } |
---|
267 | |
---|
268 | public Class<?> getStorageType() { |
---|
269 | assert param != null; |
---|
270 | return param.getStorageType(); |
---|
271 | } |
---|
272 | |
---|
273 | public Param build(String line) throws Exception { |
---|
274 | String[] paramEntryValues = line.split(","); |
---|
275 | |
---|
276 | if (paramEntryValues.length == 0) { |
---|
277 | log.warn("field empty or wrong format (" + line |
---|
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 { |
---|
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]); |
---|
293 | } catch (IndexOutOfBoundsException e) { |
---|
294 | /** everything is ok, parameters have just finished*/ |
---|
295 | } catch (NumberFormatException ex) { |
---|
296 | log.warn("wrong format of entry: " + line |
---|
297 | + ", omitting"); |
---|
298 | return null; |
---|
299 | } |
---|
300 | return finish(); |
---|
301 | } |
---|
302 | |
---|
303 | public void setField(String key, String value) { |
---|
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; |
---|
326 | } |
---|
327 | } |
---|
328 | |
---|
329 | |
---|
330 | public static FramsClass getFramsClass() { |
---|
331 | return new FramsClass("prop", "prop", null) |
---|
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)); |
---|
338 | } |
---|
339 | |
---|
340 | } |
---|