View Javadoc
1   package org.djunits.unit;
2   
3   import org.djunits.unit.unitsystem.UnitSystem;
4   
5   /**
6    * MoneyPerMassUnit defines a unit for the cost or benefit per mass, e.g. the cost of a kilogram of material.
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 MoneyPerMassUnit extends LinearUnit<MoneyPerMassUnit>
16  {
17      /** */
18      private static final long serialVersionUID = 20150905L;
19  
20      /** The unit of money for the money per mass unit, e.g., EUR. */
21      private final MoneyUnit moneyUnit;
22  
23      /** The unit of mass for the money per mass unit, e.g., kg. */
24      private final MassUnit massUnit;
25  
26      /** Euro per kg. */
27      public static final MoneyPerMassUnit EUR_PER_KILOGRAM;
28  
29      /** Euro per metric ton. */
30      public static final MoneyPerMassUnit EUR_PER_METRIC_TON;
31  
32      /** US$ per pound. */
33      public static final MoneyPerMassUnit USD_PER_POUND;
34  
35      /** US$ per long ton. */
36      public static final MoneyPerMassUnit USD_PER_LONG_TON;
37  
38      /** US$ per short ton. */
39      public static final MoneyPerMassUnit USD_PER_SHORT_TON;
40  
41      /** The standard unit to use. */
42      private static MoneyPerMassUnit standardMoneyPerMassUnit;
43  
44      static
45      {
46          EUR_PER_KILOGRAM = new MoneyPerMassUnit(MoneyUnit.EUR, MassUnit.KILOGRAM, "EUR per kilogram", "\u20AC/kg", false);
47          EUR_PER_METRIC_TON =
48                  new MoneyPerMassUnit(MoneyUnit.EUR, MassUnit.TON_METRIC, "EUR per metric ton", "\u20AC/ton(metric)", false);
49          USD_PER_POUND = new MoneyPerMassUnit(MoneyUnit.USD, MassUnit.POUND, "USD per pound", "US$/lb", false);
50          USD_PER_LONG_TON = new MoneyPerMassUnit(MoneyUnit.USD, MassUnit.TON_LONG, "USD per long ton", "US$/ton(long)", false);
51          USD_PER_SHORT_TON =
52                  new MoneyPerMassUnit(MoneyUnit.USD, MassUnit.TON_SHORT, "USD per short ton", "US$/ton(short)", false);
53          standardMoneyPerMassUnit = EUR_PER_KILOGRAM;
54      }
55  
56      /**
57       * Build a money per mass unit from a money unit and an mass unit.
58       * @param moneyUnit MoneyUnit; the unit of money for the money per mass unit, e.g., EUR
59       * @param massUnit MassUnit; the unit of mass for the money per mass unit, e.g., kg
60       * @param nameOrNameKey String; if standardUnit: the key to the locale file for the long name of the unit, otherwise the
61       *            name itself
62       * @param abbreviationOrAbbreviationKey String; if standardUnit: the key to the locale file for the abbreviation of the
63       *            unit, otherwise the abbreviation itself
64       * @param standardUnit boolean; indicates whether it is a standard unit with a definition in the locale, or a user-defined
65       *            unit
66       */
67      private MoneyPerMassUnit(final MoneyUnit moneyUnit, final MassUnit massUnit, final String nameOrNameKey,
68              final String abbreviationOrAbbreviationKey, final boolean standardUnit)
69      {
70          super(nameOrNameKey, abbreviationOrAbbreviationKey, UnitSystem.OTHER, standardMoneyPerMassUnit,
71                  moneyUnit.getScaleFactor() / massUnit.getScaleFactor(), standardUnit);
72          this.moneyUnit = moneyUnit;
73          this.massUnit = massUnit;
74      }
75  
76      /**
77       * Build a user-defined money per mass unit from a money unit and an mass unit.
78       * @param moneyUnit MoneyUnit; the unit of money for the money per mass unit, e.g., EUR
79       * @param massUnit MassUnit; the unit of mass for the money per mass unit, e.g., kg
80       * @param name String; the key to the locale file for the long name of the unit
81       * @param abbreviation String; the key to the locale file for the abbreviation of the unit
82       */
83      public MoneyPerMassUnit(final MoneyUnit moneyUnit, final MassUnit massUnit, final String name, final String abbreviation)
84      {
85          this(moneyUnit, massUnit, name, abbreviation, false);
86      }
87  
88      /**
89       * Build a MoneyPerMassUnit unit based on another MoneyPerMassUnit.
90       * @param nameOrNameKey String; if standardUnit: the key to the locale file for the long name of the unit, otherwise the
91       *            name itself
92       * @param abbreviationOrAbbreviationKey String; if standardUnit: the key to the locale file for the abbreviation of the
93       *            unit, otherwise the abbreviation itself
94       * @param referenceUnit MoneyPerMassUnit; the unit to convert to
95       * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
96       *            unit
97       * @param standardUnit boolean; indicates whether it is a standard unit with a definition in the locale, or a user-defined
98       *            unit
99       */
100     private MoneyPerMassUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey,
101             final MoneyPerMassUnit referenceUnit, final double scaleFactorToReferenceUnit, final boolean standardUnit)
102     {
103         super(nameOrNameKey, abbreviationOrAbbreviationKey, UnitSystem.OTHER, referenceUnit, scaleFactorToReferenceUnit,
104                 standardUnit);
105         this.moneyUnit = referenceUnit.getMoneyUnit();
106         this.massUnit = referenceUnit.getMassUnit();
107     }
108 
109     /**
110      * Build a user-defined MoneyPerMassUnit with a conversion factor to another MoneyPerMassUnit.
111      * @param name String; the long name of the unit
112      * @param abbreviation String; the abbreviation of the unit
113      * @param referenceUnit MoneyPerMassUnit; the unit to convert to
114      * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
115      *            unit
116      */
117     public MoneyPerMassUnit(final String name, final String abbreviation, final MoneyPerMassUnit referenceUnit,
118             final double scaleFactorToReferenceUnit)
119     {
120         this(name, abbreviation, referenceUnit, scaleFactorToReferenceUnit, false);
121     }
122 
123     /**
124      * @return moneyUnit
125      */
126     public final MoneyUnit getMoneyUnit()
127     {
128         return this.moneyUnit;
129     }
130 
131     /**
132      * @return massUnit
133      */
134     public final MassUnit getMassUnit()
135     {
136         return this.massUnit;
137     }
138 
139     /**
140      * Set the standard MoneyPerMassUnit in case the standard MoneyUnit changes, as the standard money unit is flexible.
141      * @param standardMoneyUnit MoneyUnit; the new standard money unit.
142      */
143     protected static void setStandardUnit(final MoneyUnit standardMoneyUnit)
144     {
145         try
146         {
147             standardMoneyPerMassUnit = new MoneyPerMassUnit(standardMoneyUnit, MassUnit.KILOGRAM,
148                     standardMoneyUnit.getName() + " per kilogram", standardMoneyUnit.getAbbreviation() + "/kg");
149         }
150         catch (Exception e)
151         {
152             // ignore a RunTimeException that indicates that the Unit might already exist
153             e = null;
154         }
155     }
156 
157     /** {@inheritDoc} */
158     @Override
159     public final MoneyPerMassUnit getStandardUnit()
160     {
161         return standardMoneyPerMassUnit;
162     }
163 
164     /**
165      * @return standard MoneyPerMassUnit
166      */
167     public static MoneyPerMassUnit getStandardMoneyPerMassUnit()
168     {
169         return standardMoneyPerMassUnit;
170     }
171 
172     /** {@inheritDoc} */
173     @Override
174     public final String getSICoefficientsString()
175     {
176         return "1/kg";
177     }
178 
179     /** {@inheritDoc} */
180     @Override
181     public int hashCode()
182     {
183         final int prime = 31;
184         int result = super.hashCode();
185         result = prime * result + ((this.massUnit == null) ? 0 : this.massUnit.hashCode());
186         result = prime * result + ((this.moneyUnit == null) ? 0 : this.moneyUnit.hashCode());
187         return result;
188     }
189 
190     /** {@inheritDoc} */
191     @SuppressWarnings("checkstyle:needbraces")
192     @Override
193     public boolean equals(final Object obj)
194     {
195         if (this == obj)
196             return true;
197         if (!super.equals(obj))
198             return false;
199         if (getClass() != obj.getClass())
200             return false;
201         MoneyPerMassUnit other = (MoneyPerMassUnit) obj;
202         if (this.massUnit == null)
203         {
204             if (other.massUnit != null)
205                 return false;
206         }
207         else if (!this.massUnit.equals(other.massUnit))
208             return false;
209         if (this.moneyUnit == null)
210         {
211             if (other.moneyUnit != null)
212                 return false;
213         }
214         else if (!this.moneyUnit.equals(other.moneyUnit))
215             return false;
216         return true;
217     }
218 
219 }