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