1 package org.opentrafficsim.base.parameters.constraint;
2
3 import java.util.IllegalFormatException;
4
5 import org.djutils.exceptions.Throw;
6
7 /**
8 * List of default constraint for ParameterTypes.
9 * <p>
10 * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
11 * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
12 * <p>
13 * @version $Revision$, $LastChangedDate$, by $Author$, initial version 8 sep. 2017 <br>
14 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
15 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
16 * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
17 */
18 public enum NumericConstraint implements Constraint<Number>
19 {
20
21 /** Checks for >0. */
22 POSITIVE("Value of parameter '%s' must be above zero.")
23 {
24 /** {@inheritDoc} */
25 @Override
26 public boolean accept(final Number value)
27 {
28 return value.doubleValue() > 0.0;
29 }
30 },
31
32 /** Checks for <0. */
33 NEGATIVE("Value of parameter '%s' must be below zero.")
34 {
35 /** {@inheritDoc} */
36 @Override
37 public boolean accept(final Number value)
38 {
39 return value.doubleValue() < 0.0;
40 }
41 },
42
43 /** Checks for ≥0. */
44 POSITIVEZERO("Value of parameter '%s' may not be below zero.")
45 {
46 /** {@inheritDoc} */
47 @Override
48 public boolean accept(final Number value)
49 {
50 return value.doubleValue() >= 0.0;
51 }
52 },
53
54 /** Checks for ≤0. */
55 NEGATIVEZERO("Value of parameter '%s' may not be above zero.")
56 {
57 /** {@inheritDoc} */
58 @Override
59 public boolean accept(final Number value)
60 {
61 return value.doubleValue() <= 0.0;
62 }
63 },
64
65 /** Checks for ≠0. */
66 NONZERO("Value of parameter '%s' may not be zero.")
67 {
68 /** {@inheritDoc} */
69 @Override
70 public boolean accept(final Number value)
71 {
72 return value.doubleValue() != 0.0;
73 }
74 },
75
76 /** Checks for ≥1. */
77 ATLEASTONE("Value of parameter '%s' may not be below one.")
78 {
79 /** {@inheritDoc} */
80 @Override
81 public boolean accept(final Number value)
82 {
83 return value.doubleValue() >= 1.0;
84 }
85 };
86
87 /** Message for value failure, pointing to a parameter using '%s'. */
88 private final String failMessage;
89
90 /**
91 * Constructor with message for value failure, pointing to a parameter using '%s'.
92 * @param failMessage Message for value failure, pointing to a parameter using '%s'.
93 */
94 // @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED")
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 // return value can be ignored
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 * Returns a message for value failure, pointing to a parameter using '%s'.
115 * @return Message for value failure, pointing to a parameter using '%s'.
116 */
117 @Override
118 public String failMessage()
119 {
120 return this.failMessage;
121 }
122
123 }