source: java/main/src/main/java/com/framsticks/params/PrimitiveParam.java @ 102

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

HIGHLIGHTS:

for Joinables running

CHANGELOG:
Add WorkPackageLogic? and classes representing prime experiment state.

Add classes for PrimeExperiment? state.

Extract single netload routine in Simulator.

Working netload with dummy content in PrimeExperiment?.

More development with NetLoadSaveLogic? and PrimeExperiment?.

Improvement around prime.

Improve BufferedDispatcher?.isActive logic.

Add prime-all.xml configuration.

Manual connecting to existing simulators from GUI.

Guard in SimulatorConnector? against expdef mismatch.

Guard against empty target dispatcher in BufferedDispatcher?.

Make BufferedDispatcher? a Dispatcher (and Joinable).

Minor improvements.

Done StackedJoinable?, improve Experiment.

Develop StackedJoinable?.

Add StackedJoinable? utility joinables controller.

Add dependency on apache-commons-lang.

Add ready ListChange? on Simulators.

Improve hints in ListChange?.

Several improvements.

Found bug with dispatching in Experiment.

Minor improvements.

Fix bug with early finishing Server.

Many changes in Dispatching.

Fix bug with connection.

Do not obfuscate log with socket related exceptions.

Add SocketClosedException?.

Add SimulatorConnector?.

Work out conception of experiment composing of logics building blocks.

Rename SinkInterface? to Sink.

Move saving of Accesses into AccessOperations?.

Some improvements to Experiment.

Improve joinables.

Fix issue with joinables closing.

Add direct and managed consoles to popup menu.

File size: 2.8 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(Sink 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
99}
Note: See TracBrowser for help on using the repository browser.