com.hfg.units.LengthUnit Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.units;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.hfg.util.StringUtil;
//------------------------------------------------------------------------------
/**
Enumeration of length measurement units.
@author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
public class LengthUnit extends Unit
{
private static final Map sInstanceMap = new HashMap<>(20);
public static final LengthUnit meter = new LengthUnit(MeasurementSystem.Metric, "meter", "m");
public static final LengthUnit centimeter = new LengthUnit(LengthUnit.meter, SI_ScalingFactor.centi);
public static final LengthUnit millimeter = new LengthUnit(LengthUnit.meter, SI_ScalingFactor.milli);
// The base SI unit of length is the meter
public static final LengthUnit angstrom = new LengthUnit(MeasurementSystem.Metric, "angstrom", "Å", new BaseSIUnitConverter(1E-10));
public static final LengthUnit light_year = new LengthUnit(MeasurementSystem.Metric, "light year", "ly", new BaseSIUnitConverter(9.4607304725808E+15)).addAlternateName("l.y.");
// British Imperial units
public static final LengthUnit inch = new LengthUnit(MeasurementSystem.BritishImperial, "inch", "in", new BaseSIUnitConverter(0.0254)).addAlternateName("''").addAlternateName("\"").setPlural("inches");
public static final LengthUnit foot = new LengthUnit(MeasurementSystem.BritishImperial, "foot", "ft", new BaseSIUnitConverter(0.3048)).addAlternateName("'").setPlural("feet");
public static final LengthUnit yard = new LengthUnit(MeasurementSystem.BritishImperial, "yard", "yd", new BaseSIUnitConverter(0.9144)).setPlural("yds");
public static final LengthUnit chain = new LengthUnit(MeasurementSystem.BritishImperial, "chain", "ch", new BaseSIUnitConverter(20.1168));
public static final LengthUnit furlong = new LengthUnit(MeasurementSystem.BritishImperial, "furlong", "fur", new BaseSIUnitConverter(201.168));
public static final LengthUnit mile = new LengthUnit(MeasurementSystem.BritishImperial, "mile", "mi", new BaseSIUnitConverter(1609.344));
public static final LengthUnit league = new LengthUnit(MeasurementSystem.BritishImperial, "league", "lea", new BaseSIUnitConverter(4828.032));
// British Imperial units - nautical
public static final LengthUnit fathom = new LengthUnit(MeasurementSystem.BritishImperial, "fathom", "ftm", new BaseSIUnitConverter(1.853184));
public static final LengthUnit nautical_mile = new LengthUnit(MeasurementSystem.BritishImperial, "nautical mile", "nautical miles", new BaseSIUnitConverter(1853.184));
// British Imperial units - survey
public static final LengthUnit link = new LengthUnit(MeasurementSystem.BritishImperial, "link", "link", new BaseSIUnitConverter(0.201168)).setPlural("links").setPluralAbbrev("links");
public static final LengthUnit rod = new LengthUnit(MeasurementSystem.BritishImperial, "rod", "rod", new BaseSIUnitConverter(5.0292)).setPlural("rods").setPluralAbbrev("rods");
//###########################################################################
// CONSTRUCTORS
//###########################################################################
//---------------------------------------------------------------------------
private LengthUnit(MeasurementSystem inSystem, String inName, String inAbbrev)
{
this(inSystem, inName, inAbbrev, null);
}
//---------------------------------------------------------------------------
private LengthUnit(MeasurementSystem inSystem, String inName, String inAbbrev, BaseSIUnitConverter inConversionToBaseSIUnit)
{
this(inSystem, inName, inAbbrev, null, inConversionToBaseSIUnit);
}
//---------------------------------------------------------------------------
private LengthUnit(MeasurementSystem inSystem, String inName, String inAbbrev, Integer inPow, BaseSIUnitConverter inConversionToBaseSIUnit)
{
super(inSystem, QuantityType.LENGTH, inName, inAbbrev, inPow, inConversionToBaseSIUnit);
init();
}
//---------------------------------------------------------------------------
private LengthUnit(LengthUnit inUnit, SI_ScalingFactor inScalingFactor)
{
super(inUnit, inScalingFactor);
init();
}
//---------------------------------------------------------------------------
protected LengthUnit(List inSubUnits)
{
super(inSubUnits);
// Sanity check that we were give subunits of type 'length'.
for (SubUnit subUnit : inSubUnits)
{
if (! subUnit.getUnit().getQuantityType().equals(QuantityType.LENGTH))
{
throw new RuntimeException("Cannot construct a " + getClass().getSimpleName() + " for a subunit of type " + subUnit.getUnit().getQuantityType() + "!");
}
}
}
//---------------------------------------------------------------------------
private void init()
{
sInstanceMap.put(name(), this);
sInstanceMap.put(getAbbrev(), this);
if (StringUtil.isSet(getPlural()))
{
sInstanceMap.put(getPlural(), this);
}
}
//###########################################################################
// PUBLIC METHODS
//###########################################################################
//---------------------------------------------------------------------------
public static LengthUnit valueOf(String inString)
{
LengthUnit unit = null;
if (StringUtil.isSet(inString))
{
String string = inString.trim();
unit = sInstanceMap.get(string);
if (null == unit)
{
// Handle SI scaled units
if (inString.endsWith(LengthUnit.meter.getAbbrev()))
{
int index = string.lastIndexOf(LengthUnit.meter.getAbbrev());
SI_ScalingFactor scalingFactor = SI_ScalingFactor.valueOf(string.substring(0, index));
if (scalingFactor != null)
{
unit = new LengthUnit(LengthUnit.meter, scalingFactor);
}
}
}
}
return unit;
}
//---------------------------------------------------------------------------
@Override
public LengthUnit addAlternateName(String inValue)
{
return (LengthUnit) super.addAlternateName(inValue);
}
//---------------------------------------------------------------------------
@Override
public LengthUnit setPlural(String inValue)
{
return (LengthUnit) super.setPlural(inValue);
}
//---------------------------------------------------------------------------
@Override
public LengthUnit setPluralAbbrev(String inValue)
{
return (LengthUnit) super.setPluralAbbrev(inValue);
}
}