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