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 speed gives both how fast and in what direction the object is moving.
11   * <p>
12   * Copyright (c) 2015-2019 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: 2019-03-02 19:06:46 +0100 (Sat, 02 Mar 2019) $, @version $Revision: 342 $, by $Author: averbraeck $,
16   * initial version May 15, 2014 <br>
17   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
18   */
19  public class SpeedUnit extends LinearUnit<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 DurationUnit durationUnit;
29  
30      /** The SI unit for speed is m/s. */
31      public static final SpeedUnit SI;
32  
33      /** m/s. */
34      public static final SpeedUnit METER_PER_SECOND;
35  
36      /** m/h. */
37      public static final SpeedUnit METER_PER_HOUR;
38  
39      /** km/s. */
40      public static final SpeedUnit KM_PER_SECOND;
41  
42      /** km/h. */
43      public static final SpeedUnit KM_PER_HOUR;
44  
45      /** in/s. */
46      public static final SpeedUnit INCH_PER_SECOND;
47  
48      /** in/min. */
49      public static final SpeedUnit INCH_PER_MINUTE;
50  
51      /** in/h. */
52      public static final SpeedUnit INCH_PER_HOUR;
53  
54      /** ft/s. */
55      public static final SpeedUnit FOOT_PER_SECOND;
56  
57      /** ft/min. */
58      public static final SpeedUnit FOOT_PER_MINUTE;
59  
60      /** ft/h. */
61      public static final SpeedUnit FOOT_PER_HOUR;
62  
63      /** mile/s. */
64      public static final SpeedUnit MILE_PER_SECOND;
65  
66      /** mile/min. */
67      public static final SpeedUnit MILE_PER_MINUTE;
68  
69      /** mile/h. */
70      public static final SpeedUnit MILE_PER_HOUR;
71  
72      /** knot. */
73      public static final SpeedUnit KNOT;
74  
75      static
76      {
77          SI = new SpeedUnit(LengthUnit.METER, DurationUnit.SECOND, "SpeedUnit.m/s", SI_DERIVED);
78          METER_PER_SECOND = SI;
79          METER_PER_HOUR = new SpeedUnit(LengthUnit.METER, DurationUnit.HOUR, "SpeedUnit.m/h", SI_DERIVED);
80          KM_PER_SECOND = new SpeedUnit(LengthUnit.KILOMETER, DurationUnit.SECOND, "SpeedUnit.km/s", SI_DERIVED);
81          KM_PER_HOUR = new SpeedUnit(LengthUnit.KILOMETER, DurationUnit.HOUR, "SpeedUnit.km/h", SI_DERIVED);
82          INCH_PER_SECOND = new SpeedUnit(LengthUnit.INCH, DurationUnit.SECOND, "SpeedUnit.in/s", IMPERIAL);
83          INCH_PER_MINUTE = new SpeedUnit(LengthUnit.INCH, DurationUnit.MINUTE, "SpeedUnit.in/min", IMPERIAL);
84          INCH_PER_HOUR = new SpeedUnit(LengthUnit.INCH, DurationUnit.HOUR, "SpeedUnit.in/h", IMPERIAL);
85          FOOT_PER_SECOND = new SpeedUnit(LengthUnit.FOOT, DurationUnit.SECOND, "SpeedUnit.fps", IMPERIAL);
86          FOOT_PER_MINUTE = new SpeedUnit(LengthUnit.FOOT, DurationUnit.MINUTE, "SpeedUnit.ft/min", IMPERIAL);
87          FOOT_PER_HOUR = new SpeedUnit(LengthUnit.FOOT, DurationUnit.HOUR, "SpeedUnit.ft/h", IMPERIAL);
88          MILE_PER_SECOND = new SpeedUnit(LengthUnit.MILE, DurationUnit.SECOND, "SpeedUnit.mi/s", IMPERIAL);
89          MILE_PER_MINUTE = new SpeedUnit(LengthUnit.MILE, DurationUnit.MINUTE, "SpeedUnit.mi/min", IMPERIAL);
90          MILE_PER_HOUR = new SpeedUnit(LengthUnit.MILE, DurationUnit.HOUR, "SpeedUnit.mph", IMPERIAL);
91          KNOT = new SpeedUnit(LengthUnit.NAUTICAL_MILE, DurationUnit.HOUR, "SpeedUnit.kt", IMPERIAL);
92      }
93  
94      /**
95       * Build a speed unit from a length unit and a time unit.
96       * @param lengthUnit LengthUnit; the unit of length for the speed unit, e.g., meter
97       * @param durationUnit DurationUnit; the unit of time for the speed unit, e.g., second
98       * @param abbreviationKey String; the key to the locale file for the abbreviation of the unit
99       * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
100      */
101     private SpeedUnit(final LengthUnit lengthUnit, final DurationUnit durationUnit, final String abbreviationKey,
102             final UnitSystem unitSystem)
103     {
104         super(abbreviationKey, unitSystem, METER_PER_SECOND, lengthUnit.getScaleFactor() / durationUnit.getScaleFactor());
105         this.lengthUnit = lengthUnit;
106         this.durationUnit = durationUnit;
107     }
108 
109     /**
110      * Build a user-defined speed unit from a length unit and a time unit.
111      * @param lengthUnit LengthUnit; the unit of length for the speed unit, e.g., meter
112      * @param durationUnit DurationUnit; the unit of time for the speed unit, e.g., second
113      * @param name String; the long name of the unit
114      * @param abbreviation String; the abbreviation of the unit
115      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
116      */
117     public SpeedUnit(final LengthUnit lengthUnit, final DurationUnit durationUnit, final String name, final String abbreviation,
118             final UnitSystem unitSystem)
119     {
120         super(name, abbreviation, unitSystem, METER_PER_SECOND, lengthUnit.getScaleFactor() / durationUnit.getScaleFactor());
121         this.lengthUnit = lengthUnit;
122         this.durationUnit = durationUnit;
123     }
124 
125     /**
126      * Build a SpeedUnit based on another SpeedUnit.
127      * @param abbreviationKey String; the key to the locale file for the abbreviation of the unit
128      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
129      * @param referenceUnit SpeedUnit; the unit to convert to
130      * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
131      *            unit
132      */
133     private SpeedUnit(final String abbreviationKey, final UnitSystem unitSystem, final SpeedUnit referenceUnit,
134             final double scaleFactorToReferenceUnit)
135     {
136         super(abbreviationKey, unitSystem, referenceUnit, scaleFactorToReferenceUnit);
137         this.lengthUnit = referenceUnit.getLengthUnit();
138         this.durationUnit = referenceUnit.getDurationUnit();
139     }
140 
141     /**
142      * Build a user-defined SpeedUnit with a conversion factor to another SpeedUnit.
143      * @param name String; the long name of the unit
144      * @param abbreviation String; the abbreviation of the unit
145      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
146      * @param referenceUnit SpeedUnit; the unit to convert to
147      * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
148      *            unit
149      */
150     public SpeedUnit(final String name, final String abbreviation, final UnitSystem unitSystem, final SpeedUnit referenceUnit,
151             final double scaleFactorToReferenceUnit)
152     {
153         super(name, abbreviation, unitSystem, referenceUnit, scaleFactorToReferenceUnit);
154         this.lengthUnit = referenceUnit.getLengthUnit();
155         this.durationUnit = referenceUnit.getDurationUnit();
156     }
157 
158     /**
159      * @return lengthUnit
160      */
161     public final LengthUnit getLengthUnit()
162     {
163         return this.lengthUnit;
164     }
165 
166     /**
167      * @return durationUnit
168      */
169     public final DurationUnit getDurationUnit()
170     {
171         return this.durationUnit;
172     }
173 
174     /** {@inheritDoc} */
175     @Override
176     public final SpeedUnit getStandardUnit()
177     {
178         return METER_PER_SECOND;
179     }
180 
181     /** {@inheritDoc} */
182     @Override
183     public final String getSICoefficientsString()
184     {
185         return "m/s";
186     }
187 
188 }