001package com.hfg.units;
002
003import java.util.HashMap;
004import java.util.List;
005import java.util.Map;
006
007import com.hfg.util.StringUtil;
008
009//------------------------------------------------------------------------------
010/**
011 Enumeration of length measurement units.
012 <div>
013  @author J. Alex Taylor, hairyfatguy.com
014 </div>
015 */
016//------------------------------------------------------------------------------
017// com.hfg XML/HTML Coding Library
018//
019// This library is free software; you can redistribute it and/or
020// modify it under the terms of the GNU Lesser General Public
021// License as published by the Free Software Foundation; either
022// version 2.1 of the License, or (at your option) any later version.
023//
024// This library is distributed in the hope that it will be useful,
025// but WITHOUT ANY WARRANTY; without even the implied warranty of
026// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
027// Lesser General Public License for more details.
028//
029// You should have received a copy of the GNU Lesser General Public
030// License along with this library; if not, write to the Free Software
031// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
032//
033// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
034// jataylor@hairyfatguy.com
035//------------------------------------------------------------------------------
036
037public class LengthUnit extends Unit
038{
039   private static final Map<String, LengthUnit> sInstanceMap = new HashMap<>(20);
040
041   public static final LengthUnit meter     = new LengthUnit(MeasurementSystem.Metric, "meter", "m");
042   public static final LengthUnit centimeter = new LengthUnit(LengthUnit.meter, SI_ScalingFactor.centi);
043   public static final LengthUnit millimeter = new LengthUnit(LengthUnit.meter, SI_ScalingFactor.milli);
044                                                                                                             // The base SI unit of length is the meter
045   public static final LengthUnit angstrom    = new LengthUnit(MeasurementSystem.Metric, "angstrom",   "Å",  new BaseSIUnitConverter(1E-10));
046   public static final LengthUnit light_year  = new LengthUnit(MeasurementSystem.Metric, "light year", "ly", new BaseSIUnitConverter(9.4607304725808E+15)).addAlternateName("l.y.");
047
048   // British Imperial units
049   public static final LengthUnit inch      = new LengthUnit(MeasurementSystem.BritishImperial, "inch",    "in",     new BaseSIUnitConverter(0.0254)).addAlternateName("''").addAlternateName("\"").setPlural("inches");
050   public static final LengthUnit foot      = new LengthUnit(MeasurementSystem.BritishImperial, "foot",    "ft",     new BaseSIUnitConverter(0.3048)).addAlternateName("'").setPlural("feet");
051   public static final LengthUnit yard      = new LengthUnit(MeasurementSystem.BritishImperial, "yard",    "yd",     new BaseSIUnitConverter(0.9144)).setPlural("yds");
052   public static final LengthUnit chain     = new LengthUnit(MeasurementSystem.BritishImperial, "chain",   "ch",    new BaseSIUnitConverter(20.1168));
053   public static final LengthUnit furlong   = new LengthUnit(MeasurementSystem.BritishImperial, "furlong", "fur",  new BaseSIUnitConverter(201.168));
054   public static final LengthUnit mile      = new LengthUnit(MeasurementSystem.BritishImperial, "mile",    "mi",  new BaseSIUnitConverter(1609.344));
055   public static final LengthUnit league    = new LengthUnit(MeasurementSystem.BritishImperial, "league",  "lea", new BaseSIUnitConverter(4828.032));
056   // British Imperial units - nautical
057   public static final LengthUnit fathom    = new LengthUnit(MeasurementSystem.BritishImperial, "fathom",  "ftm",    new BaseSIUnitConverter(1.853184));
058   public static final LengthUnit nautical_mile = new LengthUnit(MeasurementSystem.BritishImperial, "nautical mile",  "nautical miles", new BaseSIUnitConverter(1853.184));
059   // British Imperial units - survey
060   public static final LengthUnit link      = new LengthUnit(MeasurementSystem.BritishImperial, "link",  "link",   new BaseSIUnitConverter(0.201168)).setPlural("links").setPluralAbbrev("links");
061   public static final LengthUnit rod       = new LengthUnit(MeasurementSystem.BritishImperial, "rod",   "rod",    new BaseSIUnitConverter(5.0292)).setPlural("rods").setPluralAbbrev("rods");
062
063
064   //###########################################################################
065   // CONSTRUCTORS
066   //###########################################################################
067
068   //---------------------------------------------------------------------------
069   private LengthUnit(MeasurementSystem inSystem, String inName, String inAbbrev)
070   {
071      this(inSystem, inName, inAbbrev, null);
072   }
073
074   //---------------------------------------------------------------------------
075   private LengthUnit(MeasurementSystem inSystem, String inName, String inAbbrev, BaseSIUnitConverter inConversionToBaseSIUnit)
076   {
077      this(inSystem, inName, inAbbrev, null, inConversionToBaseSIUnit);
078   }
079
080   //---------------------------------------------------------------------------
081   private LengthUnit(MeasurementSystem inSystem, String inName, String inAbbrev, Integer inPow, BaseSIUnitConverter inConversionToBaseSIUnit)
082   {
083      super(inSystem, QuantityType.LENGTH, inName, inAbbrev, inPow, inConversionToBaseSIUnit);
084
085      init();
086   }
087
088   //---------------------------------------------------------------------------
089   private LengthUnit(LengthUnit inUnit, SI_ScalingFactor inScalingFactor)
090   {
091      super(inUnit, inScalingFactor);
092
093      init();
094   }
095
096   //---------------------------------------------------------------------------
097   protected LengthUnit(List<SubUnit> inSubUnits)
098   {
099      super(inSubUnits);
100
101      // Sanity check that we were give subunits of type 'length'.
102      for (SubUnit subUnit : inSubUnits)
103      {
104         if (! subUnit.getUnit().getQuantityType().equals(QuantityType.LENGTH))
105         {
106            throw new RuntimeException("Cannot construct a " + getClass().getSimpleName() + " for a subunit of type " + subUnit.getUnit().getQuantityType() + "!");
107         }
108      }
109   }
110
111   //---------------------------------------------------------------------------
112   private void init()
113   {
114      sInstanceMap.put(name(), this);
115      sInstanceMap.put(getAbbrev(), this);
116      if (StringUtil.isSet(getPlural()))
117      {
118         sInstanceMap.put(getPlural(), this);
119      }
120   }
121
122   //###########################################################################
123   // PUBLIC METHODS
124   //###########################################################################
125
126   //---------------------------------------------------------------------------
127   public static LengthUnit valueOf(String inString)
128   {
129      LengthUnit unit = null;
130      if (StringUtil.isSet(inString))
131      {
132         String string = inString.trim();
133
134         unit = sInstanceMap.get(string);
135         if (null == unit)
136         {
137            // Handle SI scaled units
138            if (inString.endsWith(LengthUnit.meter.getAbbrev()))
139            {
140               int index = string.lastIndexOf(LengthUnit.meter.getAbbrev());
141               SI_ScalingFactor scalingFactor = SI_ScalingFactor.valueOf(string.substring(0, index));
142               if (scalingFactor != null)
143               {
144                  unit = new LengthUnit(LengthUnit.meter, scalingFactor);
145               }
146            }
147         }
148      }
149
150      return unit;
151   }
152
153
154   //---------------------------------------------------------------------------
155   @Override
156   public LengthUnit addAlternateName(String inValue)
157   {
158      return (LengthUnit) super.addAlternateName(inValue);
159   }
160
161
162   //---------------------------------------------------------------------------
163   @Override
164   public LengthUnit setPlural(String inValue)
165   {
166      return (LengthUnit) super.setPlural(inValue);
167   }
168
169   //---------------------------------------------------------------------------
170   @Override
171   public LengthUnit setPluralAbbrev(String inValue)
172   {
173      return (LengthUnit) super.setPluralAbbrev(inValue);
174   }
175
176}