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