View Javadoc
1   package org.opentrafficsim.base.modelproperties;
2   
3   import nl.tudelft.simulation.language.Throw;
4   
5   /**
6    * Long 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/node/13">OpenTrafficSim License</a>.
10   * <p>
11   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 9 apr. 2018 <br>
12   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
13   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
14   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
15   */
16  public class LongProperty extends AbstractProperty<Long>
17  {
18  
19      /** */
20      private static final long serialVersionUID = 20180409L;
21  
22      /** Value. */
23      private long value;
24  
25      /** Minimum value. */
26      private long minValue;
27  
28      /** Maximum value. */
29      private long maxValue;
30  
31      /** Format string. */
32      private final String formatString;
33  
34      /**
35       * Constructor.
36       * @param key String; key
37       * @param displayPriority int; display property
38       * @param shortName String; shortName
39       * @param description String; description
40       * @param initialValue Long; initial value
41       * @param minValue Long; minimum value
42       * @param maxValue Long; maximum value
43       * @param formatString String; format string
44       * @param readOnly boolean; read-only
45       */
46      public LongProperty(final String key, final int displayPriority, final String shortName, final String description,
47              final long initialValue, final long minValue, final long maxValue, final String formatString,
48              final boolean readOnly)
49      {
50          super(key, displayPriority, shortName, description);
51          this.value = initialValue;
52          this.minValue = minValue;
53          this.maxValue = maxValue;
54          this.formatString = formatString;
55          setReadOnly(readOnly);
56      }
57  
58      /** {@inheritDoc} */
59      @Override
60      public Long getValue()
61      {
62          return this.value;
63      }
64  
65      /** {@inheritDoc} */
66      @Override
67      public void setValue(final Long newValue) throws PropertyException
68      {
69          Throw.when(isReadOnly(), PropertyException.class, "Cannot set read-only property.");
70          Throw.when(newValue < this.minValue || newValue > this.maxValue, PropertyException.class,
71                  "Value %d is not in the range %d through %d.", newValue, this.minValue, this.maxValue);
72          this.value = newValue;
73      }
74  
75      /** {@inheritDoc} */
76      @Override
77      public AbstractProperty<Long> deepCopy()
78      {
79          return new LongProperty(getKey(), getDisplayPriority(), getShortName(), getDescription(), this.value, this.minValue,
80                  this.maxValue, this.formatString, isReadOnly());
81      }
82  
83      /** {@inheritDoc} */
84      @Override
85      public final String htmlStateDescription()
86      {
87          return getShortName() + ": " + String.format(this.formatString, getValue());
88      }
89  
90  }