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

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

HIGHLIGHTS:

  • import refactorization: move Tree, Path, etc.

from core to structure package

  • initial serialization implementation
  • improve PrimeExperiment? test
  • many organizational changes and convenience improvements

CHANGELOG:
Make registry in AbstractTree? final.

Move most classes from core to structure package.

Minor changes.

Switch names of Future and FutureHandler?.

Rename ExceptionResultHandler? to ExceptionHandler?.

Rename ExceptionHandler? to ExceptionDispatcherHandler?.

Fix bug in ParamCandidate? cache.

Add missing synchronization to the BufferedDispatcher?.

Develop @Serialized support.

Rework serialization further.

Add serialization/deserialization interface to ValueParam?.

Move getStorageType and isNumeric from Param down to params hierarchy.

Minor changes.

Improve param type induction.

Add TestSerializedClass? for testing new serialization.

Add info files gor GenePool? and Population.

Add standard.expt exemplary netfile.

Add type name field to PropertiesObject?.

Use PropertiesObject? for PropertiesAccess? instead of ordinary map.

Hide getFramsClass is several more places.

More unification accross FramsClass?, Access and Path.

Add ParamCollection?.

Simplify interface for getting params from FramsClass?, Access
or Path.

Make Access.call() interface variadic.

Add arguments(args) convenience wrapper around new Object[] {args}.

Upgrade to apache.commons.lang version 3.1

Minor improvement with Response constructors.

Develop proper result printing in ClientAtServer?.

Add experimentNetsave to PrimeExperiment?.

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