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-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 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 the unit of time for the frequency unit, e.g., second
113      * @param nameOrNameKey if standardUnit: the key to the locale file for the long name of the unit, otherwise the name itself
114      * @param abbreviationOrAbbreviationKey if standardUnit: the key to the locale file for the abbreviation of the unit,
115      *            otherwise the abbreviation itself
116      * @param unitSystem the unit system, e.g. SI or Imperial
117      * @param standardUnit indicates whether it is a standard unit with a definition in the locale, or a user-defined unit
118      */
119     private FrequencyUnit(final DurationUnit durationUnit, final String nameOrNameKey,
120             final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem, final boolean standardUnit)
121     {
122         super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, HERTZ, 1.0 / durationUnit.getScaleFactor(),
123                 standardUnit);
124         this.durationUnit = durationUnit;
125     }
126 
127     /**
128      * Define a user-defined FrequencyUnit based on time. You can define unit like "per fortnight" here.
129      * @param durationUnit the unit of time for the frequency 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 FrequencyUnit(final DurationUnit durationUnit, final String name, final String abbreviation,
135             final UnitSystem unitSystem)
136     {
137         this(durationUnit, name, abbreviation, unitSystem, false);
138     }
139 
140     /**
141      * Build a FrequencyUnit with a conversion factor to another FrequencyUnit.
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 FrequencyUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey, final UnitSystem unitSystem,
151             final FrequencyUnit referenceUnit, final double scaleFactorToReferenceUnit, final boolean standardUnit)
152     {
153         super(nameOrNameKey, abbreviationOrAbbreviationKey, unitSystem, referenceUnit, scaleFactorToReferenceUnit,
154                 standardUnit);
155         this.durationUnit = referenceUnit.getDurationUnit();
156     }
157 
158     /**
159      * Build a user-defined FrequencyUnit with a conversion factor to another FrequencyUnit.
160      * @param name the long name of the unit
161      * @param abbreviation the abbreviation of the unit
162      * @param unitSystem the unit system, e.g. SI or Imperial
163      * @param referenceUnit the unit to convert to
164      * @param scaleFactorToReferenceUnit multiply a value in this unit by the factor to convert to the given reference unit
165      */
166     public FrequencyUnit(final String name, final String abbreviation, final UnitSystem unitSystem,
167             final FrequencyUnit referenceUnit, final double scaleFactorToReferenceUnit)
168     {
169         this(name, abbreviation, unitSystem, referenceUnit, scaleFactorToReferenceUnit, false);
170     }
171 
172     /**
173      * @return durationUnit
174      */
175     public final DurationUnit getDurationUnit()
176     {
177         return this.durationUnit;
178     }
179 
180     /** {@inheritDoc} */
181     @Override
182     public final FrequencyUnit getStandardUnit()
183     {
184         return HERTZ;
185     }
186 
187     /** {@inheritDoc} */
188     @Override
189     public final String getSICoefficientsString()
190     {
191         return "1/s";
192     }
193 
194 }