View Javadoc
1   package org.djunits.unit;
2   
3   import org.djunits.unit.unitsystem.UnitSystem;
4   
5   /**
6    * MoneyPerVolumeUnit defines a unit for the cost or benefit per volume, e.g. the cost of a gallon of fuel.
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 MoneyPerVolumeUnit extends LinearUnit<MoneyPerVolumeUnit>
16  {
17      /** */
18      private static final long serialVersionUID = 20150905L;
19  
20      /** The unit of money for the money per volume unit, e.g., EUR. */
21      private final MoneyUnit moneyUnit;
22  
23      /** The unit of volume for the money per volume unit, e.g., liter. */
24      private final VolumeUnit volumeUnit;
25  
26      /** Euro per cubic meter. */
27      public static final MoneyPerVolumeUnit EUR_PER_CUBIC_METER;
28  
29      /** Euro per liter. */
30      public static final MoneyPerVolumeUnit EUR_PER_LITER;
31  
32      /** US$ per gallon (US). */
33      public static final MoneyPerVolumeUnit USD_PER_GALLON_US_FLUID;
34  
35      /** US$ per ounce (US). */
36      public static final MoneyPerVolumeUnit USD_PER_OUNCE_US_FLUID;
37  
38      /** The standard unit to use. */
39      private static MoneyPerVolumeUnit standardMoneyPerVolumeUnit;
40  
41      static
42      {
43          EUR_PER_CUBIC_METER =
44                  new MoneyPerVolumeUnit(MoneyUnit.EUR, VolumeUnit.CUBIC_METER, "EUR per cubic meter", "\u20AC/m^3", false);
45          EUR_PER_LITER = new MoneyPerVolumeUnit(MoneyUnit.EUR, VolumeUnit.LITER, "EUR per liter", "\u20AC/l", false);
46          USD_PER_GALLON_US_FLUID =
47                  new MoneyPerVolumeUnit(MoneyUnit.USD, VolumeUnit.GALLON_US_FLUID, "USD per gallon (US)", "US$/gallon", false);
48          USD_PER_OUNCE_US_FLUID =
49                  new MoneyPerVolumeUnit(MoneyUnit.USD, VolumeUnit.OUNCE_US_FLUID, "USD per ounce (US)", "US$/ounce", false);
50          standardMoneyPerVolumeUnit = EUR_PER_CUBIC_METER;
51      }
52  
53      /**
54       * Build a money per volume unit from a money unit and an volume unit.
55       * @param moneyUnit the unit of money for the money per volume unit, e.g., EUR
56       * @param volumeUnit the unit of volume for the money per volume unit, e.g., m^3
57       * @param nameOrNameKey if standardUnit: the key to the locale file for the long name of the unit, otherwise the name itself
58       * @param abbreviationOrAbbreviationKey if standardUnit: the key to the locale file for the abbreviation of the unit,
59       *            otherwise the abbreviation itself
60       * @param standardUnit indicates whether it is a standard unit with a definition in the locale, or a user-defined unit
61       */
62      private MoneyPerVolumeUnit(final MoneyUnit moneyUnit, final VolumeUnit volumeUnit, final String nameOrNameKey,
63              final String abbreviationOrAbbreviationKey, final boolean standardUnit)
64      {
65          super(nameOrNameKey, abbreviationOrAbbreviationKey, UnitSystem.OTHER, standardMoneyPerVolumeUnit,
66                  moneyUnit.getScaleFactor() / volumeUnit.getScaleFactor(), standardUnit);
67          this.moneyUnit = moneyUnit;
68          this.volumeUnit = volumeUnit;
69      }
70  
71      /**
72       * Build a user-defined money per volume unit from a money unit and an volume unit.
73       * @param moneyUnit the unit of money for the money per volume unit, e.g., EUR
74       * @param volumeUnit the unit of volume for the money per volume unit, e.g., m^3
75       * @param name the key to the locale file for the long name of the unit
76       * @param abbreviation the key to the locale file for the abbreviation of the unit
77       */
78      public MoneyPerVolumeUnit(final MoneyUnit moneyUnit, final VolumeUnit volumeUnit, final String name,
79              final String abbreviation)
80      {
81          this(moneyUnit, volumeUnit, name, abbreviation, false);
82      }
83  
84      /**
85       * Build a MoneyPerVolumeUnit unit based on another MoneyPerVolumeUnit.
86       * @param nameOrNameKey if standardUnit: the key to the locale file for the long name of the unit, otherwise the name itself
87       * @param abbreviationOrAbbreviationKey if standardUnit: the key to the locale file for the abbreviation of the unit,
88       *            otherwise the abbreviation itself
89       * @param referenceUnit the unit to convert to
90       * @param scaleFactorToReferenceUnit multiply a value in this unit by the factor to convert to the given reference unit
91       * @param standardUnit indicates whether it is a standard unit with a definition in the locale, or a user-defined unit
92       */
93      private MoneyPerVolumeUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey,
94              final MoneyPerVolumeUnit referenceUnit, final double scaleFactorToReferenceUnit, final boolean standardUnit)
95      {
96          super(nameOrNameKey, abbreviationOrAbbreviationKey, UnitSystem.OTHER, referenceUnit, scaleFactorToReferenceUnit,
97                  standardUnit);
98          this.moneyUnit = referenceUnit.getMoneyUnit();
99          this.volumeUnit = referenceUnit.getVolumeUnit();
100     }
101 
102     /**
103      * Build a user-defined MoneyPerVolumeUnit with a conversion factor to another MoneyPerVolumeUnit.
104      * @param name the long name of the unit
105      * @param abbreviation the abbreviation of the unit
106      * @param referenceUnit the unit to convert to
107      * @param scaleFactorToReferenceUnit multiply a value in this unit by the factor to convert to the given reference unit
108      */
109     public MoneyPerVolumeUnit(final String name, final String abbreviation, final MoneyPerVolumeUnit referenceUnit,
110             final double scaleFactorToReferenceUnit)
111     {
112         this(name, abbreviation, referenceUnit, scaleFactorToReferenceUnit, false);
113     }
114 
115     /**
116      * @return moneyUnit
117      */
118     public final MoneyUnit getMoneyUnit()
119     {
120         return this.moneyUnit;
121     }
122 
123     /**
124      * @return volumeUnit
125      */
126     public final VolumeUnit getVolumeUnit()
127     {
128         return this.volumeUnit;
129     }
130 
131     /**
132      * Set the standard MoneyPerVolumeUnit in case the standard MoneyUnit changes, as the standard money unit is flexible.
133      * @param standardMoneyUnit the new standard money unit.
134      */
135     protected static void setStandardUnit(final MoneyUnit standardMoneyUnit)
136     {
137         try
138         {
139             standardMoneyPerVolumeUnit = new MoneyPerVolumeUnit(standardMoneyUnit, VolumeUnit.CUBIC_METER,
140                     standardMoneyUnit.getName() + " per cubic meter", standardMoneyUnit.getAbbreviation() + "/m^3");
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 MoneyPerVolumeUnit getStandardUnit()
152     {
153         return standardMoneyPerVolumeUnit;
154     }
155 
156     /**
157      * @return standard MoneyPerVolumeUnit
158      */
159     public static MoneyPerVolumeUnit getStandardMoneyPerVolumeUnit()
160     {
161         return standardMoneyPerVolumeUnit;
162     }
163 
164     /** {@inheritDoc} */
165     @Override
166     public final String getSICoefficientsString()
167     {
168         return "1/m3";
169     }
170 
171 }