View Javadoc
1   package org.opentrafficsim.simulationengine.properties;
2   
3   /**
4    * Integer 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 18 dec. 2014 <br>
11   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
12   */
13  public class IntegerProperty extends AbstractProperty<Integer>
14  {
15      /** The current value of the property. */
16      private Integer 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 Integer minimumValue;
29  
30      /** The maximum value of the property. */
31      private Integer maximumValue;
32  
33      /** The property is read-only. */
34      private final Boolean readOnly;
35  
36      /**
37       * Construct an IntegerProperty.
38       * @param shortName String; the short name of the new IntegerProperty
39       * @param description String; description of the new IntegerProperty (may use HTML mark up)
40       * @param initialValue Integer; the initial value of the new IntegerProperty
41       * @param minimumValue Integer; the minimum value of the new IntegerProperty
42       * @param maximumValue Integer; the maximumValue of the new IntegerProperty
43       * @param formatString String; format string to display the value
44       * @param readOnly boolean; if true this IntegerProperty can not be altered
45       * @param displayPriority int; the display priority of the new IntegerProperty
46       */
47      @SuppressWarnings("checkstyle:parameternumber")
48      public IntegerProperty(final String shortName, final String description, final Integer initialValue,
49          final Integer minimumValue, final Integer 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 Integer getValue()
65      {
66          return this.value;
67      }
68  
69      /**
70       * Retrieve the minimum value of this IntegerProperty.
71       * @return Integer; the minimum value of this IntegerProperty
72       */
73      public final Integer getMinimumValue()
74      {
75          return this.minimumValue;
76      }
77  
78      /**
79       * Retrieve the maximum value of this IntegerProperty.
80       * @return Integer; the maximum value of this IntegerProperty
81       */
82      public final Integer getMaximumValue()
83      {
84          return this.maximumValue;
85      }
86  
87      /** {@inheritDoc} */
88      @Override
89      public final String getShortName()
90      {
91          return this.shortName;
92      }
93  
94      /** {@inheritDoc} */
95      @Override
96      public final String getDescription()
97      {
98          return this.description;
99      }
100 
101     /** {@inheritDoc} */
102     @Override
103     public final void setValue(final Integer newValue) throws PropertyException
104     {
105         if (this.readOnly)
106         {
107             throw new PropertyException("This property is read-only");
108         }
109         if (this.minimumValue > newValue || this.maximumValue < newValue)
110         {
111             throw new PropertyException("new value " + newValue + " is out of valid range (" + this.minimumValue + ".."
112                 + this.maximumValue + ")");
113         }
114         this.value = newValue;
115     }
116 
117     /** {@inheritDoc} */
118     @Override
119     public final boolean isReadOnly()
120     {
121         return this.readOnly;
122     }
123 
124     /**
125      * @return String; the format string to display the value
126      */
127     public final String getFormatString()
128     {
129         return this.format;
130     }
131 
132     /** {@inheritDoc} */
133     @Override
134     public final String htmlStateDescription()
135     {
136         return getShortName() + ": " + String.format(getFormatString(), getValue());
137     }
138 
139     /** {@inheritDoc} */
140     @Override
141     public final AbstractProperty<Integer> deepCopy()
142     {
143         return new IntegerProperty(this.shortName, this.description, this.value, this.maximumValue, this.maximumValue,
144             this.format, this.readOnly, getDisplayPriority());
145     }
146 
147 }