[84] | 1 | package com.framsticks.params; |
---|
| 2 | |
---|
[87] | 3 | import com.framsticks.util.FramsticksException; |
---|
[88] | 4 | import com.framsticks.util.Misc; |
---|
[87] | 5 | import com.framsticks.util.lang.Numbers; |
---|
[105] | 6 | import com.framsticks.util.lang.Strings; |
---|
[84] | 7 | |
---|
[87] | 8 | import javax.annotation.concurrent.Immutable; |
---|
| 9 | |
---|
[84] | 10 | /** |
---|
| 11 | * @author Piotr Sniegowski |
---|
| 12 | */ |
---|
[87] | 13 | @Immutable |
---|
| 14 | public abstract class PrimitiveParam<S> extends ValueParam { |
---|
[84] | 15 | |
---|
| 16 | /** The minimum allowed value of parameter. */ |
---|
[87] | 17 | protected final Object min; |
---|
[84] | 18 | |
---|
| 19 | /** The maximum allowed value of parameter. */ |
---|
[87] | 20 | protected final Object max; |
---|
[84] | 21 | |
---|
| 22 | /** The default value of parameter. */ |
---|
[87] | 23 | protected final Object def; |
---|
[84] | 24 | |
---|
[87] | 25 | /** |
---|
| 26 | * @param builder |
---|
| 27 | */ |
---|
| 28 | public PrimitiveParam(ParamBuilder builder) { |
---|
| 29 | super(builder); |
---|
[88] | 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); |
---|
[87] | 34 | } |
---|
| 35 | |
---|
[105] | 36 | // public void save(Sink sink, Object value) { |
---|
| 37 | // sink.print(value); |
---|
| 38 | // } |
---|
[84] | 39 | |
---|
| 40 | protected <T> T tryCastAndReturn(Object value, Class<T> type) { |
---|
[87] | 41 | if (value == null) { |
---|
[84] | 42 | return null; |
---|
[87] | 43 | } |
---|
[84] | 44 | try { |
---|
[87] | 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); |
---|
[107] | 53 | } if ((value instanceof Integer) && (type.equals(Double.class))) { |
---|
| 54 | return type.cast(new Double((Integer) value)); |
---|
[87] | 55 | } else { |
---|
| 56 | return type.cast(value); |
---|
| 57 | } |
---|
[84] | 58 | } catch (ClassCastException e) { |
---|
[107] | 59 | throw new FramsticksException().msg("failed to cast").cause(e).arg("param", this).arg("actual", value.getClass()).arg("requested", type).arg("value", value); |
---|
[84] | 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 | */ |
---|
[87] | 95 | public <T> T getDef(Class<T> type) { |
---|
[84] | 96 | return tryCastAndReturn(def, type); |
---|
| 97 | } |
---|
[90] | 98 | |
---|
[105] | 99 | public <T> String serialize(T value) { |
---|
| 100 | return Strings.toStringNullProof(value); |
---|
| 101 | } |
---|
| 102 | |
---|
[84] | 103 | } |
---|