source: java/main/src/main/java/com/framsticks/params/types/NumberParam.java @ 193

Last change on this file since 193 was 193, checked in by Maciej Komosinski, 10 years ago

Set svn:eol-style native for all textual files

  • Property svn:eol-style set to native
File size: 1.3 KB
Line 
1package com.framsticks.params.types;
2
3import com.framsticks.params.CastFailure;
4import com.framsticks.params.ParamBuilder;
5import com.framsticks.params.PrimitiveParam;
6import com.framsticks.params.ReassignResult;
7import com.framsticks.util.lang.Casting;
8import com.framsticks.util.lang.Numbers;
9
10import javax.annotation.concurrent.Immutable;
11import static com.framsticks.params.SetStateFlags.*;
12
13/**
14 * @author Piotr Sniegowski
15 */
16@Immutable
17public 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}
Note: See TracBrowser for help on using the repository browser.