View Javadoc
1   package org.djunits.unit;
2   
3   import static org.djunits.unit.unitsystem.UnitSystem.OTHER;
4   import static org.djunits.unit.unitsystem.UnitSystem.SI_ACCEPTED;
5   import static org.djunits.unit.unitsystem.UnitSystem.SI_DERIVED;
6   
7   import org.djunits.unit.unitsystem.UnitSystem;
8   
9   /**
10   * Standard frequency unit based on time.
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 FrequencyUnit extends LinearUnit<FrequencyUnit>
20  {
21      /** */
22      private static final long serialVersionUID = 20140607L;
23  
24      /** the actual time unit, e.g. second. */
25      private final DurationUnit durationUnit;
26  
27      /** The SI unit for frequency is Hertz. */
28      public static final FrequencyUnit SI;
29  
30      /** Hertz. */
31      public static final FrequencyUnit HERTZ;
32  
33      /** kiloHertz. */
34      public static final FrequencyUnit KILOHERTZ;
35  
36      /** megaHertz. */
37      public static final FrequencyUnit MEGAHERTZ;
38  
39      /** gigaHertz. */
40      public static final FrequencyUnit GIGAHERTZ;
41  
42      /** teraHertz. */
43      public static final FrequencyUnit TERAHERTZ;
44  
45      /** Revolutions per minute = 1/60 Hz. */
46      public static final FrequencyUnit RPM;
47  
48      /** 1/attosecond. */
49      public static final FrequencyUnit PER_ATTOSECOND;
50  
51      /** 1/femtosecond. */
52      public static final FrequencyUnit PER_FEMTOSECOND;
53  
54      /** 1/picosecond. */
55      public static final FrequencyUnit PER_PICOSECOND;
56  
57      /** 1/nanosecond. */
58      public static final FrequencyUnit PER_NANOSECOND;
59  
60      /** 1/microsecond. */
61      public static final FrequencyUnit PER_MICROSECOND;
62  
63      /** 1/millisecond. */
64      public static final FrequencyUnit PER_MILLISECOND;
65  
66      /** 1/s. */
67      public static final FrequencyUnit PER_SECOND;
68  
69      /** 1/min. */
70      public static final FrequencyUnit PER_MINUTE;
71  
72      /** 1/hour. */
73      public static final FrequencyUnit PER_HOUR;
74  
75      /** 1/day. */
76      public static final FrequencyUnit PER_DAY;
77  
78      /** 1/week. */
79      public static final FrequencyUnit PER_WEEK;
80  
81      static
82      {
83          SI = new FrequencyUnit(DurationUnit.SECOND, "FrequencyUnit.Hertz", "FrequencyUnit.Hz", SI_DERIVED, true);
84          HERTZ = SI;
85          KILOHERTZ = new FrequencyUnit("FrequencyUnit.kilohertz", "FrequencyUnit.kHz", SI_DERIVED, HERTZ, 1000.0, true);
86          MEGAHERTZ = new FrequencyUnit("FrequencyUnit.megahertz", "FrequencyUnit.MHz", SI_DERIVED, HERTZ, 1.0E6, true);
87          GIGAHERTZ = new FrequencyUnit("FrequencyUnit.gigahertz", "FrequencyUnit.GHz", SI_DERIVED, HERTZ, 1.0E9, true);
88          TERAHERTZ = new FrequencyUnit("FrequencyUnit.terahertz", "FrequencyUnit.THz", SI_DERIVED, HERTZ, 1.0E12, true);
89          RPM = new FrequencyUnit("FrequencyUnit.revolutions_per_minute", "FrequencyUnit.rpm", OTHER, HERTZ, 1.0 / 60.0, true);
90          PER_ATTOSECOND = new FrequencyUnit(DurationUnit.ATTOSECOND, "FrequencyUnit.per_attosecond", "FrequencyUnit.1/as",
91                  SI_DERIVED, true);
92          PER_FEMTOSECOND = new FrequencyUnit(DurationUnit.FEMTOSECOND, "FrequencyUnit.per_femtosecond", "FrequencyUnit.1/fs",
93                  SI_DERIVED, true);
94          PER_PICOSECOND = new FrequencyUnit(DurationUnit.PICOSECOND, "FrequencyUnit.per_picosecond", "FrequencyUnit.1/ps",
95                  SI_DERIVED, true);
96          PER_NANOSECOND = new FrequencyUnit(DurationUnit.NANOSECOND, "FrequencyUnit.per_nanosecond", "FrequencyUnit.1/ns",
97                  SI_DERIVED, true);
98          PER_MICROSECOND = new FrequencyUnit(DurationUnit.MICROSECOND, "FrequencyUnit.per_microsecond", "FrequencyUnit.1/mus",
99                  SI_DERIVED, true);
100         PER_MILLISECOND = new FrequencyUnit(DurationUnit.MILLISECOND, "FrequencyUnit.per_millisecond", "FrequencyUnit.1/ms",
101                 SI_DERIVED, true);
102         PER_SECOND = new FrequencyUnit(DurationUnit.SECOND, "FrequencyUnit.per_second", "FrequencyUnit.1/s", SI_DERIVED, true);
103         PER_MINUTE =
104                 new FrequencyUnit(DurationUnit.MINUTE, "FrequencyUnit.per_minute", "FrequencyUnit.1/min", SI_ACCEPTED, true);
105         PER_HOUR = new FrequencyUnit(DurationUnit.HOUR, "FrequencyUnit.per_hour", "FrequencyUnit.1/h", SI_ACCEPTED, true);
106         PER_DAY = new FrequencyUnit(DurationUnit.DAY, "FrequencyUnit.per_day", "FrequencyUnit.1/d", SI_ACCEPTED, true);
107         PER_WEEK = new FrequencyUnit(DurationUnit.WEEK, "FrequencyUnit.per_week", "FrequencyUnit.1/w", OTHER, true);
108     }
109 
110     /**
111      * Define a FrequencyUnit based on time. You can define unit like "per second" (Hertz) here.
112      * @param durationUnit DurationUnit; the unit of time for the frequency unit, e.g., second
113      * @param nameOrNameKey String; if standardUnit: the key to the locale file for the long name of the unit, otherwise the
114      *            name itself
115      * @param abbreviationOrAbbreviationKey String; if standardUnit: the key to the locale file for the abbreviation of the
116      *            unit, otherwise the abbreviation itself
117      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
118      * @param standardUnit boolean; indicates whether it is a standard unit with a definition in the locale, or a user-defined
119      *            unit
120      */
121     private FrequencyUnit(final DurationUnit durationUnit, final String nameOrNameKey,
122             final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem, final boolean standardUnit)
123     {
124         super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, HERTZ, 1.0 / durationUnit.getScaleFactor(),
125                 standardUnit);
126         this.durationUnit = durationUnit;
127     }
128 
129     /**
130      * Define a user-defined FrequencyUnit based on time. You can define unit like "per fortnight" here.
131      * @param durationUnit DurationUnit; the unit of time for the frequency 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 FrequencyUnit(final DurationUnit durationUnit, final String name, final String abbreviation,
137             final UnitSystem unitSystem)
138     {
139         this(durationUnit, name, abbreviation, unitSystem, false);
140     }
141 
142     /**
143      * Build a FrequencyUnit with a conversion factor to another FrequencyUnit.
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 FrequencyUnit; 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 FrequencyUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem,
156             final FrequencyUnit referenceUnit, final double scaleFactorToReferenceUnit, final boolean standardUnit)
157     {
158         super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, referenceUnit, scaleFactorToReferenceUnit,
159                 standardUnit);
160         this.durationUnit = referenceUnit.getDurationUnit();
161     }
162 
163     /**
164      * Build a user-defined FrequencyUnit with a conversion factor to another FrequencyUnit.
165      * @param name String; the long name of the unit
166      * @param abbreviation String; the abbreviation of the unit
167      * @param unitSystem UnitSystem; the unit system, e.g. SI or Imperial
168      * @param referenceUnit FrequencyUnit; the unit to convert to
169      * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
170      *            unit
171      */
172     public FrequencyUnit(final String name, final String abbreviation, final UnitSystem unitSystem,
173             final FrequencyUnit referenceUnit, final double scaleFactorToReferenceUnit)
174     {
175         this(name, abbreviation, unitSystem, referenceUnit, scaleFactorToReferenceUnit, false);
176     }
177 
178     /**
179      * @return durationUnit
180      */
181     public final DurationUnit getDurationUnit()
182     {
183         return this.durationUnit;
184     }
185 
186     /** {@inheritDoc} */
187     @Override
188     public final FrequencyUnit getStandardUnit()
189     {
190         return HERTZ;
191     }
192 
193     /** {@inheritDoc} */
194     @Override
195     public final String getSICoefficientsString()
196     {
197         return "1/s";
198     }
199 
200 }