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

Last change on this file was 193, checked in by Maciej Komosinski, 10 years ago

Set svn:eol-style native for all textual files

  • Property svn:eol-style set to native
File size: 3.0 KB
Line 
1package com.framsticks.params;
2
3import com.framsticks.util.FramsticksException;
4import com.framsticks.util.Misc;
5import com.framsticks.util.lang.Numbers;
6import com.framsticks.util.lang.Strings;
7
8import javax.annotation.concurrent.Immutable;
9
10/**
11 * @author Piotr Sniegowski
12 */
13@Immutable
14public abstract class PrimitiveParam<S> extends ValueParam {
15
16        /** The minimum allowed value of parameter. */
17        protected final Object min;
18
19        /** The maximum allowed value of parameter. */
20        protected final Object max;
21
22        /** The default value of parameter. */
23        protected final Object def;
24
25        /**
26         * @param builder
27         */
28        public PrimitiveParam(ParamBuilder builder) {
29                super(builder);
30                Class<?> storageType = Misc.returnNotNull(builder.getStorageType(), Object.class);
31                min = tryCastAndReturn(builder.getMin(), storageType);
32                max = tryCastAndReturn(builder.getMax(), storageType);
33                def = tryCastAndReturn(builder.getDef(), storageType);
34        }
35
36        // public void save(Sink sink, Object value) {
37        //      sink.print(value);
38        // }
39
40        protected <T> T tryCastAndReturn(Object value, Class<T> type) {
41                if (value == null) {
42                        return null;
43                }
44                try {
45                        if (type.equals(Object.class)) {
46                                return type.cast(value);
47                        }
48                        if (value instanceof String) {
49                                if (type.equals(String.class)) {
50                                        return type.cast(value);
51                                }
52                                return Numbers.parse((String) value, type);
53                        } if ((value instanceof Integer) && (type.equals(Double.class))) {
54                                return type.cast(new Double((Integer) value));
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).arg("value", value);
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        public <T> String serialize(T value) {
100                return Strings.toStringNullProof(value);
101        }
102
103}
Note: See TracBrowser for help on using the repository browser.