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-2019 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: 2019-03-02 19:06:46 +0100 (Sat, 02 Mar 2019) $, @version $Revision: 342 $, 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/h", "\u20AC/h");
44          EUR_PER_DAY = new MoneyPerDurationUnit(MoneyUnit.EUR, DurationUnit.DAY, "EUR/day", "\u20AC/day");
45          USD_PER_HOUR = new MoneyPerDurationUnit(MoneyUnit.USD, DurationUnit.HOUR, "US$/h", "US$/h");
46          USD_PER_DAY = new MoneyPerDurationUnit(MoneyUnit.USD, DurationUnit.DAY, "US$/day", "US$/day");
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 MoneyUnit; the unit of money for the money per time unit, e.g., EUR
53       * @param durationUnit DurationUnit; the unit of time for the money per time unit, e.g., hour
54       * @param abbreviationKey String; the key to the locale file for the abbreviation of the unit
55       */
56      private MoneyPerDurationUnit(final MoneyUnit moneyUnit, final DurationUnit durationUnit, final String abbreviationKey)
57      {
58          super(abbreviationKey, UnitSystem.OTHER, standardMoneyPerDurationUnit,
59                  moneyUnit.getScaleFactor() / durationUnit.getScaleFactor());
60          this.moneyUnit = moneyUnit;
61          this.durationUnit = durationUnit;
62      }
63  
64      /**
65       * Build a user-defined money per time unit from a money unit and an time unit.
66       * @param moneyUnit MoneyUnit; the unit of money for the money per time unit, e.g., EUR
67       * @param durationUnit DurationUnit; the unit of time for the money per time unit, e.g., hour
68       * @param name String; the key to the locale file for the long name of the unit
69       * @param abbreviation String; the key to the locale file for the abbreviation of the unit
70       */
71      public MoneyPerDurationUnit(final MoneyUnit moneyUnit, final DurationUnit durationUnit, final String name,
72              final String abbreviation)
73      {
74          super(name, abbreviation, UnitSystem.OTHER, standardMoneyPerDurationUnit,
75                  moneyUnit.getScaleFactor() / durationUnit.getScaleFactor());
76          this.moneyUnit = moneyUnit;
77          this.durationUnit = durationUnit;
78      }
79  
80      /**
81       * Build a MoneyPerDurationUnit unit based on another MoneyPerDurationUnit.
82       * @param abbreviationKey String; the key to the locale file for the abbreviation of the unit
83       * @param referenceUnit MoneyPerDurationUnit; the unit to convert to
84       * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
85       *            unit
86       */
87      private MoneyPerDurationUnit(final String abbreviationKey, final MoneyPerDurationUnit referenceUnit,
88              final double scaleFactorToReferenceUnit)
89      {
90          super(abbreviationKey, UnitSystem.OTHER, referenceUnit, scaleFactorToReferenceUnit);
91          this.moneyUnit = referenceUnit.getMoneyUnit();
92          this.durationUnit = referenceUnit.getDurationUnit();
93      }
94  
95      /**
96       * Build a user-defined MoneyPerDurationUnit with a conversion factor to another MoneyPerDurationUnit.
97       * @param name String; the long name of the unit
98       * @param abbreviation String; the abbreviation of the unit
99       * @param referenceUnit MoneyPerDurationUnit; the unit to convert to
100      * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
101      *            unit
102      */
103     public MoneyPerDurationUnit(final String name, final String abbreviation, final MoneyPerDurationUnit referenceUnit,
104             final double scaleFactorToReferenceUnit)
105     {
106         super(name, abbreviation, UnitSystem.OTHER, referenceUnit, scaleFactorToReferenceUnit);
107         this.moneyUnit = referenceUnit.getMoneyUnit();
108         this.durationUnit = referenceUnit.getDurationUnit();
109     }
110 
111     /**
112      * @return moneyUnit
113      */
114     public final MoneyUnit getMoneyUnit()
115     {
116         return this.moneyUnit;
117     }
118 
119     /**
120      * @return durationUnit
121      */
122     public final DurationUnit getDurationUnit()
123     {
124         return this.durationUnit;
125     }
126 
127     /**
128      * Set the standard MoneyPerDurationUnit in case the standard MoneyUnit changes, as the standard money unit is flexible.
129      * @param standardMoneyUnit MoneyUnit; the new standard money unit.
130      */
131     protected static void setStandardUnit(final MoneyUnit standardMoneyUnit)
132     {
133         try
134         {
135             standardMoneyPerDurationUnit = new MoneyPerDurationUnit(standardMoneyUnit, DurationUnit.HOUR,
136                     standardMoneyUnit.getName() + " per hour", standardMoneyUnit.getAbbreviation() + "/h");
137         }
138         catch (Exception e)
139         {
140             // ignore a RunTimeException that indicates that the Unit might already exist
141             e = null;
142         }
143     }
144 
145     /** {@inheritDoc} */
146     @Override
147     public final MoneyPerDurationUnit getStandardUnit()
148     {
149         return standardMoneyPerDurationUnit;
150     }
151 
152     /**
153      * @return standard MoneyPerDurationUnit
154      */
155     public static MoneyPerDurationUnit getStandardMoneyPerDurationUnit()
156     {
157         return standardMoneyPerDurationUnit;
158     }
159 
160     /** {@inheritDoc} */
161     @Override
162     public final String getSICoefficientsString()
163     {
164         return "1/s";
165     }
166 
167     /** {@inheritDoc} */
168     @Override
169     public int hashCode()
170     {
171         final int prime = 31;
172         int result = super.hashCode();
173         result = prime * result + ((this.durationUnit == null) ? 0 : this.durationUnit.hashCode());
174         result = prime * result + ((this.moneyUnit == null) ? 0 : this.moneyUnit.hashCode());
175         return result;
176     }
177 
178     /** {@inheritDoc} */
179     @SuppressWarnings("checkstyle:needbraces")
180     @Override
181     public boolean equals(final Object obj)
182     {
183         if (this == obj)
184             return true;
185         if (!super.equals(obj))
186             return false;
187         if (getClass() != obj.getClass())
188             return false;
189         MoneyPerDurationUnit other = (MoneyPerDurationUnit) obj;
190         if (this.durationUnit == null)
191         {
192             if (other.durationUnit != null)
193                 return false;
194         }
195         else if (!this.durationUnit.equals(other.durationUnit))
196             return false;
197         if (this.moneyUnit == null)
198         {
199             if (other.moneyUnit != null)
200                 return false;
201         }
202         else if (!this.moneyUnit.equals(other.moneyUnit))
203             return false;
204         return true;
205     }
206 
207 }