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