source: java/main/src/main/java/com/framsticks/params/ParamBuilder.java @ 88

Last change on this file since 88 was 88, checked in by psniegowski, 11 years ago

HIGHLIGHTS:

  • loading f0 schema with XmlLoader?
  • use XmlLoader? to load configuration
  • introduce unified fork-join model of various entities

(Instances, Connections, GUI Frames, etc.),
all those entities clean up gracefully on
shutdown, which may be initialized by user
or by some entity

  • basing on above, simplify several organizing classes

(Observer, main class)

(to host native frams server process from Java level)

CHANGELOG:
Remove redundant Observer class.

Clean up in AbstractJoinable?.

Update ExternalProcess? class to changes in joining model.

Another sweep through code with FindBugs?.

Find bug with not joining RemoteInstance?.

Joining almost works.

Much improved joining model.

More improvement to joining model.

Add logging messages around joinable operations.

Rename methods in AbstractJoinable?.

Improve Joinable.

Rewrite of entity structure.

More simplifications with entities.

Further improve joinables.

Let Frame compose from JFrame instead of inheriting.

Add join classes.

Improvements of closing.

Add Builder interface.

Add FramsServerTest?.xml

FramsServer? may be configured through xml.

Make Framsticks main class an Observer of Entities.

Make Observer a generic type.

Remove variables regarding to removed endpoint.

Simplify observer (remove endpoints).

More changes to Observer and Endpoint.

Minor improvements.

Add OutputListener? to ExternalProcess?.

Improve testing of ExternalProcess?.

Add ExternalProcess? runner.

Rename the Program class to Framsticks.

Migrate Program to use XmlLoader? configuration.

First steps with configuration using XmlLoader?.

Fix several bugs.

Move all f0 classes to apriopriate package.

XmlLoader? is able to load Schema.

XmlLoader? is loading classes and props.

Add GroupBuilder?.

