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