View Javadoc
1   package org.opentrafficsim.core.unit;
2   
3   import static org.opentrafficsim.core.unit.unitsystem.UnitSystem.IMPERIAL;
4   import static org.opentrafficsim.core.unit.unitsystem.UnitSystem.SI_DERIVED;
5   
6   import org.opentrafficsim.core.unit.unitsystem.UnitSystem;
7   
8   /**
9    * According to <a href="http://en.wikipedia.org/wiki/Velocity">Wikipedia</a>: Speed describes only how fast an object
10   * is moving, whereas velocity gives both how fast and in what direction the object is moving.
11   * <p>
12   * Copyright (c) 2014 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
13   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
14   * <p>
15   * @version May 15, 2014 <br>
16   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
17   */
18  public class SpeedUnit extends Unit<SpeedUnit>
19  {
20      /** */
21      private static final long serialVersionUID = 20140607L;
22  
23      /** The unit of length for the speed unit, e.g., meter. */
24      private final LengthUnit lengthUnit;
25  
26      /** The unit of time for the speed unit, e.g., second. */
27      private final TimeUnit timeUnit;
28  
29      /** The SI unit for velocity is m/s. */
30      public static final SpeedUnit SI;
31  
32      /** m/s. */
33      public static final SpeedUnit METER_PER_SECOND;
34  
35      /** km/h. */
36      public static final SpeedUnit KM_PER_HOUR;
37  
38      /** mile/h. */
39      public static final SpeedUnit MILE_PER_HOUR;
40  
41      /** ft/s. */
42      public static final SpeedUnit FOOT_PER_SECOND;
43  
44      /** knot. */
45      public static final SpeedUnit KNOT;
46  
47      static
48      {
49          SI =
50                  new SpeedUnit(LengthUnit.METER, TimeUnit.SECOND, "SpeedUnit.meter_per_second", "SpeedUnit.m/s",
51                          SI_DERIVED);
52          METER_PER_SECOND = SI;
53          KM_PER_HOUR =
54                  new SpeedUnit(LengthUnit.KILOMETER, TimeUnit.HOUR, "SpeedUnit.kilometer_per_hour", "SpeedUnit.km/h",
55                          SI_DERIVED);
56          MILE_PER_HOUR =
57                  new SpeedUnit(LengthUnit.MILE, TimeUnit.HOUR, "SpeedUnit.mile_per_hour", "SpeedUnit.mph", IMPERIAL);
58          FOOT_PER_SECOND =
59                  new SpeedUnit(LengthUnit.FOOT, TimeUnit.SECOND, "SpeedUnit.foot_per_second", "SpeedUnit.fps", IMPERIAL);
60          KNOT = new SpeedUnit(LengthUnit.NAUTICAL_MILE, TimeUnit.HOUR, "SpeedUnit.knot", "SpeedUnit.kt", IMPERIAL);
61      }
62  
63      /**
64       * Build a speed unit from a length unit and a time unit.
65       * @param lengthUnit the unit of length for the speed unit, e.g., meter
66       * @param timeUnit the unit of time for the speed unit, e.g., second
67       * @param nameKey the key to the locale file for the long name of the unit
68       * @param abbreviationKey the key to the locale file for the abbreviation of the unit
69       * @param unitSystem the unit system, e.g. SI or Imperial
70       */
71      public SpeedUnit(final LengthUnit lengthUnit, final TimeUnit timeUnit, final String nameKey,
72              final String abbreviationKey, final UnitSystem unitSystem)
73      {
74          super(nameKey, abbreviationKey, unitSystem, METER_PER_SECOND, lengthUnit.getConversionFactorToStandardUnit()
75                  / timeUnit.getConversionFactorToStandardUnit(), true);
76          this.lengthUnit = lengthUnit;
77          this.timeUnit = timeUnit;
78      }
79  
80      /**
81       * Build a speed unit based on another speed unit.
82       * @param nameKey the key to the locale file for the long name of the unit
83       * @param abbreviationKey the key to the locale file for the abbreviation of the unit
84       * @param unitSystem the unit system, e.g. SI or Imperial
85       * @param referenceUnit the unit to convert to
86       * @param conversionFactorToReferenceUnit multiply a value in this unit by the factor to convert to the given
87       *            reference unit
88       */
89      public SpeedUnit(final String nameKey, final String abbreviationKey, final UnitSystem unitSystem,
90              final SpeedUnit referenceUnit, final double conversionFactorToReferenceUnit)
91      {
92          super(nameKey, abbreviationKey, unitSystem, referenceUnit, conversionFactorToReferenceUnit, true);
93          this.lengthUnit = referenceUnit.getLengthUnit();
94          this.timeUnit = referenceUnit.getTimeUnit();
95      }
96  
97      /**
98       * @return lengthUnit
99       */
100     public final LengthUnit getLengthUnit()
101     {
102         return this.lengthUnit;
103     }
104 
105     /**
106      * @return timeUnit
107      */
108     public final TimeUnit getTimeUnit()
109     {
110         return this.timeUnit;
111     }
112 
113     /** {@inheritDoc} */
114     @Override
115     public final SpeedUnit getStandardUnit()
116     {
117         return METER_PER_SECOND;
118     }
119 
120     /** {@inheritDoc} */
121     @Override
122     public final String getSICoefficientsString()
123     {
124         return "m/s";
125     }
126 
127 }