summaryrefslogtreecommitdiff
path: root/dicore3/command/src/main/java/io/dico/dicore/command/annotation/Range.java
blob: 98aec2288c7837d007e4f5c3a4e21e09fe208849 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package io.dico.dicore.command.annotation;

import io.dico.dicore.command.CommandException;
import io.dico.dicore.command.Validate;
import io.dico.dicore.command.parameter.type.ParameterConfig;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface Range {
    Class<?> MEMORY_CLASS = Memory.class;
    ParameterConfig<Range, Memory> CONFIG = ParameterConfig.getMemoryClassFromField(Range.class);
    Memory DEFAULT = new Memory(-Double.MAX_VALUE, Double.MAX_VALUE, 0);

    double min() default -Double.MAX_VALUE;

    double max() default Double.MAX_VALUE;

    double defaultValue() default 0;

    class Memory {
        private final double min;
        private final double max;
        private final double defaultValue;

        public Memory(Range range) {
            this(range.min(), range.max(), range.defaultValue());
        }

        public Memory(double min, double max, double defaultValue) {
            this.min = min;
            this.max = max;
            this.defaultValue = defaultValue;
        }

        public double min() {
            return min;
        }

        public double max() {
            return max;
        }

        public double defaultValue() {
            return defaultValue;
        }

        public void validate(Number x, String failMessage) throws CommandException {
            Validate.isTrue(valid(x), failMessage);
        }

        public boolean valid(Number x) {
            double d = x.doubleValue();
            return min <= d && d <= max;
        }

        public boolean isDefault() {
            return this == DEFAULT || (min == DEFAULT.min && max == DEFAULT.max && defaultValue == DEFAULT.defaultValue);
        }

    }

}