View Javadoc
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-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
11   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
12   * </p>
13   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
14   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
15   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
16   */
17  public enum NumericConstraint implements Constraint<Number>
18  {
19  
20      /** Checks for &gt;0. */
21      POSITIVE("Value of parameter '%s' must be above zero.")
22      {
23          @Override
24          public boolean accept(final Number value)
25          {
26              return value.doubleValue() > 0.0;
27          }
28      },
29  
30      /** Checks for &lt;0. */
31      NEGATIVE("Value of parameter '%s' must be below zero.")
32      {
33          @Override
34          public boolean accept(final Number value)
35          {
36              return value.doubleValue() < 0.0;
37          }
38      },
39  
40      /** Checks for &ge;0. */
41      POSITIVEZERO("Value of parameter '%s' may not be below zero.")
42      {
43          @Override
44          public boolean accept(final Number value)
45          {
46              return value.doubleValue() >= 0.0;
47          }
48      },
49  
50      /** Checks for &le;0. */
51      NEGATIVEZERO("Value of parameter '%s' may not be above zero.")
52      {
53          @Override
54          public boolean accept(final Number value)
55          {
56              return value.doubleValue() <= 0.0;
57          }
58      },
59  
60      /** Checks for &ne;0. */
61      NONZERO("Value of parameter '%s' may not be zero.")
62      {
63          @Override
64          public boolean accept(final Number value)
65          {
66              return value.doubleValue() != 0.0;
67          }
68      },
69  
70      /** Checks for &ge;1. */
71      ATLEASTONE("Value of parameter '%s' may not be below one.")
72      {
73          @Override
74          public boolean accept(final Number value)
75          {
76              return value.doubleValue() >= 1.0;
77          }
78      };
79  
80      /** Message for value failure, pointing to a parameter using '%s'. */
81      private final String failMessage;
82  
83      /**
84       * Constructor with message for value failure, pointing to a parameter using '%s'.
85       * @param failMessage Message for value failure, pointing to a parameter using '%s'.
86       */
87      // @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED")
88      NumericConstraint(final String failMessage)
89      {
90          Throw.whenNull(failMessage, "Default parameter constraint '%s' has null as fail message as given to the constructor,"
91                  + " which is not allowed.", this);
92          try
93          {
94              // return value can be ignored
95              String.format(failMessage, "dummy");
96          }
97          catch (IllegalFormatException ife)
98          {
99              throw new RuntimeException("Default parameter constraint " + this.toString()
100                     + " has an illegal formatting of the fail message as given to the constructor."
101                     + " It should contain a single '%s'.", ife);
102         }
103         this.failMessage = failMessage;
104     }
105 
106     @Override
107     public String failMessage()
108     {
109         return this.failMessage;
110     }
111 
112 }