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