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
RevLine 
[84]1package com.framsticks.params.types;
2
3import com.framsticks.params.CastFailure;
[87]4import com.framsticks.params.ParamBuilder;
[84]5import com.framsticks.params.PrimitiveParam;
6import com.framsticks.params.ReassignResult;
7import com.framsticks.util.lang.Casting;
8import com.framsticks.util.lang.Numbers;
9
[87]10import javax.annotation.concurrent.Immutable;
[99]11import static com.framsticks.params.SetStateFlags.*;
[87]12
[84]13/**
14 * @author Piotr Sniegowski
15 */
[87]16@Immutable
17public 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}
Note: See TracBrowser for help on using the repository browser.