View Javadoc
1   package org.opentrafficsim.core.unit;
2   
3   import static org.opentrafficsim.core.unit.unitsystem.UnitSystem.OTHER;
4   import static org.opentrafficsim.core.unit.unitsystem.UnitSystem.SI_ACCEPTED;
5   import static org.opentrafficsim.core.unit.unitsystem.UnitSystem.SI_DERIVED;
6   
7   import org.opentrafficsim.core.unit.unitsystem.UnitSystem;
8   
9   /**
10   * Standard frequency units based on time.
11   * <p>
12   * Copyright (c) 2014 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
13   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
14   * <p>
15   * @version May 15, 2014 <br>
16   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
17   */
18  public class FrequencyUnit extends Unit<FrequencyUnit>
19  {
20      /** */
21      private static final long serialVersionUID = 20140607L;
22  
23      /** the actual time unit, e.g. second. */
24      private final TimeUnit timeUnit;
25  
26      /** The SI unit for frequency is Hertz. */
27      public static final FrequencyUnit SI;
28  
29      /** Hertz. */
30      public static final FrequencyUnit HERTZ;
31  
32      /** kiloHertz. */
33      public static final FrequencyUnit KILOHERTZ;
34  
35      /** megaHertz. */
36      public static final FrequencyUnit MEGAHERTZ;
37  
38      /** gigaHertz. */
39      public static final FrequencyUnit GIGAHERTZ;
40  
41      /** teraHertz. */
42      public static final FrequencyUnit TERAHERTZ;
43  
44      /** Revolutions per minute = 1/60 Hz. */
45      public static final FrequencyUnit RPM;
46  
47      /** 1/s. */
48      public static final FrequencyUnit PER_SECOND;
49  
50      /** 1/min. */
51      public static final FrequencyUnit PER_MINUTE;
52  
53      /** 1/hour. */
54      public static final FrequencyUnit PER_HOUR;
55  
56      /** 1/day. */
57      public static final FrequencyUnit PER_DAY;
58  
59      static
60      {
61          SI = new FrequencyUnit(TimeUnit.SECOND, "FrequencyUnit.Hertz", "FrequencyUnit.Hz", SI_DERIVED);
62          HERTZ = SI;
63          KILOHERTZ = new FrequencyUnit("FrequencyUnit.kilohertz", "FrequencyUnit.kHz", SI_DERIVED, HERTZ, 1000.0);
64          MEGAHERTZ = new FrequencyUnit("FrequencyUnit.megahertz", "FrequencyUnit.MHz", SI_DERIVED, HERTZ, 1.0E6);
65          GIGAHERTZ = new FrequencyUnit("FrequencyUnit.gigahertz", "FrequencyUnit.GHz", SI_DERIVED, HERTZ, 1.0E9);
66          TERAHERTZ = new FrequencyUnit("FrequencyUnit.terahertz", "FrequencyUnit.THz", SI_DERIVED, HERTZ, 1.0E12);
67          RPM = new FrequencyUnit("FrequencyUnit.revolutions_per_minute", "FrequencyUnit.rpm", OTHER, HERTZ, 1.0 / 60.0);
68          PER_SECOND = new FrequencyUnit(TimeUnit.SECOND, "FrequencyUnit.per_second", "FrequencyUnit.1/s", SI_DERIVED);
69          PER_MINUTE = new FrequencyUnit(TimeUnit.MINUTE, "FrequencyUnit.per_minute", "FrequencyUnit.1/min", SI_ACCEPTED);
70          PER_HOUR = new FrequencyUnit(TimeUnit.HOUR, "FrequencyUnit.per_hour", "FrequencyUnit.1/h", SI_ACCEPTED);
71          PER_DAY = new FrequencyUnit(TimeUnit.DAY, "FrequencyUnit.per_day", "FrequencyUnit.1/d", SI_ACCEPTED);
72      }
73  
74      /**
75       * Define frequency unit based on time. You can define unit like "per second" (Hertz) here.
76       * @param timeUnit the unit of time for the frequency unit, e.g., second
77       * @param nameKey the key to the locale file for the long name of the unit
78       * @param abbreviationKey the key to the locale file for the abbreviation of the unit
79       * @param unitSystem the unit system, e.g. SI or Imperial
80       */
81      public FrequencyUnit(final TimeUnit timeUnit, final String nameKey, final String abbreviationKey,
82              final UnitSystem unitSystem)
83      {
84          super(nameKey, abbreviationKey, unitSystem, HERTZ, 1.0 / timeUnit.getConversionFactorToStandardUnit(), true);
85          this.timeUnit = timeUnit;
86      }
87  
88      /**
89       * Build a unit with a conversion factor to another unit.
90       * @param nameKey the key to the locale file for the long name of the unit
91       * @param abbreviationKey the key to the locale file for the abbreviation of the unit
92       * @param unitSystem the unit system, e.g. SI or Imperial
93       * @param referenceUnit the unit to convert to
94       * @param conversionFactorToReferenceUnit multiply a value in this unit by the factor to convert to the given
95       *            reference unit
96       */
97      public FrequencyUnit(final String nameKey, final String abbreviationKey, final UnitSystem unitSystem,
98              final FrequencyUnit referenceUnit, final double conversionFactorToReferenceUnit)
99      {
100         super(nameKey, abbreviationKey, unitSystem, referenceUnit, conversionFactorToReferenceUnit, true);
101         this.timeUnit = referenceUnit.getTimeUnit();
102     }
103 
104     /**
105      * @return timeUnit
106      */
107     public final TimeUnit getTimeUnit()
108     {
109         return this.timeUnit;
110     }
111 
112     /** {@inheritDoc} */
113     @Override
114     public final FrequencyUnit getStandardUnit()
115     {
116         return HERTZ;
117     }
118 
119     /** {@inheritDoc} */
120     @Override
121     public final String getSICoefficientsString()
122     {
123         return "s-1";
124     }
125 
126 }