View Javadoc
1   package org.djunits.unit;
2   
3   import org.djunits.unit.unitsystem.UnitSystem;
4   
5   /**
6    * MoneyPerLengthUnit defines a unit for the cost or benefit per length, e.g. the cost of driving a kilometer.
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 MoneyPerLengthUnit extends LinearUnit<MoneyPerLengthUnit>
16  {
17      /** */
18      private static final long serialVersionUID = 20150905L;
19  
20      /** The unit of money for the money per length unit, e.g., EUR. */
21      private final MoneyUnit moneyUnit;
22  
23      /** The unit of length for the money per length unit, e.g., m. */
24      private final LengthUnit lengthUnit;
25  
26      /** Euro per meter. */
27      public static final MoneyPerLengthUnit EUR_PER_METER;
28  
29      /** Euro per kilometer. */
30      public static final MoneyPerLengthUnit EUR_PER_KILOMETER;
31  
32      /** US$ per mile. */
33      public static final MoneyPerLengthUnit USD_PER_MILE;
34  
35      /** US$ per foot. */
36      public static final MoneyPerLengthUnit USD_PER_FOOT;
37  
38      /** The standard unit to use. */
39      private static MoneyPerLengthUnit standardMoneyPerLengthUnit;
40  
41      static
42      {
43          EUR_PER_METER = new MoneyPerLengthUnit(MoneyUnit.EUR, LengthUnit.METER, "EUR per meter", "\u20AC/m", false);
44          EUR_PER_KILOMETER =
45                  new MoneyPerLengthUnit(MoneyUnit.EUR, LengthUnit.KILOMETER, "EUR per kilometer", "\u20AC/km", false);
46          USD_PER_MILE = new MoneyPerLengthUnit(MoneyUnit.USD, LengthUnit.MILE, "USD per mile", "US$/mi", false);
47          USD_PER_FOOT = new MoneyPerLengthUnit(MoneyUnit.USD, LengthUnit.FOOT, "USD per foot", "US$/ft", false);
48          standardMoneyPerLengthUnit = EUR_PER_METER;
49      }
50  
51      /**
52       * Build a money per length unit from a money unit and an length unit.
53       * @param moneyUnit MoneyUnit; the unit of money for the money per length unit, e.g., EUR
54       * @param lengthUnit LengthUnit; the unit of length for the money per length unit, e.g., meter
55       * @param nameOrNameKey String; if standardUnit: the key to the locale file for the long name of the unit, otherwise the
56       *            name itself
57       * @param abbreviationOrAbbreviationKey String; if standardUnit: the key to the locale file for the abbreviation of the
58       *            unit, otherwise the abbreviation itself
59       * @param standardUnit boolean; indicates whether it is a standard unit with a definition in the locale, or a user-defined
60       *            unit
61       */
62      private MoneyPerLengthUnit(final MoneyUnit moneyUnit, final LengthUnit lengthUnit, final String nameOrNameKey,
63              final String abbreviationOrAbbreviationKey, final boolean standardUnit)
64      {
65          super(nameOrNameKey, abbreviationOrAbbreviationKey, UnitSystem.OTHER, standardMoneyPerLengthUnit,
66                  moneyUnit.getScaleFactor() / lengthUnit.getScaleFactor(), standardUnit);
67          this.moneyUnit = moneyUnit;
68          this.lengthUnit = lengthUnit;
69      }
70  
71      /**
72       * Build a user-defined money per length unit from a money unit and an length unit.
73       * @param moneyUnit MoneyUnit; the unit of money for the money per length unit, e.g., EUR
74       * @param lengthUnit LengthUnit; the unit of length for the money per length unit, e.g., meter
75       * @param name String; the key to the locale file for the long name of the unit
76       * @param abbreviation String; the key to the locale file for the abbreviation of the unit
77       */
78      public MoneyPerLengthUnit(final MoneyUnit moneyUnit, final LengthUnit lengthUnit, final String name,
79              final String abbreviation)
80      {
81          this(moneyUnit, lengthUnit, name, abbreviation, false);
82      }
83  
84      /**
85       * Build a MoneyPerLengthUnit unit based on another MoneyPerLengthUnit.
86       * @param nameOrNameKey String; if standardUnit: the key to the locale file for the long name of the unit, otherwise the
87       *            name itself
88       * @param abbreviationOrAbbreviationKey String; if standardUnit: the key to the locale file for the abbreviation of the
89       *            unit, otherwise the abbreviation itself
90       * @param referenceUnit MoneyPerLengthUnit; the unit to convert to
91       * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
92       *            unit
93       * @param standardUnit boolean; indicates whether it is a standard unit with a definition in the locale, or a user-defined
94       *            unit
95       */
96      private MoneyPerLengthUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey,
97              final MoneyPerLengthUnit referenceUnit, final double scaleFactorToReferenceUnit, final boolean standardUnit)
98      {
99          super(nameOrNameKey, abbreviationOrAbbreviationKey, UnitSystem.OTHER, referenceUnit, scaleFactorToReferenceUnit,
100                 standardUnit);
101         this.moneyUnit = referenceUnit.getMoneyUnit();
102         this.lengthUnit = referenceUnit.getLengthUnit();
103     }
104 
105     /**
106      * Build a user-defined MoneyPerLengthUnit with a conversion factor to another MoneyPerLengthUnit.
107      * @param name String; the long name of the unit
108      * @param abbreviation String; the abbreviation of the unit
109      * @param referenceUnit MoneyPerLengthUnit; the unit to convert to
110      * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
111      *            unit
112      */
113     public MoneyPerLengthUnit(final String name, final String abbreviation, final MoneyPerLengthUnit referenceUnit,
114             final double scaleFactorToReferenceUnit)
115     {
116         this(name, abbreviation, referenceUnit, scaleFactorToReferenceUnit, false);
117     }
118 
119     /**
120      * @return moneyUnit
121      */
122     public final MoneyUnit getMoneyUnit()
123     {
124         return this.moneyUnit;
125     }
126 
127     /**
128      * @return lengthUnit
129      */
130     public final LengthUnit getLengthUnit()
131     {
132         return this.lengthUnit;
133     }
134 
135     /**
136      * Set the standard MoneyPerLengthUnit 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             standardMoneyPerLengthUnit = new MoneyPerLengthUnit(standardMoneyUnit, LengthUnit.METER,
144                     standardMoneyUnit.getName() + " per meter", standardMoneyUnit.getAbbreviation() + "/m");
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 MoneyPerLengthUnit getStandardUnit()
156     {
157         return standardMoneyPerLengthUnit;
158     }
159 
160     /**
161      * @return standard MoneyPerLengthUnit
162      */
163     public static MoneyPerLengthUnit getStandardMoneyPerLengthUnit()
164     {
165         return standardMoneyPerLengthUnit;
166     }
167 
168     /** {@inheritDoc} */
169     @Override
170     public final String getSICoefficientsString()
171     {
172         return "1/m";
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.lengthUnit == null) ? 0 : this.lengthUnit.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         MoneyPerLengthUnit other = (MoneyPerLengthUnit) obj;
198         if (this.lengthUnit == null)
199         {
200             if (other.lengthUnit != null)
201                 return false;
202         }
203         else if (!this.lengthUnit.equals(other.lengthUnit))
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 }