View Javadoc
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    * List of default constraint for ParameterTypes.
10   * <p>
11   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
12   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
13   * </p>
14   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
15   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
16   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
17   */
18  public enum NumericConstraint implements Constraint<Number>
19  {
20  
21      /** Checks for &gt;0. */
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      /** Checks for &lt;0. */
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      /** Checks for &ge;0. */
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      /** Checks for &le;0. */
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      /** Checks for &ne;0. */
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      /** Checks for &ge;1. */
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      /** Message for value failure, pointing to a parameter using '%s'. */
82      private final String failMessage;
83  
84      /**
85       * Constructor with message for value failure, pointing to a parameter using '%s'.
86       * @param failMessage Message for value failure, pointing to a parameter using '%s'.
87       */
88      // @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED")
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              // return value can be ignored
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 }