1 package org.opentrafficsim.base.modelproperties;
2
3 import java.io.Serializable;
4
5 /**
6 * Continuous property.
7 * <p>
8 * Copyright (c) 2013-2016 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 /**
33 * Construct a ContinousProperty.
34 * @param key String; the unique key of the new property
35 * @param shortName String; the short name of the new ContinuousProperty
36 * @param description String; description of the new ContinuousProperty (may use HTML mark up)
37 * @param initialValue Double; the initial value of the new ContinuousProperty
38 * @param minimumValue Double; the minimum value of the new ContinuousProperty
39 * @param maximumValue Double; the maximumValue of the new ContinuousProperty
40 * @param formatString String; format string to display the value
41 * @param readOnly boolean; if true this ContinuousProperty can not be altered
42 * @param displayPriority int; the displayPriority of the new ContinuousProperty
43 */
44 @SuppressWarnings("checkstyle:parameternumber")
45 public ContinuousProperty(final String key, final String shortName, final String description, final Double initialValue,
46 final Double minimumValue, final Double maximumValue, final String formatString, final boolean readOnly,
47 final int displayPriority)
48 {
49 super(key, displayPriority, shortName, description);
50 this.value = initialValue;
51 this.minimumValue = minimumValue;
52 this.maximumValue = maximumValue;
53 this.format = formatString;
54 setReadOnly(readOnly);
55 }
56
57 /** {@inheritDoc} */
58 @Override
59 public final Double getValue()
60 {
61 return this.value;
62 }
63
64 /**
65 * @return the minimum value
66 */
67 public final Double getMinimumValue()
68 {
69 return this.minimumValue;
70 }
71
72 /**
73 * @return the minimum value
74 */
75 public final Double getMaximumValue()
76 {
77 return this.maximumValue;
78 }
79
80 /** {@inheritDoc} */
81 @Override
82 public final void setValue(final Double newValue) throws PropertyException
83 {
84 if (isReadOnly())
85 {
86 throw new PropertyException("This property is read-only");
87 }
88 if (this.minimumValue > newValue || this.maximumValue < newValue)
89 {
90 throw new PropertyException("new value " + newValue + " is out of valid range (" + this.minimumValue + ".."
91 + this.maximumValue + ")");
92 }
93 this.value = newValue;
94 }
95
96 /**
97 * @return String; the format string to display the value
98 */
99 public final String getFormatString()
100 {
101 return this.format;
102 }
103
104 /** {@inheritDoc} */
105 @Override
106 public final String htmlStateDescription()
107 {
108 return getShortName() + ": " + String.format(getFormatString(), getValue());
109 }
110
111 /** {@inheritDoc} */
112 @Override
113 public final AbstractProperty<Double> deepCopy()
114 {
115 return new ContinuousProperty(getKey(), getShortName(), getDescription(), this.value, this.minimumValue,
116 this.maximumValue, this.format, isReadOnly(), getDisplayPriority());
117 }
118
119 }