View Javadoc
1   package org.djunits.unit;
2   
3   import org.djunits.unit.unitsystem.UnitSystem;
4   
5   /**
6    * MoneyPerAreaUnit defines a unit for the cost or benefit per area, e.g. the cost of land per hectare.
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 MoneyPerAreaUnit extends LinearUnit<MoneyPerAreaUnit>
16  {
17      /** */
18      private static final long serialVersionUID = 20150905L;
19  
20      /** The unit of money for the money per area unit, e.g., EUR. */
21      private final MoneyUnit moneyUnit;
22  
23      /** The unit of area for the money per area unit, e.g., m2. */
24      private final AreaUnit areaUnit;
25  
26      /** Euro per square meter. */
27      public static final MoneyPerAreaUnit EUR_PER_SQUARE_METER;
28  
29      /** Euro per hectare. */
30      public static final MoneyPerAreaUnit EUR_PER_HECTARE;
31  
32      /** US$ per acre. */
33      public static final MoneyPerAreaUnit USD_PER_ACRE;
34  
35      /** US$ per square foot. */
36      public static final MoneyPerAreaUnit USD_PER_SQUARE_FOOT;
37  
38      /** The standard unit to use. */
39      private static MoneyPerAreaUnit standardMoneyPerAreaUnit;
40  
41      static
42      {
43          EUR_PER_SQUARE_METER = new MoneyPerAreaUnit(MoneyUnit.EUR, AreaUnit.SQUARE_METER, "EUR per sq.meter", "\u20AC/m^2");
44          EUR_PER_HECTARE = new MoneyPerAreaUnit(MoneyUnit.EUR, AreaUnit.HECTARE, "EUR per hectare", "\u20AC/ha");
45          USD_PER_ACRE = new MoneyPerAreaUnit(MoneyUnit.USD, AreaUnit.ACRE, "US$ per acre", "US$/acre");
46          USD_PER_SQUARE_FOOT = new MoneyPerAreaUnit(MoneyUnit.USD, AreaUnit.SQUARE_FOOT, "US$ per sq/foot", "US$/ft^2");
47          standardMoneyPerAreaUnit = EUR_PER_SQUARE_METER;
48      }
49  
50      /**
51       * Build a money per area unit from a money unit and an area unit.
52       * @param moneyUnit MoneyUnit; the unit of money for the money per area unit, e.g., EUR
53       * @param areaUnit AreaUnit; the unit of area for the money per area unit, e.g., m^2
54       * @param abbreviationKey String; the key to the locale file for the abbreviation of the unit
55       */
56      private MoneyPerAreaUnit(final MoneyUnit moneyUnit, final AreaUnit areaUnit, final String abbreviationKey)
57      {
58          super(abbreviationKey, UnitSystem.OTHER, standardMoneyPerAreaUnit,
59                  moneyUnit.getScaleFactor() / areaUnit.getScaleFactor());
60          this.moneyUnit = moneyUnit;
61          this.areaUnit = areaUnit;
62      }
63  
64      /**
65       * Build a user-defined money per area unit from a money unit and an area unit.
66       * @param moneyUnit MoneyUnit; the unit of money for the money per area unit, e.g., EUR
67       * @param areaUnit AreaUnit; the unit of area for the money per area unit, e.g., m^2
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 MoneyPerAreaUnit(final MoneyUnit moneyUnit, final AreaUnit areaUnit, final String name, final String abbreviation)
72      {
73          super(name, abbreviation, UnitSystem.OTHER, standardMoneyPerAreaUnit,
74                  moneyUnit.getScaleFactor() / areaUnit.getScaleFactor());
75          this.moneyUnit = moneyUnit;
76          this.areaUnit = areaUnit;
77      }
78  
79      /**
80       * Build a MoneyPerAreaUnit unit based on another MoneyPerAreaUnit.
81       * @param abbreviationKey String; the key to the locale file for the abbreviation of the unit
82       * @param referenceUnit MoneyPerAreaUnit; the unit to convert to
83       * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
84       *            unit
85       */
86      private MoneyPerAreaUnit(final String abbreviationKey, final MoneyPerAreaUnit referenceUnit,
87              final double scaleFactorToReferenceUnit)
88      {
89          super(abbreviationKey, UnitSystem.OTHER, referenceUnit, scaleFactorToReferenceUnit);
90          this.moneyUnit = referenceUnit.getMoneyUnit();
91          this.areaUnit = referenceUnit.getAreaUnit();
92      }
93  
94      /**
95       * Build a user-defined MoneyPerAreaUnit with a conversion factor to another MoneyPerAreaUnit.
96       * @param name String; the long name of the unit
97       * @param abbreviation String; the abbreviation of the unit
98       * @param referenceUnit MoneyPerAreaUnit; the unit to convert to
99       * @param scaleFactorToReferenceUnit double; multiply a value in this unit by the factor to convert to the given reference
100      *            unit
101      */
102     public MoneyPerAreaUnit(final String name, final String abbreviation, final MoneyPerAreaUnit referenceUnit,
103             final double scaleFactorToReferenceUnit)
104     {
105         super(name, abbreviation, UnitSystem.OTHER, referenceUnit, scaleFactorToReferenceUnit);
106         this.moneyUnit = referenceUnit.getMoneyUnit();
107         this.areaUnit = referenceUnit.getAreaUnit();
108     }
109 
110     /**
111      * @return moneyUnit
112      */
113     public final MoneyUnit getMoneyUnit()
114     {
115         return this.moneyUnit;
116     }
117 
118     /**
119      * @return areaUnit
120      */
121     public final AreaUnit getAreaUnit()
122     {
123         return this.areaUnit;
124     }
125 
126     /**
127      * Set the standard MoneyPerAreaUnit in case the standard MoneyUnit changes, as the standard money unit is flexible.
128      * @param standardMoneyUnit MoneyUnit; the new standard money unit.
129      */
130     protected static void setStandardUnit(final MoneyUnit standardMoneyUnit)
131     {
132         try
133         {
134             standardMoneyPerAreaUnit = new MoneyPerAreaUnit(standardMoneyUnit, AreaUnit.SQUARE_METER,
135                     standardMoneyUnit.getName() + " per square meter", standardMoneyUnit.getAbbreviation() + "/m^2");
136         }
137         catch (Exception e)
138         {
139             // ignore a RunTimeException that indicates that the Unit might already exist
140             e = null;
141         }
142     }
143 
144     /** {@inheritDoc} */
145     @Override
146     public final MoneyPerAreaUnit getStandardUnit()
147     {
148         return standardMoneyPerAreaUnit;
149     }
150 
151     /**
152      * @return standard MoneyPerAreaUnit
153      */
154     public static MoneyPerAreaUnit getStandardMoneyPerAreaUnit()
155     {
156         return standardMoneyPerAreaUnit;
157     }
158 
159     /** {@inheritDoc} */
160     @Override
161     public final String getSICoefficientsString()
162     {
163         return "1/m2";
164     }
165 
166     /** {@inheritDoc} */
167     @Override
168     public int hashCode()
169     {
170         final int prime = 31;
171         int result = super.hashCode();
172         result = prime * result + ((this.areaUnit == null) ? 0 : this.areaUnit.hashCode());
173         result = prime * result + ((this.moneyUnit == null) ? 0 : this.moneyUnit.hashCode());
174         return result;
175     }
176 
177     /** {@inheritDoc} */
178     @SuppressWarnings("checkstyle:needbraces")
179     @Override
180     public boolean equals(final Object obj)
181     {
182         if (this == obj)
183             return true;
184         if (!super.equals(obj))
185             return false;
186         if (getClass() != obj.getClass())
187             return false;
188         MoneyPerAreaUnit other = (MoneyPerAreaUnit) obj;
189         if (this.areaUnit == null)
190         {
191             if (other.areaUnit != null)
192                 return false;
193         }
194         else if (!this.areaUnit.equals(other.areaUnit))
195             return false;
196         if (this.moneyUnit == null)
197         {
198             if (other.moneyUnit != null)
199                 return false;
200         }
201         else if (!this.moneyUnit.equals(other.moneyUnit))
202             return false;
203         return true;
204     }
205 
206 }