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://tudelft.nl/staff/p.knoppers-1">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          /** {@inheritDoc} */
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          /** {@inheritDoc} */
35          @Override
36          public boolean accept(final Number value)
37          {
38              return value.doubleValue() < 0.0;
39          }
40      },
41  
42      /** Checks for &ge;0. */
43      POSITIVEZERO("Value of parameter '%s' may not be below zero.")
44      {
45          /** {@inheritDoc} */
46          @Override
47          public boolean accept(final Number value)
48          {
49              return value.doubleValue() >= 0.0;
50          }
51      },
52  
53      /** Checks for &le;0. */
54      NEGATIVEZERO("Value of parameter '%s' may not be above zero.")
55      {
56          /** {@inheritDoc} */
57          @Override
58          public boolean accept(final Number value)
59          {
60              return value.doubleValue() <= 0.0;
61          }
62      },
63  
64      /** Checks for &ne;0. */
65      NONZERO("Value of parameter '%s' may not be zero.")
66      {
67          /** {@inheritDoc} */
68          @Override
69          public boolean accept(final Number value)
70          {
71              return value.doubleValue() != 0.0;
72          }
73      },
74  
75      /** Checks for &ge;1. */
76      ATLEASTONE("Value of parameter '%s' may not be below one.")
77      {
78          /** {@inheritDoc} */
79          @Override
80          public boolean accept(final Number value)
81          {
82              return value.doubleValue() >= 1.0;
83          }
84      };
85  
86      /** Message for value failure, pointing to a parameter using '%s'. */
87      private final String failMessage;
88  
89      /**
90       * Constructor with message for value failure, pointing to a parameter using '%s'.
91       * @param failMessage Message for value failure, pointing to a parameter using '%s'.
92       */
93      // @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED")
94      NumericConstraint(final String failMessage)
95      {
96          Throw.whenNull(failMessage, "Default parameter constraint '%s' has null as fail message as given to the constructor,"
97                  + " which is not allowed.", this);
98          try
99          {
100             // return value can be ignored
101             String.format(failMessage, "dummy");
102         }
103         catch (IllegalFormatException ife)
104         {
105             throw new RuntimeException("Default parameter constraint " + this.toString()
106                     + " has an illegal formatting of the fail message as given to the constructor."
107                     + " It should contain a single '%s'.", ife);
108         }
109         this.failMessage = failMessage;
110     }
111 
112     /**
113      * Returns a message for value failure, pointing to a parameter using '%s'.
114      * @return Message for value failure, pointing to a parameter using '%s'.
115      */
116     @Override
117     public String failMessage()
118     {
119         return this.failMessage;
120     }
121 
122 }