[84] | 1 | package com.framsticks.params.types; |
---|
| 2 | |
---|
| 3 | import com.framsticks.params.CastFailure; |
---|
[87] | 4 | import com.framsticks.params.ParamBuilder; |
---|
[84] | 5 | import com.framsticks.params.PrimitiveParam; |
---|
| 6 | import com.framsticks.params.ReassignResult; |
---|
| 7 | import com.framsticks.util.lang.Casting; |
---|
| 8 | import com.framsticks.util.lang.Numbers; |
---|
| 9 | |
---|
[87] | 10 | import javax.annotation.concurrent.Immutable; |
---|
[99] | 11 | import static com.framsticks.params.SetStateFlags.*; |
---|
[87] | 12 | |
---|
[84] | 13 | /** |
---|
| 14 | * @author Piotr Sniegowski |
---|
| 15 | */ |
---|
[87] | 16 | @Immutable |
---|
| 17 | public abstract class NumberParam<T extends Number & Comparable<T>> extends PrimitiveParam<T> { |
---|
[84] | 18 | |
---|
[87] | 19 | /** |
---|
| 20 | * @param builder |
---|
| 21 | */ |
---|
| 22 | public NumberParam(ParamBuilder builder) { |
---|
| 23 | super(builder); |
---|
| 24 | } |
---|
| 25 | |
---|
[84] | 26 | @Override |
---|
| 27 | public boolean isNumeric() { |
---|
| 28 | return true; |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | protected ReassignResult<T> reassignNumber(Object newValue, Object oldValue, Class<T> type) throws CastFailure { |
---|
| 32 | T v = null; |
---|
| 33 | if (newValue instanceof String) { |
---|
[87] | 34 | v = Numbers.parse((String) newValue, type); |
---|
[84] | 35 | } else { |
---|
| 36 | v = Casting.tryCast(type, newValue); |
---|
| 37 | } |
---|
| 38 | if (v == null) { |
---|
| 39 | throw new CastFailure(); |
---|
| 40 | } |
---|
| 41 | if (min != null && v.compareTo(getMin(type)) < 0) { |
---|
[99] | 42 | return new ReassignResult<T>(getMin(type), PSET_HITMIN); |
---|
[84] | 43 | } |
---|
| 44 | if (max != null && v.compareTo(getMax(type)) > 0) { |
---|
[99] | 45 | return new ReassignResult<T>(getMax(type), PSET_HITMAX); |
---|
[84] | 46 | } |
---|
| 47 | return ReassignResult.create(v); |
---|
| 48 | } |
---|
| 49 | } |
---|