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