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-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-08-10 02:43:31 +0200 (Fri, 10 Aug 2018) $, @version $Revision: 286 $, 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 =
44                  new MoneyPerAreaUnit(MoneyUnit.EUR, AreaUnit.SQUARE_METER, "EUR per square meter", "\u20AC/m^2", false);
45          EUR_PER_HECTARE = new MoneyPerAreaUnit(MoneyUnit.EUR, AreaUnit.HECTARE, "EUR per hectare", "\u20AC/ha", false);
46          USD_PER_ACRE = new MoneyPerAreaUnit(MoneyUnit.USD, AreaUnit.ACRE, "USD per acre", "US$/acre", false);
47          USD_PER_SQUARE_FOOT =
48                  new MoneyPerAreaUnit(MoneyUnit.USD, AreaUnit.SQUARE_FOOT, "USD per square foot", "US$/ft^2", false);
49          standardMoneyPerAreaUnit = EUR_PER_SQUARE_METER;
50      }
51  
52      /**
53       * Build a money per area unit from a money unit and an area unit.
54       * @param moneyUnit the unit of money for the money per area unit, e.g., EUR
55       * @param areaUnit the unit of area for the money per area unit, e.g., m^2
56       * @param nameOrNameKey if standardUnit: the key to the locale file for the long name of the unit, otherwise the name itself
57       * @param abbreviationOrAbbreviationKey if standardUnit: the key to the locale file for the abbreviation of the unit,
58       *            otherwise the abbreviation itself
59       * @param standardUnit indicates whether it is a standard unit with a definition in the locale, or a user-defined unit
60       */
61      private MoneyPerAreaUnit(final MoneyUnit moneyUnit, final AreaUnit areaUnit, final String nameOrNameKey,
62              final String abbreviationOrAbbreviationKey, final boolean standardUnit)
63      {
64          super(nameOrNameKey, abbreviationOrAbbreviationKey, UnitSystem.OTHER, standardMoneyPerAreaUnit,
65                  moneyUnit.getScaleFactor() / areaUnit.getScaleFactor(), standardUnit);
66          this.moneyUnit = moneyUnit;
67          this.areaUnit = areaUnit;
68      }
69  
70      /**
71       * Build a user-defined money per area unit from a money unit and an area unit.
72       * @param moneyUnit the unit of money for the money per area unit, e.g., EUR
73       * @param areaUnit the unit of area for the money per area unit, e.g., m^2
74       * @param name the key to the locale file for the long name of the unit
75       * @param abbreviation the key to the locale file for the abbreviation of the unit
76       */
77      public MoneyPerAreaUnit(final MoneyUnit moneyUnit, final AreaUnit areaUnit, final String name, final String abbreviation)
78      {
79          this(moneyUnit, areaUnit, name, abbreviation, false);
80      }
81  
82      /**
83       * Build a MoneyPerAreaUnit unit based on another MoneyPerAreaUnit.
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 MoneyPerAreaUnit(final String nameOrNameKey, final String abbreviationOrAbbreviationKey,
92              final MoneyPerAreaUnit 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.areaUnit = referenceUnit.getAreaUnit();
98      }
99  
100     /**
101      * Build a user-defined MoneyPerAreaUnit with a conversion factor to another MoneyPerAreaUnit.
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 MoneyPerAreaUnit(final String name, final String abbreviation, final MoneyPerAreaUnit 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 areaUnit
123      */
124     public final AreaUnit getAreaUnit()
125     {
126         return this.areaUnit;
127     }
128 
129     /**
130      * Set the standard MoneyPerAreaUnit 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             standardMoneyPerAreaUnit = new MoneyPerAreaUnit(standardMoneyUnit, AreaUnit.SQUARE_METER,
138                     standardMoneyUnit.getName() + " per square meter", standardMoneyUnit.getAbbreviation() + "/m^2");
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 MoneyPerAreaUnit getStandardUnit()
150     {
151         return standardMoneyPerAreaUnit;
152     }
153 
154     /**
155      * @return standard MoneyPerAreaUnit
156      */
157     public static MoneyPerAreaUnit getStandardMoneyPerAreaUnit()
158     {
159         return standardMoneyPerAreaUnit;
160     }
161 
162     /** {@inheritDoc} */
163     @Override
164     public final String getSICoefficientsString()
165     {
166         return "1/m2";
167     }
168 
169     /** {@inheritDoc} */
170     @Override
171     public int hashCode()
172     {
173         final int prime = 31;
174         int result = super.hashCode();
175         result = prime * result + ((this.areaUnit == null) ? 0 : this.areaUnit.hashCode());
176         result = prime * result + ((this.moneyUnit == null) ? 0 : this.moneyUnit.hashCode());
177         return result;
178     }
179 
180     /** {@inheritDoc} */
181     @SuppressWarnings("checkstyle:needbraces")
182     @Override
183     public boolean equals(final Object obj)
184     {
185         if (this == obj)
186             return true;
187         if (!super.equals(obj))
188             return false;
189         if (getClass() != obj.getClass())
190             return false;
191         MoneyPerAreaUnit other = (MoneyPerAreaUnit) obj;
192         if (this.areaUnit == null)
193         {
194             if (other.areaUnit != null)
195                 return false;
196         }
197         else if (!this.areaUnit.equals(other.areaUnit))
198             return false;
199         if (this.moneyUnit == null)
200         {
201             if (other.moneyUnit != null)
202                 return false;
203         }
204         else if (!this.moneyUnit.equals(other.moneyUnit))
205             return false;
206         return true;
207     }
208 
209 }