1 | package com.framsticks.params.types; |
---|
2 | |
---|
3 | import com.framsticks.params.CastFailure; |
---|
4 | import com.framsticks.params.ParamBuilder; |
---|
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 | |
---|
10 | import javax.annotation.concurrent.Immutable; |
---|
11 | import static com.framsticks.params.SetStateFlags.*; |
---|
12 | |
---|
13 | /** |
---|
14 | * @author Piotr Sniegowski |
---|
15 | */ |
---|
16 | @Immutable |
---|
17 | public abstract class NumberParam<T extends Number & Comparable<T>> extends PrimitiveParam<T> { |
---|
18 | |
---|
19 | /** |
---|
20 | * @param builder |
---|
21 | */ |
---|
22 | public NumberParam(ParamBuilder builder) { |
---|
23 | super(builder); |
---|
24 | } |
---|
25 | |
---|
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) { |
---|
34 | v = Numbers.parse((String) newValue, type); |
---|
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) { |
---|
42 | return new ReassignResult<T>(getMin(type), PSET_HITMIN); |
---|
43 | } |
---|
44 | if (max != null && v.compareTo(getMax(type)) > 0) { |
---|
45 | return new ReassignResult<T>(getMax(type), PSET_HITMAX); |
---|
46 | } |
---|
47 | return ReassignResult.create(v); |
---|
48 | } |
---|
49 | } |
---|