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-2018 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: 2018-01-28 03:17:44 +0100 (Sun, 28 Jan 2018) $, @version $Revision: 256 $, 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 the unit of length for the speed unit, e.g., meter
110      * @param durationUnit the unit of time for the speed unit, e.g., second
111      * @param nameOrNameKey if standardUnit: the key to the locale file for the long name of the unit, otherwise the name itself
112      * @param abbreviationOrAbbreviationKey if standardUnit: the key to the locale file for the abbreviation of the unit,
113      *            otherwise the abbreviation itself
114      * @param unitSystem the unit system, e.g. SI or Imperial
115      * @param standardUnit indicates whether it is a standard unit with a definition in the locale, or a user-defined unit
116      */
117     private SpeedUnit(final LengthUnit lengthUnit, final DurationUnit durationUnit, final String nameOrNameKey,
118             final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem, final boolean standardUnit)
119     {
120         super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, METER_PER_SECOND,
121                 lengthUnit.getScaleFactor() / durationUnit.getScaleFactor(), standardUnit);
122         this.lengthUnit = lengthUnit;
123         this.durationUnit = durationUnit;
124     }
125 
126     /**
127      * Build a user-defined speed unit from a length unit and a time unit.
128      * @param lengthUnit the unit of length for the speed unit, e.g., meter
129      * @param durationUnit the unit of time for the speed unit, e.g., second
130      * @param name the long name of the unit
131      * @param abbreviation the abbreviation of the unit
132      * @param unitSystem the unit system, e.g. SI or Imperial
133      */
134     public SpeedUnit(final LengthUnit lengthUnit, final DurationUnit durationUnit, final String name, final String abbreviation,
135             final UnitSystem unitSystem)
136     {
137         this(lengthUnit, durationUnit, name, abbreviation, unitSystem, false);
138     }
139 
140     /**
141      * Build a SpeedUnit based on another SpeedUnit.
142      * @param nameOrNameKey if standardUnit: the key to the locale file for the long name of the unit, otherwise the name itself
143      * @param abbreviationOrAbbreviationKey if standardUnit: the key to the locale file for the abbreviation of the unit,
144      *            otherwise the abbreviation itself
145      * @param unitSystem the unit system, e.g. SI or Imperial
146      * @param referenceUnit the unit to convert to
147      * @param scaleFactorToReferenceUnit multiply a value in this unit by the factor to convert to the given reference unit
148      * @param standardUnit indicates whether it is a standard unit with a definition in the locale, or a user-defined unit
149      */
150     private SpeedUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem,
151             final SpeedUnit referenceUnit, final double scaleFactorToReferenceUnit, final boolean standardUnit)
152     {
153         super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, referenceUnit, scaleFactorToReferenceUnit,
154                 standardUnit);
155         this.lengthUnit = referenceUnit.getLengthUnit();
156         this.durationUnit = referenceUnit.getDurationUnit();
157     }
158 
159     /**
160      * Build a user-defined SpeedUnit with a conversion factor to another SpeedUnit.
161      * @param name the long name of the unit
162      * @param abbreviation the abbreviation of the unit
163      * @param unitSystem the unit system, e.g. SI or Imperial
164      * @param referenceUnit the unit to convert to
165      * @param scaleFactorToReferenceUnit multiply a value in this unit by the factor to convert to the given reference unit
166      */
167     public SpeedUnit(final String name, final String abbreviation, final UnitSystem unitSystem, final SpeedUnit referenceUnit,
168             final double scaleFactorToReferenceUnit)
169     {
170         this(name, abbreviation, unitSystem, referenceUnit, scaleFactorToReferenceUnit, false);
171     }
172 
173     /**
174      * @return lengthUnit
175      */
176     public final LengthUnit getLengthUnit()
177     {
178         return this.lengthUnit;
179     }
180 
181     /**
182      * @return durationUnit
183      */
184     public final DurationUnit getDurationUnit()
185     {
186         return this.durationUnit;
187     }
188 
189     /** {@inheritDoc} */
190     @Override
191     public final SpeedUnit getStandardUnit()
192     {
193         return METER_PER_SECOND;
194     }
195 
196     /** {@inheritDoc} */
197     @Override
198     public final String getSICoefficientsString()
199     {
200         return "m/s";
201     }
202 
203 }