source: java/main/src/main/java/com/framsticks/params/PrimitiveParam.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: 2.9 KB
Line 
1package com.framsticks.params;
2
3import com.framsticks.util.FramsticksException;
4import com.framsticks.util.Misc;
5import com.framsticks.util.lang.Numbers;
6
7import javax.annotation.concurrent.Immutable;
8
9/**
10 * @author Piotr Sniegowski
11 */
12@Immutable
13public abstract class PrimitiveParam<S> extends ValueParam {
14
15        /** The minimum allowed value of parameter. */
16        protected final Object min;
17
18        /** The maximum allowed value of parameter. */
19        protected final Object max;
20
21        /** The default value of parameter. */
22        protected final Object def;
23
24        /**
25         * @param builder
26         */
27        public PrimitiveParam(ParamBuilder builder) {
28                super(builder);
29                Class<?> storageType = Misc.returnNotNull(builder.getStorageType(), Object.class);
30                min = tryCastAndReturn(builder.getMin(), storageType);
31                max = tryCastAndReturn(builder.getMax(), storageType);
32                def = tryCastAndReturn(builder.getDef(), storageType);
33        }
34
35        public void save(SinkInterface sink, Object value) {
36                sink.print(value);
37        }
38
39        protected <T> T tryCastAndReturn(Object value, Class<T> type) {
40                if (value == null) {
41                        return null;
42                }
43                try {
44                        if (type.equals(Object.class)) {
45                                return type.cast(value);
46                        }
47                        if (value instanceof String) {
48                                if (type.equals(String.class)) {
49                                        return type.cast(value);
50                                }
51                                // if (Number.class.isAssignableFrom(type)) {
52                                return Numbers.parse((String) value, type);
53                                // }
54                                // return null;
55                        } else {
56                                return type.cast(value);
57                        }
58                } catch (ClassCastException e) {
59                        throw new FramsticksException().msg("failed to cast").cause(e).arg("param", this).arg("actual", value.getClass()).arg("requested", type);
60                }
61        }
62
63        /**
64         * Gets the minimum value of parameter.
65         *
66         * @param <T> the generic getType which must be correctly specified by user
67         * @param type the getType of variable, can be checked by
68         * @return the minimum allowed value of parameter
69         * @throws ClassCastException the class cast exception, thrown when given getType is incorrect
70         */
71        public <T> T getMin(Class<T> type) {
72                return tryCastAndReturn(min, type);
73        }
74
75        /**
76         * Gets the maximum value of parameter.
77         *
78         * @param <T> the generic getType which must be correctly specified by user
79         * @param type the getType of variable, can be checked by {@link #getStorageType()}
80         * @return the maximum allowed value of parameter
81         * @throws ClassCastException the class cast exception, thrown when given getType is incorrect
82         */
83        public <T> T getMax(Class<T> type) {
84                return tryCastAndReturn(max, type);
85        }
86
87        /**
88         * Gets the default value of parameter.
89         *
90         * @param <T> the generic getType which must be correctly specified by user
91         * @param type the getType of variable, can be checked by {@link #getStorageType()}
92         * @return the default value of parameter
93         * @throws ClassCastException the class cast exception, thrown when given getType is incorrect
94         */
95        public <T> T getDef(Class<T> type) {
96                return tryCastAndReturn(def, type);
97        }
98}
Note: See TracBrowser for help on using the repository browser.