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-01-18 00:35:01 +0100 (Fri, 18 Jan 2019) $, @version $Revision: 324 $, 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 =
50                  new MoneyPerEnergyUnit(MoneyUnit.EUR, EnergyUnit.KILOWATT_HOUR, "EUR per kilowatthour", "\u20AC/kWh", false);
51          EUR_PER_MEGAWATTHOUR =
52                  new MoneyPerEnergyUnit(MoneyUnit.EUR, EnergyUnit.MEGAWATT_HOUR, "EUR per megawatthour", "\u20AC/MWh", false);
53          EUR_PER_MEGAJOULE =
54                  new MoneyPerEnergyUnit(MoneyUnit.EUR, EnergyUnit.MEGAJOULE, "EUR per megajoule", "\u20AC/MJ", false);
55          USD_PER_KILOWATTHOUR =
56                  new MoneyPerEnergyUnit(MoneyUnit.USD, EnergyUnit.KILOWATT_HOUR, "USD per kilowatthour", "US$/kWh", false);
57          USD_PER_MEGAWATTHOUR =
58                  new MoneyPerEnergyUnit(MoneyUnit.USD, EnergyUnit.MEGAWATT_HOUR, "USD per megawatthour", "US$/MWh", false);
59          USD_PER_MEGAJOULE = new MoneyPerEnergyUnit(MoneyUnit.USD, EnergyUnit.MEGAJOULE, "USD per megajoule", "US$/MJ", false);
60          standardMoneyPerEnergyUnit = EUR_PER_KILOWATTHOUR;
61      }
62  
63      /**
64       * Build a money per energy unit from a money unit and an energy unit.
65       * @param moneyUnit MoneyUnit; the unit of money for the money per energy unit, e.g., EUR
66       * @param energyUnit EnergyUnit; the unit of energy for the money per energy unit, e.g., kWh
67       * @param nameOrNameKey String; if standardUnit: the key to the locale file for the long name of the unit, otherwise the
68       *            name itself
69       * @param abbreviationOrAbbreviationKey String; if standardUnit: the key to the locale file for the abbreviation of the
70       *            unit, otherwise the abbreviation itself
71       * @param standardUnit boolean; indicates whether it is a standard unit with a definition in the locale, or a user-defined
72       *            unit
73       */
74      private MoneyPerEnergyUnit(final MoneyUnit moneyUnit, final EnergyUnit energyUnit, final String nameOrNameKey,
75              final String abbreviationOrAbbreviationKey, final boolean standardUnit)
76      {
77          super(nameOrNameKey, abbreviationOrAbbreviationKey, UnitSystem.OTHER, standardMoneyPerEnergyUnit,
78                  moneyUnit.getScaleFactor() / energyUnit.getScaleFactor(), standardUnit);
79          this.moneyUnit = moneyUnit;
80          this.energyUnit = energyUnit;
81      }
82  
83      /**
84       * Build a user-defined money per energy unit from a money unit and an energy unit.
85       * @param moneyUnit MoneyUnit; the unit of money for the money per energy unit, e.g., EUR
86       * @param energyUnit EnergyUnit; the unit of energy for the money per energy unit, e.g., kWh
87       * @param name String; the key to the locale file for the long name of the unit
88       * @param abbreviation String; the key to the locale file for the abbreviation of the unit
89       */
90      public MoneyPerEnergyUnit(final MoneyUnit moneyUnit, final EnergyUnit energyUnit, final String name,
91              final String abbreviation)
92      {
93          this(moneyUnit, energyUnit, name, abbreviation, false);
94      }
95  
96      /**
97       * Build a MoneyPerEnergyUnit with a conversion factor to another MoneyPerEnergyUnit.
98       * @param nameOrNameKey String; if standardUnit: the key to the locale file for the long name of the unit, otherwise the
99       *            name itself
100      * @param abbreviationOrAbbreviationKey String; if standardUnit: the key to the locale file for the abbreviation of the
101      *            unit, otherwise the abbreviation itself
102      * @param referenceUnit MoneyPerEnergyUnit; 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      * @param standardUnit boolean; indicates whether it is a standard unit with a definition in the locale, or a user-defined
106      *            unit
107      */
108     private MoneyPerEnergyUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey,
109             final MoneyPerEnergyUnit referenceUnit, final double scaleFactorToReferenceUnit, final boolean standardUnit)
110     {
111         super(nameOrNameKey, abbreviationOrAbbreviationKey, UnitSystem.OTHER, referenceUnit, scaleFactorToReferenceUnit,
112                 standardUnit);
113         this.moneyUnit = referenceUnit.getMoneyUnit();
114         this.energyUnit = referenceUnit.getEnergyUnit();
115     }
116 
117     /**
118      * Build a user-defined MoneyPerEnergyUnitit with a conversion factor to another MoneyPerEnergyUnit.
119      * @param name String; the long name of the unit
120      * @param abbreviation String; the abbreviation of the unit
121      * @param referenceUnit MoneyPerEnergyUnit; the unit to convert to
122      * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
123      *            unit
124      */
125     public MoneyPerEnergyUnit(final String name, final String abbreviation, final MoneyPerEnergyUnit referenceUnit,
126             final double scaleFactorToReferenceUnit)
127     {
128         this(name, abbreviation, referenceUnit, scaleFactorToReferenceUnit, false);
129     }
130 
131     /**
132      * @return moneyUnit
133      */
134     public final MoneyUnit getMoneyUnit()
135     {
136         return this.moneyUnit;
137     }
138 
139     /**
140      * @return energyUnit
141      */
142     public final EnergyUnit getEnergyUnit()
143     {
144         return this.energyUnit;
145     }
146 
147     /**
148      * Set the standard MoneyPerEnergyUnit in case the standard MoneyUnit changes, as the standard money unit is flexible.
149      * @param standardMoneyUnit MoneyUnit; the new standard money unit.
150      */
151     protected static void setStandardUnit(final MoneyUnit standardMoneyUnit)
152     {
153         try
154         {
155             standardMoneyPerEnergyUnit = new MoneyPerEnergyUnit(standardMoneyUnit, EnergyUnit.KILOWATT_HOUR,
156                     standardMoneyUnit.getName() + " per kilowatthour", standardMoneyUnit.getAbbreviation() + "/kWh");
157         }
158         catch (Exception e)
159         {
160             // ignore a RunTimeException that indicates that the Unit might already exist
161             e = null;
162         }
163     }
164 
165     /** {@inheritDoc} */
166     @Override
167     public final MoneyPerEnergyUnit getStandardUnit()
168     {
169         return standardMoneyPerEnergyUnit;
170     }
171 
172     /**
173      * @return standard MoneyPerEnergyUnit
174      */
175     public static MoneyPerEnergyUnit getStandardMoneyPerEnergyUnit()
176     {
177         return standardMoneyPerEnergyUnit;
178     }
179 
180     /** {@inheritDoc} */
181     @Override
182     public final String getSICoefficientsString()
183     {
184         return "s2/kgm2";
185     }
186 
187     /** {@inheritDoc} */
188     @Override
189     public int hashCode()
190     {
191         final int prime = 31;
192         int result = super.hashCode();
193         result = prime * result + ((this.energyUnit == null) ? 0 : this.energyUnit.hashCode());
194         result = prime * result + ((this.moneyUnit == null) ? 0 : this.moneyUnit.hashCode());
195         return result;
196     }
197 
198     /** {@inheritDoc} */
199     @SuppressWarnings("checkstyle:needbraces")
200     @Override
201     public boolean equals(final Object obj)
202     {
203         if (this == obj)
204             return true;
205         if (!super.equals(obj))
206             return false;
207         if (getClass() != obj.getClass())
208             return false;
209         MoneyPerEnergyUnit other = (MoneyPerEnergyUnit) obj;
210         if (this.energyUnit == null)
211         {
212             if (other.energyUnit != null)
213                 return false;
214         }
215         else if (!this.energyUnit.equals(other.energyUnit))
216             return false;
217         if (this.moneyUnit == null)
218         {
219             if (other.moneyUnit != null)
220                 return false;
221         }
222         else if (!this.moneyUnit.equals(other.moneyUnit))
223             return false;
224         return true;
225     }
226 
227 }