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