View Javadoc
1   package org.djunits.unit;
2   
3   import org.djunits.unit.unitsystem.UnitSystem;
4   
5   /**
6    * MoneyPerDurationUnit defines a unit for the cost or benefit per time, e.g. wage per hour.
7    * <p>
8    * Copyright (c) 2015-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
9    * BSD-style license. See <a href="http://djunits.org/docs/license.html">DJUNITS License</a>.
10   * <p>
11   * $LastChangedDate: 2018-01-28 03:17:44 +0100 (Sun, 28 Jan 2018) $, @version $Revision: 256 $, by $Author: averbraeck $,
12   * initial version Sep 5, 2015 <br>
13   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
14   */
15  public class MoneyPerDurationUnit extends LinearUnit<MoneyPerDurationUnit>
16  {
17      /** */
18      private static final long serialVersionUID = 20150905L;
19  
20      /** The unit of money for the money per time unit, e.g., EUR. */
21      private final MoneyUnit moneyUnit;
22  
23      /** The unit of time for the money per time unit, e.g., hour. */
24      private final DurationUnit durationUnit;
25  
26      /** Euro per hour. */
27      public static final MoneyPerDurationUnit EUR_PER_HOUR;
28  
29      /** Euro per day. */
30      public static final MoneyPerDurationUnit EUR_PER_DAY;
31  
32      /** US$ per hour. */
33      public static final MoneyPerDurationUnit USD_PER_HOUR;
34  
35      /** US$ per day. */
36      public static final MoneyPerDurationUnit USD_PER_DAY;
37  
38      /** The standard unit to use. */
39      private static MoneyPerDurationUnit standardMoneyPerDurationUnit;
40  
41      static
42      {
43          EUR_PER_HOUR = new MoneyPerDurationUnit(MoneyUnit.EUR, DurationUnit.HOUR, "EUR per hour", "\u20AC/h", false);
44          EUR_PER_DAY = new MoneyPerDurationUnit(MoneyUnit.EUR, DurationUnit.DAY, "EUR per day", "\u20AC/day", false);
45          USD_PER_HOUR = new MoneyPerDurationUnit(MoneyUnit.USD, DurationUnit.HOUR, "USD per hour", "US$/h", false);
46          USD_PER_DAY = new MoneyPerDurationUnit(MoneyUnit.USD, DurationUnit.DAY, "USD per day", "US$/day", false);
47          standardMoneyPerDurationUnit = EUR_PER_HOUR;
48      }
49  
50      /**
51       * Build a money per time unit from a money unit and an time unit.
52       * @param moneyUnit the unit of money for the money per time unit, e.g., EUR
53       * @param durationUnit the unit of time for the money per time unit, e.g., hour
54       * @param nameOrNameKey if standardUnit: the key to the locale file for the long name of the unit, otherwise the name itself
55       * @param abbreviationOrAbbreviationKey if standardUnit: the key to the locale file for the abbreviation of the unit,
56       *            otherwise the abbreviation itself
57       * @param standardUnit indicates whether it is a standard unit with a definition in the locale, or a user-defined unit
58       */
59      private MoneyPerDurationUnit(final MoneyUnit moneyUnit, final DurationUnit durationUnit, final String nameOrNameKey,
60              final String abbreviationOrAbbreviationKey, final boolean standardUnit)
61      {
62          super(nameOrNameKey, abbreviationOrAbbreviationKey, UnitSystem.OTHER, standardMoneyPerDurationUnit,
63                  moneyUnit.getScaleFactor() / durationUnit.getScaleFactor(), standardUnit);
64          this.moneyUnit = moneyUnit;
65          this.durationUnit = durationUnit;
66      }
67  
68      /**
69       * Build a user-defined money per time unit from a money unit and an time unit.
70       * @param moneyUnit the unit of money for the money per time unit, e.g., EUR
71       * @param durationUnit the unit of time for the money per time unit, e.g., hour
72       * @param name the key to the locale file for the long name of the unit
73       * @param abbreviation the key to the locale file for the abbreviation of the unit
74       */
75      public MoneyPerDurationUnit(final MoneyUnit moneyUnit, final DurationUnit durationUnit, final String name,
76              final String abbreviation)
77      {
78          this(moneyUnit, durationUnit, name, abbreviation, false);
79      }
80  
81      /**
82       * Build a MoneyPerDurationUnit unit based on another MoneyPerDurationUnit.
83       * @param nameOrNameKey if standardUnit: the key to the locale file for the long name of the unit, otherwise the name itself
84       * @param abbreviationOrAbbreviationKey if standardUnit: the key to the locale file for the abbreviation of the unit,
85       *            otherwise the abbreviation itself
86       * @param referenceUnit the unit to convert to
87       * @param scaleFactorToReferenceUnit multiply a value in this unit by the factor to convert to the given reference unit
88       * @param standardUnit indicates whether it is a standard unit with a definition in the locale, or a user-defined unit
89       */
90      private MoneyPerDurationUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey,
91              final MoneyPerDurationUnit referenceUnit, final double scaleFactorToReferenceUnit, final boolean standardUnit)
92      {
93          super(nameOrNameKey, abbreviationOrAbbreviationKey, UnitSystem.OTHER, referenceUnit, scaleFactorToReferenceUnit,
94                  standardUnit);
95          this.moneyUnit = referenceUnit.getMoneyUnit();
96          this.durationUnit = referenceUnit.getDurationUnit();
97      }
98  
99      /**
100      * Build a user-defined MoneyPerDurationUnit with a conversion factor to another MoneyPerDurationUnit.
101      * @param name the long name of the unit
102      * @param abbreviation the abbreviation of the unit
103      * @param referenceUnit the unit to convert to
104      * @param scaleFactorToReferenceUnit multiply a value in this unit by the factor to convert to the given reference unit
105      */
106     public MoneyPerDurationUnit(final String name, final String abbreviation, final MoneyPerDurationUnit referenceUnit,
107             final double scaleFactorToReferenceUnit)
108     {
109         this(name, abbreviation, referenceUnit, scaleFactorToReferenceUnit, false);
110     }
111 
112     /**
113      * @return moneyUnit
114      */
115     public final MoneyUnit getMoneyUnit()
116     {
117         return this.moneyUnit;
118     }
119 
120     /**
121      * @return durationUnit
122      */
123     public final DurationUnit getDurationUnit()
124     {
125         return this.durationUnit;
126     }
127 
128     /**
129      * Set the standard MoneyPerDurationUnit in case the standard MoneyUnit changes, as the standard money unit is flexible.
130      * @param standardMoneyUnit the new standard money unit.
131      */
132     protected static void setStandardUnit(final MoneyUnit standardMoneyUnit)
133     {
134         try
135         {
136             standardMoneyPerDurationUnit = new MoneyPerDurationUnit(standardMoneyUnit, DurationUnit.HOUR,
137                     standardMoneyUnit.getName() + " per hour", standardMoneyUnit.getAbbreviation() + "/h");
138         }
139         catch (Exception e)
140         {
141             // ignore a RunTimeException that indicates that the Unit might already exist
142             e = null;
143         }
144     }
145 
146     /** {@inheritDoc} */
147     @Override
148     public final MoneyPerDurationUnit getStandardUnit()
149     {
150         return standardMoneyPerDurationUnit;
151     }
152 
153     /**
154      * @return standard MoneyPerDurationUnit
155      */
156     public static MoneyPerDurationUnit getStandardMoneyPerDurationUnit()
157     {
158         return standardMoneyPerDurationUnit;
159     }
160 
161     /** {@inheritDoc} */
162     @Override
163     public final String getSICoefficientsString()
164     {
165         return "1/s";
166     }
167 
168 }