com.hfg.units.length.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.length;
import java.util.HashMap;
import java.util.Map;
import com.hfg.units.BaseSIUnitConverter;
import com.hfg.units.MeasurementSystem;
import com.hfg.units.QuantityType;
import com.hfg.units.SI_ScalingFactor;
import com.hfg.units.Unit;
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 Map sInstanceMap = new HashMap<>();
public static final LengthUnit meter = new LengthUnit(MeasurementSystem.Metric, "meter", "m");
public static final LengthUnit angstrom = new LengthUnit(MeasurementSystem.Metric, "angstrom", "Å", new BaseSIUnitConverter(1E-10));
// British Imperial units
public static final LengthUnit inch = new LengthUnit(MeasurementSystem.BritishImperial, "inch", "in", new BaseSIUnitConverter(0.0254)).setPlural("inches");
public static final LengthUnit foot = new LengthUnit(MeasurementSystem.BritishImperial, "foot", "ft", new BaseSIUnitConverter(0.3048)).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", "links", new BaseSIUnitConverter(0.201168));
public static final LengthUnit rod = new LengthUnit(MeasurementSystem.BritishImperial, "rod", "rods", new BaseSIUnitConverter(5.0292));
//###########################################################################
// 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();
}
//---------------------------------------------------------------------------
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 setPlural(String inValue)
{
return (LengthUnit) super.setPlural(inValue);
}
}