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