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