File size: 9.3 KB
Line 
1package com.framsticks.params;
2
3import com.framsticks.params.annotations.FramsClassAnnotation;
4import com.framsticks.params.annotations.ParamAnnotation;
5import com.framsticks.params.types.*;
6import com.framsticks.util.Builder;
7import com.framsticks.util.FramsticksException;
8import com.framsticks.util.lang.Strings;
9
10import org.apache.log4j.Logger;
11
12import java.lang.reflect.InvocationTargetException;
13import java.util.Arrays;
14import java.util.List;
15
16/**
17 * The class ParamBuilder helps building Param objects.
18 *
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
25@FramsClassAnnotation(name = "prop", id = "prop")
26public class ParamBuilder implements Builder<Param> {
27        private final static Logger log = Logger.getLogger(ParamBuilder.class.getName());
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
36        /** The parameter id. */
37        private String id;
38
39        /**
40         * The parameter internal id. It's set by a user to use user's own id in
41         * code
42         */
43        private String internalId;
44
45        /** The number of group, that parameter belongs to. */
46        private Integer group = 0;
47
48        /** The flags stored as a bit sum. */
49        private Integer flags = 0;
50
51        /** The parameter name. */
52        private String name;
53
54        /** The help (description) concerning parameter. */
55        private String help;
56
57        /** The type of parameter. */
58        private Class<? extends Param> paramType;
59
60        private Object min;
61
62        private Object max;
63
64        private Object def;
65
66        private Integer extra;
67
68        String containedTypeName;
69
70        protected Class<?> storageType;
71
72        protected FramsClassBuilder classBuilder;
73
74        public ParamBuilder() {
75                this(null);
76        }
77
78
79        /**
80         * @param classBuilder
81         */
82        public ParamBuilder(FramsClassBuilder classBuilder) {
83                this.classBuilder = classBuilder;
84        }
85
86
87        /**
88         * @return the min
89         */
90        @ParamAnnotation
91        public Object getMin() {
92                return min;
93        }
94
95        /**
96         * @return the max
97         */
98        @ParamAnnotation
99        public Object getMax() {
100                return max;
101        }
102
103        /**
104         * @return the def
105         */
106        @ParamAnnotation
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        /**
137         * Build Param based on provided data.
138         *
139         * @return Param object
140         * @throws Exception
141         *             when Param getType is not defined
142         */
143        public Param finish() {
144                try {
145                        if (paramType == null) {
146                                throw new FramsticksException().msg("trying to finish incomplete param");
147                        }
148
149                        return paramType.getConstructor(ParamBuilder.class).newInstance(this);
150                } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | FramsticksException e) {
151                        throw new FramsticksException().msg("failed to create param").cause(e).arg("name", name);
152                }
153        }
154
155        @ParamAnnotation
156        public ParamBuilder id(String id) {
157                this.id = id;
158                return this;
159        }
160
161        public <T extends Param> ParamBuilder type(Class<T> type) {
162                assert type != null;
163                this.paramType = type;
164                return this;
165        }
166
167        // public <T extends Param> ParamBuilder type(T param) {
168        //      return internalSetType(param.getClass(), param);
169        // }
170
171
172        /**
173         * @return the id
174         */
175        @ParamAnnotation
176        public String getId() {
177                return id;
178        }
179
180        /**
181         * @return the internalId
182         */
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        @ParamAnnotation
193        public ParamBuilder group(Integer group) {
194                this.group = group;
195                return this;
196        }
197
198        @ParamAnnotation
199        public ParamBuilder flags(Integer flags) {
200                this.flags = flags;
201                return this;
202        }
203
204        @ParamAnnotation
205        public ParamBuilder name(String name) {
206                this.name = name;
207                return this;
208        }
209
210        protected <T extends Number> void parseMinMaxDefNumber(Class<T> type, String second, String third) {
211                if (second != null) {
212                        min = second;
213                }
214                if (third != null) {
215                        max = third;
216                }
217        }
218
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
230        @ParamAnnotation
231        public ParamBuilder type(String type) {
232                // typeString = type;
233                assert type != null;
234
235                log.trace("parsing type: " + type);
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': {
244                                containedTypeName = second != null ? second : first.substring(1);
245                                type(ObjectParam.class);
246                                break;
247                        }
248                        case 'p': {
249                                procedureSignature = type.substring(1);
250                                type(ProcedureParam.class);
251                                break;
252                        }
253                        case 'd': {
254
255                                int tildeIndex = type.indexOf("~");
256                                if (tildeIndex != -1) {
257                                        enums(Arrays.asList(type.substring(tildeIndex + 1).split("~")));
258                                } else {
259                                        if (first.length() >= 2) {
260                                                switch (first.charAt(1)) {
261                                                        case 'b': {
262                                                                type(BinaryParam.class);
263                                                                break;
264                                                        }
265                                                        case 'c': {
266                                                                type(ColorParam.class);
267                                                                break;
268                                                        }
269                                                        default: {
270                                                                log.error("unknown type: " + first);
271                                                                return this;
272                                                        }
273                                                }
274                                        }
275                                        if ("0".equals(second) && "1".equals(third)) {
276                                                type(BooleanParam.class);
277                                        }
278                                        if (paramType == null) {
279                                                type(DecimalParam.class);
280                                        }
281                                }
282                                if (DecimalParam.class.isAssignableFrom(this.paramType)) {
283                                        parseMinMaxDefNumber(Integer.class, second, third);
284                                }
285                                break;
286                        }
287                        case 'f': {
288                                type(FloatParam.class);
289                                parseMinMaxDefNumber(Double.class, second, third);
290                                break;
291                        }
292                        case 'x': {
293                                type(UniversalParam.class);
294                                break;
295                        }
296                        case 's': {
297                                type(StringParam.class);
298                                min(second);
299                                max(third);
300                                break;
301                        }
302                        case 'e': {
303                                type(EventParam.class);
304                                break;
305                        }
306                        case 'l': {
307                                containedTypeName = second;
308                                if (third != null) {
309                                        type(UniqueListParam.class);
310                                        uid = third;
311                                } else {
312                                        type(ArrayListParam.class);
313                                }
314                                break;
315                        }
316                        default:{
317                                log.error("unknown type: " + first);
318                                return this;
319                        }
320                }
321                return this;
322        }
323
324        @ParamAnnotation
325        public ParamBuilder help(String help) {
326                this.help = help;
327                return this;
328        }
329
330        /**
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() {
364                return "?";
365        }
366
367        @ParamAnnotation(id = "xtra")
368        public Integer getExtra() {
369                return extra;
370        }
371
372        /**
373         * @return the paramType
374         */
375        public Class<? extends Param> getParamType() {
376                return paramType;
377        }
378
379        @ParamAnnotation(id = "xtra")
380        public ParamBuilder extra(Integer extra) {
381                this.extra = extra;
382                return this;
383        }
384
385        @ParamAnnotation
386        public ParamBuilder min(Object min) {
387                this.min = min;
388                return this;
389        }
390
391        @ParamAnnotation
392        public ParamBuilder max(Object max) {
393                this.max = max;
394                return this;
395        }
396
397        @ParamAnnotation
398        public ParamBuilder def(Object def) {
399                this.def = def;
400                return this;
401        }
402
403
404        public Param build(String line) throws Exception {
405                String[] paramEntryValues = line.split(",");
406
407                if (paramEntryValues.length == 0) {
408                        log.warn("field empty or wrong format (" + line
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 {
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]);
424                } catch (IndexOutOfBoundsException e) {
425                        /** everything is ok, parameters have just finished*/
426                } catch (NumberFormatException ex) {
427                        log.warn("wrong format of entry: " + line
428                                        + ", omitting");
429                        return null;
430                }
431                return finish();
432        }
433
434        public void setField(String key, String value) {
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;
457                }
458        }
459
460        public ParamBuilder fillDef(Object def) {
461                if (this.def == null) {
462                        return def(def);
463                }
464                return this;
465        }
466
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        }
478}
479
Note: See TracBrowser for help on using the repository browser.