1 | package com.framsticks.params; |
---|
2 | |
---|
3 | import com.framsticks.util.FramsticksException; |
---|
4 | import com.framsticks.util.Misc; |
---|
5 | import com.framsticks.util.lang.Numbers; |
---|
6 | |
---|
7 | import javax.annotation.concurrent.Immutable; |
---|
8 | |
---|
9 | /** |
---|
10 | * @author Piotr Sniegowski |
---|
11 | */ |
---|
12 | @Immutable |
---|
13 | public 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 | } |
---|