[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; |
---|
[84] | 6 | |
---|
[87] | 7 | import javax.annotation.concurrent.Immutable; |
---|
| 8 | |
---|
[84] | 9 | /** |
---|
| 10 | * @author Piotr Sniegowski |
---|
| 11 | */ |
---|
[87] | 12 | @Immutable |
---|
| 13 | public abstract class PrimitiveParam<S> extends ValueParam { |
---|
[84] | 14 | |
---|
| 15 | /** The minimum allowed value of parameter. */ |
---|
[87] | 16 | protected final Object min; |
---|
[84] | 17 | |
---|
| 18 | /** The maximum allowed value of parameter. */ |
---|
[87] | 19 | protected final Object max; |
---|
[84] | 20 | |
---|
| 21 | /** The default value of parameter. */ |
---|
[87] | 22 | protected final Object def; |
---|
[84] | 23 | |
---|
[87] | 24 | /** |
---|
| 25 | * @param builder |
---|
| 26 | */ |
---|
| 27 | public PrimitiveParam(ParamBuilder builder) { |
---|
| 28 | super(builder); |
---|
[88] | 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); |
---|
[87] | 33 | } |
---|
| 34 | |
---|
[102] | 35 | public void save(Sink sink, Object value) { |
---|
[84] | 36 | sink.print(value); |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | protected <T> T tryCastAndReturn(Object value, Class<T> type) { |
---|
[87] | 40 | if (value == null) { |
---|
[84] | 41 | return null; |
---|
[87] | 42 | } |
---|
[84] | 43 | try { |
---|
[87] | 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 | } |
---|
[84] | 58 | } catch (ClassCastException e) { |
---|
[87] | 59 | throw new FramsticksException().msg("failed to cast").cause(e).arg("param", this).arg("actual", value.getClass()).arg("requested", type); |
---|
[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 | |
---|
[84] | 99 | } |
---|