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-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 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 the unit of money for the money per mass unit, e.g., EUR
59       * @param massUnit the unit of mass for the money per mass unit, e.g., kg
60       * @param nameOrNameKey if standardUnit: the key to the locale file for the long name of the unit, otherwise the name itself
61       * @param abbreviationOrAbbreviationKey if standardUnit: the key to the locale file for the abbreviation of the unit,
62       *            otherwise the abbreviation itself
63       * @param standardUnit indicates whether it is a standard unit with a definition in the locale, or a user-defined unit
64       */
65      private MoneyPerMassUnit(final MoneyUnit moneyUnit, final MassUnit massUnit, final String nameOrNameKey,
66              final String abbreviationOrAbbreviationKey, final boolean standardUnit)
67      {
68          super(nameOrNameKey, abbreviationOrAbbreviationKey, UnitSystem.OTHER, standardMoneyPerMassUnit,
69                  moneyUnit.getScaleFactor() / massUnit.getScaleFactor(), standardUnit);
70          this.moneyUnit = moneyUnit;
71          this.massUnit = massUnit;
72      }
73  
74      /**
75       * Build a user-defined money per mass unit from a money unit and an mass unit.
76       * @param moneyUnit the unit of money for the money per mass unit, e.g., EUR
77       * @param massUnit the unit of mass for the money per mass unit, e.g., kg
78       * @param name the key to the locale file for the long name of the unit
79       * @param abbreviation the key to the locale file for the abbreviation of the unit
80       */
81      public MoneyPerMassUnit(final MoneyUnit moneyUnit, final MassUnit massUnit, final String name, final String abbreviation)
82      {
83          this(moneyUnit, massUnit, name, abbreviation, false);
84      }
85  
86      /**
87       * Build a MoneyPerMassUnit unit based on another MoneyPerMassUnit.
88       * @param nameOrNameKey if standardUnit: the key to the locale file for the long name of the unit, otherwise the name itself
89       * @param abbreviationOrAbbreviationKey if standardUnit: the key to the locale file for the abbreviation of the unit,
90       *            otherwise the abbreviation itself
91       * @param referenceUnit the unit to convert to
92       * @param scaleFactorToReferenceUnit multiply a value in this unit by the factor to convert to the given reference unit
93       * @param standardUnit indicates whether it is a standard unit with a definition in the locale, or a user-defined unit
94       */
95      private MoneyPerMassUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey,
96              final MoneyPerMassUnit 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.massUnit = referenceUnit.getMassUnit();
102     }
103 
104     /**
105      * Build a user-defined MoneyPerMassUnit with a conversion factor to another MoneyPerMassUnit.
106      * @param name the long name of the unit
107      * @param abbreviation the abbreviation of the unit
108      * @param referenceUnit the unit to convert to
109      * @param scaleFactorToReferenceUnit multiply a value in this unit by the factor to convert to the given reference unit
110      */
111     public MoneyPerMassUnit(final String name, final String abbreviation, final MoneyPerMassUnit referenceUnit,
112             final double scaleFactorToReferenceUnit)
113     {
114         this(name, abbreviation, referenceUnit, scaleFactorToReferenceUnit, false);
115     }
116 
117     /**
118      * @return moneyUnit
119      */
120     public final MoneyUnit getMoneyUnit()
121     {
122         return this.moneyUnit;
123     }
124 
125     /**
126      * @return massUnit
127      */
128     public final MassUnit getMassUnit()
129     {
130         return this.massUnit;
131     }
132 
133     /**
134      * Set the standard MoneyPerMassUnit in case the standard MoneyUnit changes, as the standard money unit is flexible.
135      * @param standardMoneyUnit the new standard money unit.
136      */
137     protected static void setStandardUnit(final MoneyUnit standardMoneyUnit)
138     {
139         try
140         {
141             standardMoneyPerMassUnit = new MoneyPerMassUnit(standardMoneyUnit, MassUnit.KILOGRAM,
142                     standardMoneyUnit.getName() + " per kilogram", standardMoneyUnit.getAbbreviation() + "/kg");
143         }
144         catch (Exception e)
145         {
146             // ignore a RunTimeException that indicates that the Unit might already exist
147             e = null;
148         }
149     }
150 
151     /** {@inheritDoc} */
152     @Override
153     public final MoneyPerMassUnit getStandardUnit()
154     {
155         return standardMoneyPerMassUnit;
156     }
157 
158     /**
159      * @return standard MoneyPerMassUnit
160      */
161     public static MoneyPerMassUnit getStandardMoneyPerMassUnit()
162     {
163         return standardMoneyPerMassUnit;
164     }
165 
166     /** {@inheritDoc} */
167     @Override
168     public final String getSICoefficientsString()
169     {
170         return "1/kg";
171     }
172 
173 }