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