View Javadoc
1   package org.opentrafficsim.base.modelproperties;
2   
3   import java.io.Serializable;
4   
5   /**
6    * Continuous property.
7    * <p>
8    * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
9    * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
10   * <p>
11   * $LastChangedDate: 2016-05-28 11:33:31 +0200 (Sat, 28 May 2016) $, @version $Revision: 2051 $, by $Author: averbraeck $,
12   * initial version 30 dec. 2014 <br>
13   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
14   */
15  public class ContinuousProperty extends AbstractProperty<Double> implements Serializable
16  {
17      /** */
18      private static final long serialVersionUID = 20150000L;
19  
20      /** The current value. */
21      private Double value;
22  
23      /** Format string to display the value of the property. */
24      private String format;
25  
26      /** The minimum value of the property. */
27      private Double minimumValue;
28  
29      /** The maximum value of the property. */
30      private Double maximumValue;
31  
32      /** The minimum value is valid. */
33      private boolean includesLowerBound;
34  
35      /** The maximum value is valid. */
36      private boolean includesUpperBound;
37  
38      /**
39       * Construct a ContinousProperty that is inclusive of lowerBound and upperBound.
40       * @param key String; the unique key of the new property
41       * @param shortName String; the short name of the new ContinuousProperty
42       * @param description String; description of the new ContinuousProperty (may use HTML mark up)
43       * @param initialValue Double; the initial value of the new ContinuousProperty (may be out of range)
44       * @param minimumValue Double; the minimum value of the new ContinuousProperty
45       * @param maximumValue Double; the maximumValue of the new ContinuousProperty
46       * @param formatString String; format string to display the value
47       * @param readOnly boolean; if true this ContinuousProperty can not be altered
48       * @param displayPriority int; the displayPriority of the new ContinuousProperty
49       */
50      @SuppressWarnings("checkstyle:parameternumber")
51      public ContinuousProperty(final String key, final String shortName, final String description, final Double initialValue,
52              final Double minimumValue, final Double maximumValue, final String formatString, final boolean readOnly,
53              final int displayPriority)
54      {
55          this(key, shortName, description, initialValue, minimumValue, maximumValue, true, true, formatString, readOnly,
56                  displayPriority);
57      }
58  
59      /**
60       * Construct a ContinousProperty.
61       * @param key String; the unique key of the new property
62       * @param shortName String; the short name of the new ContinuousProperty
63       * @param description String; description of the new ContinuousProperty (may use HTML mark up)
64       * @param initialValue Double; the initial value of the new ContinuousProperty (may be out of range)
65       * @param minimumValue Double; the minimum value of the new ContinuousProperty
66       * @param maximumValue Double; the maximumValue of the new ContinuousProperty
67       * @param includesLowerBound boolean; if true, the minimumValue is valid; if false, the minimumValue is not valid
68       * @param includesUpperBound boolean; if true, the maximumValue is valid; if false, the maximumValue is not valid
69       * @param formatString String; format string to display the value
70       * @param readOnly boolean; if true this ContinuousProperty can not be altered
71       * @param displayPriority int; the displayPriority of the new ContinuousProperty
72       */
73      @SuppressWarnings("checkstyle:parameternumber")
74      public ContinuousProperty(final String key, final String shortName, final String description, final Double initialValue,
75              final Double minimumValue, final Double maximumValue, final boolean includesLowerBound,
76              final boolean includesUpperBound, final String formatString, final boolean readOnly, final int displayPriority)
77      {
78          super(key, displayPriority, shortName, description);
79          this.value = initialValue;
80          this.minimumValue = minimumValue;
81          this.maximumValue = maximumValue;
82          this.includesLowerBound = includesLowerBound;
83          this.includesUpperBound = includesUpperBound;
84          this.format = formatString;
85          setReadOnly(readOnly);
86      }
87  
88      /** {@inheritDoc} */
89      @Override
90      public final Double getValue()
91      {
92          return this.value;
93      }
94  
95      /**
96       * @return the minimum value
97       */
98      public final Double getMinimumValue()
99      {
100         return this.minimumValue;
101     }
102 
103     /**
104      * @return the minimum value
105      */
106     public final Double getMaximumValue()
107     {
108         return this.maximumValue;
109     }
110 
111     /** {@inheritDoc} */
112     @Override
113     public final void setValue(final Double newValue) throws PropertyException
114     {
115         if (isReadOnly())
116         {
117             throw new PropertyException("This property is read-only");
118         }
119         // @formatter:off
120         if (this.minimumValue > newValue || this.maximumValue < newValue
121                 || this.minimumValue == newValue && (!this.includesLowerBound) && this.minimumValue != this.maximumValue 
122                 || this.maximumValue == newValue && (!this.includesUpperBound) && this.minimumValue != this.maximumValue 
123                 || this.minimumValue == newValue && this.maximumValue == newValue && (!this.includesLowerBound) 
124                 && (!this.includesUpperBound))
125         // @formatter:on
126         {
127             throw new PropertyException(
128                     "new value " + newValue + " is out of valid range (" + this.minimumValue + ".." + this.maximumValue + ")");
129         }
130         this.value = newValue;
131     }
132 
133     /**
134      * @return String; the format string to display the value
135      */
136     public final String getFormatString()
137     {
138         return this.format;
139     }
140 
141     /** {@inheritDoc} */
142     @Override
143     public final String htmlStateDescription()
144     {
145         return getShortName() + ": " + String.format(getFormatString(), getValue());
146     }
147 
148     /** {@inheritDoc} */
149     @Override
150     public final AbstractProperty<Double> deepCopy()
151     {
152         return new ContinuousProperty(getKey(), getShortName(), getDescription(), this.value, this.minimumValue,
153                 this.maximumValue, this.format, isReadOnly(), getDisplayPriority());
154     }
155 
156     /**
157      * Is the range inclusive of the minimumValue?
158      * @return boolean; true if the range is inclusive of the minimumValue
159      */
160     public final boolean includesLowerBound()
161     {
162         return this.includesLowerBound;
163     }
164 
165     /**
166      * Is the range inclusive of the maximumValue?
167      * @return boolean; true if the range is inclusive of the maximumValue
168      */
169     public final boolean includesUpperBound()
170     {
171         return this.includesUpperBound;
172     }
173 
174 }