View Javadoc
1   package org.opentrafficsim.core.gtu.behavioralcharacteristics;
2   
3   import java.io.Serializable;
4   
5   import org.djunits.unit.DimensionlessUnit;
6   import org.djunits.value.formatter.EngineeringFormatter;
7   import org.djunits.value.vdouble.scalar.Dimensionless;
8   
9   import nl.tudelft.simulation.language.Throw;
10  
11  /**
12   * Wrapper class for double parameters.
13   * <p>
14   * Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
15   * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
16   * <p>
17   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Apr 13, 2016 <br>
18   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
19   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
20   */
21  public class ParameterTypeDouble extends AbstractParameterType<Dimensionless> implements Serializable
22  {
23  
24      /** */
25      private static final long serialVersionUID = 120160400;
26  
27      /**
28       * Constructor without default value and check.
29       * @param id Short name of parameter.
30       * @param description Parameter description or full name.
31       */
32      public ParameterTypeDouble(final String id, final String description)
33      {
34          this(id, description, Double.NaN, null, false);
35      }
36  
37      /**
38       * Constructor with default value, without check.
39       * @param id Short name of parameter.
40       * @param description Parameter description or full name..
41       * @param defaultValue Default value.
42       */
43      public ParameterTypeDouble(final String id, final String description, final double defaultValue)
44      {
45          this(id, description, defaultValue, null, true);
46      }
47  
48      /**
49       * Constructor without default value, with check.
50       * @param id Short name of parameter.
51       * @param description Parameter description or full name.
52       * @param check Check for parameter values.
53       */
54      public ParameterTypeDouble(final String id, final String description, final Check check)
55      {
56          this(id, description, Double.NaN, check, false);
57      }
58  
59      /**
60       * Constructor with default value and check.
61       * @param id Short name of parameter.
62       * @param description Parameter description or full name.
63       * @param defaultValue Default value.
64       * @param check Check for parameter values.
65       */
66      public ParameterTypeDouble(final String id, final String description, final double defaultValue, final Check check)
67      {
68          super(id, description, Dimensionless.class, new Dimensionless(defaultValue, DimensionlessUnit.SI), check, true);
69      }
70  
71      /**
72       * Private constructor with default value and check, which may check the default value.
73       * @param id Short name of parameter.
74       * @param description Parameter description or full name.
75       * @param defaultValue Default value.
76       * @param check Check for parameter values.
77       * @param hasDefaultValue Whether to check the default value for null.
78       */
79      private ParameterTypeDouble(final String id, final String description, final double defaultValue, final Check check,
80              final boolean hasDefaultValue)
81      {
82          super(id, description, Dimensionless.class,
83                  hasDefaultValue ? new Dimensionless(defaultValue, DimensionlessUnit.SI) : null, check, hasDefaultValue);
84          try
85          {
86              // Forward empty set of parameters. At creation time of parameter types, values cannot be checked with values of
87              // other parameter types.
88              check(defaultValue, new BehavioralCharacteristics());
89          }
90          catch (ParameterException exception)
91          {
92              throw new RuntimeException("Default value does not comply with constraints.", exception);
93          }
94      }
95  
96      /** {@inheritDoc} */
97      public final Double getDefaultValue() throws ParameterException
98      {
99          Throw.when(null == this.defaultValue, ParameterException.class, "No default value was set for '%s'.", getId());
100         return super.defaultValue.si;
101     }
102 
103     /** {@inheritDoc} */
104     public final String printValue(final BehavioralCharacteristics behavioralCharacteristics) throws ParameterException
105     {
106         return EngineeringFormatter.format(behavioralCharacteristics.getParameter(this));
107     }
108 
109     /**
110      * Method to overwrite for checks with constraints.
111      * @param value Value to check with constraints.
112      * @param bc Set of behavioral characteristics.
113      * @throws ParameterException If the value does not comply with constraints.
114      */
115     public void check(final double value, final BehavioralCharacteristics bc) throws ParameterException
116     {
117         //
118     }
119 
120     /** {@inheritDoc} */
121     @SuppressWarnings("checkstyle:designforextension")
122     @Override
123     public String toString()
124     {
125         return "ParameterTypeDouble [id=" + getId() + ", description=" + getDescription() + "]";
126     }
127 
128 }