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