View Javadoc
1   package org.opentrafficsim.simulationengine.properties;
2   
3   import java.io.Serializable;
4   
5   /**
6    * Integer 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 18 dec. 2014 <br>
13   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
14   */
15  public class IntegerProperty extends AbstractProperty<Integer> implements Serializable
16  {
17      /** */
18      private static final long serialVersionUID = 20150000L;
19  
20      /** The current value of the property. */
21      private Integer 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 Integer minimumValue;
28  
29      /** The maximum value of the property. */
30      private Integer maximumValue;
31  
32      /**
33       * Construct an IntegerProperty.
34       * @param key String; the unique key of the new property
35       * @param shortName String; the short name of the new IntegerProperty
36       * @param description String; description of the new IntegerProperty (may use HTML mark up)
37       * @param initialValue Integer; the initial value of the new IntegerProperty
38       * @param minimumValue Integer; the minimum value of the new IntegerProperty
39       * @param maximumValue Integer; the maximumValue of the new IntegerProperty
40       * @param formatString String; format string to display the value
41       * @param readOnly boolean; if true this IntegerProperty can not be altered
42       * @param displayPriority int; the display priority of the new IntegerProperty
43       */
44      @SuppressWarnings("checkstyle:parameternumber")
45      public IntegerProperty(final String key, final String shortName, final String description, final Integer initialValue,
46              final Integer minimumValue, final Integer 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 Integer getValue()
60      {
61          return this.value;
62      }
63  
64      /**
65       * Retrieve the minimum value of this IntegerProperty.
66       * @return Integer; the minimum value of this IntegerProperty
67       */
68      public final Integer getMinimumValue()
69      {
70          return this.minimumValue;
71      }
72  
73      /**
74       * Retrieve the maximum value of this IntegerProperty.
75       * @return Integer; the maximum value of this IntegerProperty
76       */
77      public final Integer getMaximumValue()
78      {
79          return this.maximumValue;
80      }
81  
82      /** {@inheritDoc} */
83      @Override
84      public final void setValue(final Integer newValue) throws PropertyException
85      {
86          if (isReadOnly())
87          {
88              throw new PropertyException("This property is read-only");
89          }
90          if (this.minimumValue > newValue || this.maximumValue < newValue)
91          {
92              throw new PropertyException("new value " + newValue + " is out of valid range (" + this.minimumValue + ".."
93                      + this.maximumValue + ")");
94          }
95          this.value = newValue;
96      }
97  
98      /**
99       * @return String; the format string to display the value
100      */
101     public final String getFormatString()
102     {
103         return this.format;
104     }
105 
106     /** {@inheritDoc} */
107     @Override
108     public final String htmlStateDescription()
109     {
110         return getShortName() + ": " + String.format(getFormatString(), getValue());
111     }
112 
113     /** {@inheritDoc} */
114     @Override
115     public final AbstractProperty<Integer> deepCopy()
116     {
117         return new IntegerProperty(getKey(), getShortName(), getDescription(), this.value, this.maximumValue,
118                 this.maximumValue, this.format, isReadOnly(), getDisplayPriority());
119     }
120 
121 }