001package com.hfg.units; 002 003 004import java.util.HashMap; 005import java.util.List; 006import java.util.Map; 007 008import com.hfg.util.StringUtil; 009 010//------------------------------------------------------------------------------ 011/** 012 Enumeration of standard area units. 013 <div> 014 @author J. Alex Taylor, hairyfatguy.com 015 </div> 016 */ 017//------------------------------------------------------------------------------ 018// com.hfg XML/HTML Coding Library 019// 020// This library is free software; you can redistribute it and/or 021// modify it under the terms of the GNU Lesser General Public 022// License as published by the Free Software Foundation; either 023// version 2.1 of the License, or (at your option) any later version. 024// 025// This library is distributed in the hope that it will be useful, 026// but WITHOUT ANY WARRANTY; without even the implied warranty of 027// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 028// Lesser General Public License for more details. 029// 030// You should have received a copy of the GNU Lesser General Public 031// License along with this library; if not, write to the Free Software 032// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 033// 034// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 035// jataylor@hairyfatguy.com 036//------------------------------------------------------------------------------ 037 038public class AreaUnit extends Unit 039{ 040 private static Map<String, AreaUnit> sInstanceMap = new HashMap<>(3); 041 042 public static final AreaUnit sq_meter = new AreaUnit(MeasurementSystem.SI, new SubUnit(LengthUnit.meter, 2)); 043 044 // Quasi SI unit 045 public static final AreaUnit hectare = new AreaUnit(MeasurementSystem.Metric, "hectare", "ha", new BaseSIUnitConverter(10000d)); 046 047 // British Imperial units 048 public static final AreaUnit acre = new AreaUnit(MeasurementSystem.BritishImperial, "acre", "acres", new BaseSIUnitConverter(4046.8564224)); 049 050 //########################################################################### 051 // CONSTRUCTORS 052 //########################################################################### 053 054 //--------------------------------------------------------------------------- 055 private AreaUnit(MeasurementSystem inSystem, SubUnit inSubUnit) 056 { 057 super(inSystem, QuantityType.AREA, inSubUnit); 058 } 059 060 //--------------------------------------------------------------------------- 061 private AreaUnit(MeasurementSystem inSystem, String inName, String inAbbrev, BaseSIUnitConverter inConversionToBaseSIUnit) 062 { 063 this(inSystem, inName, inAbbrev, null, inConversionToBaseSIUnit); 064 } 065 066 //--------------------------------------------------------------------------- 067 private AreaUnit(MeasurementSystem inSystem, String inName, String inAbbrev, Integer inPow, BaseSIUnitConverter inConversionToBaseSIUnit) 068 { 069 super(inSystem, QuantityType.AREA, inName, inAbbrev, inPow, inConversionToBaseSIUnit); 070 071 sInstanceMap.put(name(), this); 072 sInstanceMap.put(getAbbrev(), this); 073 } 074 075 //--------------------------------------------------------------------------- 076 protected AreaUnit(List<SubUnit> inSubUnits) 077 { 078 super(inSubUnits); 079 080 // Sanity check that we were give subunits of type 'area'. 081 for (SubUnit subUnit : inSubUnits) 082 { 083 if (! subUnit.getUnit().getQuantityType().equals(QuantityType.AREA)) 084 { 085 throw new RuntimeException("Cannot construct a " + getClass().getSimpleName() + " for a subunit of type " + subUnit.getUnit().getQuantityType() + "!"); 086 } 087 } 088 } 089 090 //########################################################################### 091 // PUBLIC METHODS 092 //########################################################################### 093 094 //--------------------------------------------------------------------------- 095 public static AreaUnit valueOf(String inString) 096 { 097 return StringUtil.isSet(inString) ? sInstanceMap.get(inString) : null; 098 } 099 100}