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-01-18 00:35:01 +0100 (Fri, 18 Jan 2019) $, @version $Revision: 324 $, 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.meter_per_second", "SpeedUnit.m/s", SI_DERIVED,
78                  true);
79          METER_PER_SECOND = SI;
80          METER_PER_HOUR = new SpeedUnit(LengthUnit.METER, DurationUnit.HOUR, "SpeedUnit.meter_per_hour", "SpeedUnit.m/h",
81                  SI_DERIVED, true);
82          KM_PER_SECOND = new SpeedUnit(LengthUnit.KILOMETER, DurationUnit.SECOND, "SpeedUnit.kilometer_per_second",
83                  "SpeedUnit.km/s", SI_DERIVED, true);
84          KM_PER_HOUR = new SpeedUnit(LengthUnit.KILOMETER, DurationUnit.HOUR, "SpeedUnit.kilometer_per_hour", "SpeedUnit.km/h",
85                  SI_DERIVED, true);
86          INCH_PER_SECOND = new SpeedUnit(LengthUnit.INCH, DurationUnit.SECOND, "SpeedUnit.inch_per_second", "SpeedUnit.in/s",
87                  IMPERIAL, true);
88          INCH_PER_MINUTE = new SpeedUnit(LengthUnit.INCH, DurationUnit.MINUTE, "SpeedUnit.inch_per_minute", "SpeedUnit.in/min",
89                  IMPERIAL, true);
90          INCH_PER_HOUR =
91                  new SpeedUnit(LengthUnit.INCH, DurationUnit.HOUR, "SpeedUnit.inch_per_hour", "SpeedUnit.in/h", IMPERIAL, true);
92          FOOT_PER_SECOND = new SpeedUnit(LengthUnit.FOOT, DurationUnit.SECOND, "SpeedUnit.foot_per_second", "SpeedUnit.fps",
93                  IMPERIAL, true);
94          FOOT_PER_MINUTE = new SpeedUnit(LengthUnit.FOOT, DurationUnit.MINUTE, "SpeedUnit.foot_per_minute", "SpeedUnit.ft/min",
95                  IMPERIAL, true);
96          FOOT_PER_HOUR =
97                  new SpeedUnit(LengthUnit.FOOT, DurationUnit.HOUR, "SpeedUnit.foot_per_hour", "SpeedUnit.ft/h", IMPERIAL, true);
98          MILE_PER_SECOND = new SpeedUnit(LengthUnit.MILE, DurationUnit.SECOND, "SpeedUnit.mile_per_second", "SpeedUnit.mi/s",
99                  IMPERIAL, true);
100         MILE_PER_MINUTE = new SpeedUnit(LengthUnit.MILE, DurationUnit.MINUTE, "SpeedUnit.mile_per_minute", "SpeedUnit.mi/min",
101                 IMPERIAL, true);
102         MILE_PER_HOUR =
103                 new SpeedUnit(LengthUnit.MILE, DurationUnit.HOUR, "SpeedUnit.mile_per_hour", "SpeedUnit.mph", IMPERIAL, true);
104         KNOT = new SpeedUnit(LengthUnit.NAUTICAL_MILE, DurationUnit.HOUR, "SpeedUnit.knot", "SpeedUnit.kt", IMPERIAL, true);
105     }
106 
107     /**
108      * Build a speed unit from a length unit and a time unit.
109      * @param lengthUnit LengthUnit; the unit of length for the speed unit, e.g., meter
110      * @param durationUnit DurationUnit; the unit of time for the speed unit, e.g., second
111      * @param nameOrNameKey String; if standardUnit: the key to the locale file for the long name of the unit, otherwise the
112      *            name itself
113      * @param abbreviationOrAbbreviationKey String; if standardUnit: the key to the locale file for the abbreviation of the
114      *            unit, otherwise the abbreviation itself
115      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
116      * @param standardUnit boolean; indicates whether it is a standard unit with a definition in the locale, or a user-defined
117      *            unit
118      */
119     private SpeedUnit(final LengthUnit lengthUnit, final DurationUnit durationUnit, final String nameOrNameKey,
120             final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem, final boolean standardUnit)
121     {
122         super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, METER_PER_SECOND,
123                 lengthUnit.getScaleFactor() / durationUnit.getScaleFactor(), standardUnit);
124         this.lengthUnit = lengthUnit;
125         this.durationUnit = durationUnit;
126     }
127 
128     /**
129      * Build a user-defined speed unit from a length unit and a time unit.
130      * @param lengthUnit LengthUnit; the unit of length for the speed unit, e.g., meter
131      * @param durationUnit DurationUnit; the unit of time for the speed unit, e.g., second
132      * @param name String; the long name of the unit
133      * @param abbreviation String; the abbreviation of the unit
134      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
135      */
136     public SpeedUnit(final LengthUnit lengthUnit, final DurationUnit durationUnit, final String name, final String abbreviation,
137             final UnitSystem unitSystem)
138     {
139         this(lengthUnit, durationUnit, name, abbreviation, unitSystem, false);
140     }
141 
142     /**
143      * Build a SpeedUnit based on another SpeedUnit.
144      * @param nameOrNameKey String; if standardUnit: the key to the locale file for the long name of the unit, otherwise the
145      *            name itself
146      * @param abbreviationOrAbbreviationKey String; if standardUnit: the key to the locale file for the abbreviation of the
147      *            unit, otherwise the abbreviation itself
148      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
149      * @param referenceUnit SpeedUnit; the unit to convert to
150      * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
151      *            unit
152      * @param standardUnit boolean; indicates whether it is a standard unit with a definition in the locale, or a user-defined
153      *            unit
154      */
155     private SpeedUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem,
156             final SpeedUnit referenceUnit, final double scaleFactorToReferenceUnit, final boolean standardUnit)
157     {
158         super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, referenceUnit, scaleFactorToReferenceUnit,
159                 standardUnit);
160         this.lengthUnit = referenceUnit.getLengthUnit();
161         this.durationUnit = referenceUnit.getDurationUnit();
162     }
163 
164     /**
165      * Build a user-defined SpeedUnit with a conversion factor to another SpeedUnit.
166      * @param name String; the long name of the unit
167      * @param abbreviation String; the abbreviation of the unit
168      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
169      * @param referenceUnit SpeedUnit; the unit to convert to
170      * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
171      *            unit
172      */
173     public SpeedUnit(final String name, final String abbreviation, final UnitSystem unitSystem, final SpeedUnit referenceUnit,
174             final double scaleFactorToReferenceUnit)
175     {
176         this(name, abbreviation, unitSystem, referenceUnit, scaleFactorToReferenceUnit, false);
177     }
178 
179     /**
180      * @return lengthUnit
181      */
182     public final LengthUnit getLengthUnit()
183     {
184         return this.lengthUnit;
185     }
186 
187     /**
188      * @return durationUnit
189      */
190     public final DurationUnit getDurationUnit()
191     {
192         return this.durationUnit;
193     }
194 
195     /** {@inheritDoc} */
196     @Override
197     public final SpeedUnit getStandardUnit()
198     {
199         return METER_PER_SECOND;
200     }
201 
202     /** {@inheritDoc} */
203     @Override
204     public final String getSICoefficientsString()
205     {
206         return "m/s";
207     }
208 
209 }