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