View Javadoc
1   package org.opentrafficsim.base.parameters.constraint;
2   
3   /**
4    * In order to define default constraints within a Parameter Type, an <code>enum</code> is available. This interface supplies
5    * easy access to the values of this <code>enum</code>. To use this interface, simply implement it as below. The value
6    * <code>POSITIVE</code> is a property of this interface pointing to the <code>enum</code> field <code>POSITIVE</code>. As a
7    * result, the value that is set for <code>X</code> is checked to be above zero. Note that model and parameterType do not have
8    * to be defined in the same class.
9    * 
10   * <pre>
11   * public class myModel implements ConstraintInterface
12   * {
13   * 
14   *     public static final ParameterTypeLength X = new ParameterTypeLength(&quot;x&quot;, &quot;My x parameter.&quot;, POSITIVE);
15   * 
16   *     // ... model that uses parameter of type X.
17   * 
18   * }
19   * </pre>
20   * 
21   * Another way to access the <code>enum</code> fields is to import them, e.g.:
22   * 
23   * <pre>
24   * import static org.opentrafficsim.core.gtu.drivercharacteristics.AbstractParameterType.Constraint.POSITIVE;
25   * </pre>
26   * 
27   * <br>
28   * In order to implement <i>custom</i> checks, any Parameter Type must extend the <code>check</code> method of its super. An
29   * example is given below. The method should throw a <code>ParameterException</code> whenever a constraint is not met. The
30   * static <code>throwIf</code> method is used to do this. The first check is a simple check on the SI value being above 2. The
31   * second check compares the value with the value of another parameter in the <code>Parameters</code>. These checks can only be
32   * performed if the other parameter is present in the <code>Parameters</code>. <b>Checks with other parameter type values should
33   * always check whether <code>Parameters</code> contains the other parameter type</b>. i.e. <code>params.contains()</code>.<br>
34   * 
35   * <pre>
36   * public static final ParameterTypeLength X = new ParameterTypeLength(&quot;x&quot;, &quot;My x parameter.&quot;)
37   * {
38   *     public void check(Length value, Parameters params) throws ParameterException
39   *     {
40   *         Throw.when(value.si &lt;= 2, ParameterException.class, &quot;Value of X is not above 2.&quot;);
41   *         Throw.when(params.contains(Y) &amp;&amp; value.si &gt; params.getParameter(Y).si, ParameterException.class,
42   *                 &quot;Value of X is larger than value of Y.&quot;);
43   *     }
44   * };
45   * </pre>
46   * 
47   * Checks are invoked on default values (if given), in which case an empty <code>Parameters</code> is forwarded. At construction
48   * of a Parameter Type, no <code>Parameters</code> is available. Checks are also invoked when value are set into
49   * <code>Parameters</code>, in which case that <code>Parameters</code> forwards itself. Even still, if in the above case X is
50   * set before Y, the check is never performed. Therefore, Y should also compare itself to X. <b>Two parameters that are checked
51   * with each other, should both implement a check which is consistent with and mirrored to the the others' check.</b> <br>
52   * <br>
53   * The type of the first argument in the <code>check()</code> method depends on the super Parameter Type. For example:<br>
54   * <ul>
55   * <li><code>double</code> for <code>ParameterTypeDouble</code></li>
56   * <li><code>int</code> for <code>ParameterTypeInteger</code></li>
57   * <li><code>Speed</code> for <code>ParameterTypeSpeed</code></li>
58   * <li><code>Length</code> for <code>ParameterTypeLength</code></li>
59   * <li><code>T</code> for <code>ParameterType&lt;T&gt;</code></li>
60   * </ul>
61   * Note that <code>ParameterTypeBoolean</code> has no check method as checks on booleans are senseless.<br>
62   * <br>
63   * <p>
64   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
65   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
66   * </p>
67   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
68   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
69   */
70  @SuppressWarnings({"checkstyle:interfaceistype", "checkstyle:javadoctype", "checkstyle:javadocvariable", "javadoc"})
71  public interface ConstraintInterface
72  {
73  
74      // @formatter:off
75      Constraint<Number> POSITIVE     = NumericConstraint.POSITIVE;
76      Constraint<Number> NEGATIVE     = NumericConstraint.NEGATIVE;
77      Constraint<Number> POSITIVEZERO = NumericConstraint.POSITIVEZERO;
78      Constraint<Number> NEGATIVEZERO = NumericConstraint.NEGATIVEZERO;
79      Constraint<Number> NONZERO      = NumericConstraint.NONZERO;
80      Constraint<Number> ATLEASTONE   = NumericConstraint.ATLEASTONE;
81      Constraint<Number> UNITINTERVAL = DualBound.UNITINTERVAL;
82      // @formatter:on
83  }