View Javadoc
1   package org.djunits.unit;
2   
3   import static org.djunits.unit.unitsystem.UnitSystem.IMPERIAL;
4   import static org.djunits.unit.unitsystem.UnitSystem.SI_DERIVED;
5   
6   import org.djunits.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 is
10   * moving, whereas velocity gives both how fast and in what direction the object is moving.
11   * <p>
12   * Copyright (c) 2015 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
13   * BSD-style license. See <a href="http://djunits.org/docs/license.html">DJUNITS License</a>.
14   * <p>
15   * $LastChangedDate: 2015-10-04 20:48:33 +0200 (Sun, 04 Oct 2015) $, @version $Revision: 87 $, by $Author: averbraeck $, initial
16   * version May 15, 2014 <br>
17   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
18   */
19  public class SpeedUnit extends Unit<SpeedUnit>
20  {
21      /** */
22      private static final long serialVersionUID = 20140607L;
23  
24      /** The unit of length for the speed unit, e.g., meter. */
25      private final LengthUnit lengthUnit;
26  
27      /** The unit of time for the speed unit, e.g., second. */
28      private final TimeUnit timeUnit;
29  
30      /** The SI unit for velocity is m/s. */
31      public static final SpeedUnit SI;
32  
33      /** m/s. */
34      public static final SpeedUnit METER_PER_SECOND;
35  
36      /** km/h. */
37      public static final SpeedUnit KM_PER_HOUR;
38  
39      /** mile/h. */
40      public static final SpeedUnit MILE_PER_HOUR;
41  
42      /** ft/s. */
43      public static final SpeedUnit FOOT_PER_SECOND;
44  
45      /** knot. */
46      public static final SpeedUnit KNOT;
47  
48      static
49      {
50          SI =
51              new SpeedUnit(LengthUnit.METER, TimeUnit.SECOND, "SpeedUnit.meter_per_second", "SpeedUnit.m/s", SI_DERIVED,
52                  true);
53          METER_PER_SECOND = SI;
54          KM_PER_HOUR =
55              new SpeedUnit(LengthUnit.KILOMETER, TimeUnit.HOUR, "SpeedUnit.kilometer_per_hour", "SpeedUnit.km/h",
56                  SI_DERIVED, true);
57          MILE_PER_HOUR =
58              new SpeedUnit(LengthUnit.MILE, TimeUnit.HOUR, "SpeedUnit.mile_per_hour", "SpeedUnit.mph", IMPERIAL, true);
59          FOOT_PER_SECOND =
60              new SpeedUnit(LengthUnit.FOOT, TimeUnit.SECOND, "SpeedUnit.foot_per_second", "SpeedUnit.fps", IMPERIAL,
61                  true);
62          KNOT = new SpeedUnit(LengthUnit.NAUTICAL_MILE, TimeUnit.HOUR, "SpeedUnit.knot", "SpeedUnit.kt", IMPERIAL, true);
63      }
64  
65      /**
66       * Build a speed unit from a length unit and a time unit.
67       * @param lengthUnit the unit of length for the speed unit, e.g., meter
68       * @param timeUnit the unit of time for the speed unit, e.g., second
69       * @param nameOrNameKey if standardUnit: the key to the locale file for the long name of the unit, otherwise the name itself
70       * @param abbreviationOrAbbreviationKey if standardUnit: the key to the locale file for the abbreviation of the unit,
71       *            otherwise the abbreviation itself
72       * @param unitSystem the unit system, e.g. SI or Imperial
73       * @param standardUnit indicates whether it is a standard unit with a definition in the locale, or a user-defined unit
74       */
75      private SpeedUnit(final LengthUnit lengthUnit, final TimeUnit timeUnit, final String nameOrNameKey,
76          final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem, final boolean standardUnit)
77      {
78          super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, METER_PER_SECOND, lengthUnit
79              .getConversionFactorToStandardUnit()
80              / timeUnit.getConversionFactorToStandardUnit(), standardUnit);
81          this.lengthUnit = lengthUnit;
82          this.timeUnit = timeUnit;
83      }
84  
85      /**
86       * Build a user-defined speed unit from a length unit and a time unit.
87       * @param lengthUnit the unit of length for the speed unit, e.g., meter
88       * @param timeUnit the unit of time for the speed unit, e.g., second
89       * @param name the long name of the unit
90       * @param abbreviation the abbreviation of the unit
91       * @param unitSystem the unit system, e.g. SI or Imperial
92       */
93      public SpeedUnit(final LengthUnit lengthUnit, final TimeUnit timeUnit, final String name,
94          final String abbreviation, final UnitSystem unitSystem)
95      {
96          this(lengthUnit, timeUnit, name, abbreviation, unitSystem, false);
97      }
98  
99      /**
100      * Build a SpeedUnit based on another SpeedUnit.
101      * @param nameOrNameKey if standardUnit: the key to the locale file for the long name of the unit, otherwise the name itself
102      * @param abbreviationOrAbbreviationKey if standardUnit: the key to the locale file for the abbreviation of the unit,
103      *            otherwise the abbreviation itself
104      * @param unitSystem the unit system, e.g. SI or Imperial
105      * @param referenceUnit the unit to convert to
106      * @param conversionFactorToReferenceUnit multiply a value in this unit by the factor to convert to the given reference unit
107      * @param standardUnit indicates whether it is a standard unit with a definition in the locale, or a user-defined unit
108      */
109     private SpeedUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey,
110         final UnitSystem unitSystem, final SpeedUnit referenceUnit, final double conversionFactorToReferenceUnit,
111         final boolean standardUnit)
112     {
113         super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, referenceUnit, conversionFactorToReferenceUnit,
114             standardUnit);
115         this.lengthUnit = referenceUnit.getLengthUnit();
116         this.timeUnit = referenceUnit.getTimeUnit();
117     }
118 
119     /**
120      * Build a user-defined SpeedUnit with a conversion factor to another SpeedUnit.
121      * @param name the long name of the unit
122      * @param abbreviation the abbreviation of the unit
123      * @param unitSystem the unit system, e.g. SI or Imperial
124      * @param referenceUnit the unit to convert to
125      * @param conversionFactorToReferenceUnit multiply a value in this unit by the factor to convert to the given reference unit
126      */
127     public SpeedUnit(final String name, final String abbreviation, final UnitSystem unitSystem,
128         final SpeedUnit referenceUnit, final double conversionFactorToReferenceUnit)
129     {
130         this(name, abbreviation, unitSystem, referenceUnit, conversionFactorToReferenceUnit, false);
131     }
132 
133     /**
134      * @return lengthUnit
135      */
136     public final LengthUnit getLengthUnit()
137     {
138         return this.lengthUnit;
139     }
140 
141     /**
142      * @return timeUnit
143      */
144     public final TimeUnit getTimeUnit()
145     {
146         return this.timeUnit;
147     }
148 
149     /** {@inheritDoc} */
150     @Override
151     public final SpeedUnit getStandardUnit()
152     {
153         return METER_PER_SECOND;
154     }
155 
156     /** {@inheritDoc} */
157     @Override
158     public final String getSICoefficientsString()
159     {
160         return "m/s";
161     }
162 
163 }