1 package org.opentrafficsim.base.parameters.constraint;
2
3 import java.util.IllegalFormatException;
4
5 import org.djutils.exceptions.Throw;
6
7
8
9
10
11
12
13
14
15
16
17 public enum NumericConstraint implements Constraint<Number>
18 {
19
20
21 POSITIVE("Value of parameter '%s' must be above zero.")
22 {
23
24 @Override
25 public boolean accept(final Number value)
26 {
27 return value.doubleValue() > 0.0;
28 }
29 },
30
31
32 NEGATIVE("Value of parameter '%s' must be below zero.")
33 {
34
35 @Override
36 public boolean accept(final Number value)
37 {
38 return value.doubleValue() < 0.0;
39 }
40 },
41
42
43 POSITIVEZERO("Value of parameter '%s' may not be below zero.")
44 {
45
46 @Override
47 public boolean accept(final Number value)
48 {
49 return value.doubleValue() >= 0.0;
50 }
51 },
52
53
54 NEGATIVEZERO("Value of parameter '%s' may not be above zero.")
55 {
56
57 @Override
58 public boolean accept(final Number value)
59 {
60 return value.doubleValue() <= 0.0;
61 }
62 },
63
64
65 NONZERO("Value of parameter '%s' may not be zero.")
66 {
67
68 @Override
69 public boolean accept(final Number value)
70 {
71 return value.doubleValue() != 0.0;
72 }
73 },
74
75
76 ATLEASTONE("Value of parameter '%s' may not be below one.")
77 {
78
79 @Override
80 public boolean accept(final Number value)
81 {
82 return value.doubleValue() >= 1.0;
83 }
84 };
85
86
87 private final String failMessage;
88
89
90
91
92
93
94 NumericConstraint(final String failMessage)
95 {
96 Throw.whenNull(failMessage, "Default parameter constraint '%s' has null as fail message as given to the constructor,"
97 + " which is not allowed.", this);
98 try
99 {
100
101 String.format(failMessage, "dummy");
102 }
103 catch (IllegalFormatException ife)
104 {
105 throw new RuntimeException("Default parameter constraint " + this.toString()
106 + " has an illegal formatting of the fail message as given to the constructor."
107 + " It should contain a single '%s'.", ife);
108 }
109 this.failMessage = failMessage;
110 }
111
112
113
114
115
116 @Override
117 public String failMessage()
118 {
119 return this.failMessage;
120 }
121
122 }