com.hfg.units.VolumeUnit 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.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.hfg.util.StringUtil;
//------------------------------------------------------------------------------
/**
Enumeration of standard volume 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 VolumeUnit extends Unit
{
private static final Set sInstances = new HashSet<>(10);
private static final Map sInstanceMap = new HashMap<>(15);
public static final VolumeUnit liter = new VolumeUnit(MeasurementSystem.Metric, "liter", "L", new SubUnit(LengthUnit.meter, SI_ScalingFactor.deci, 3)).addAlternateName("l");
public static final VolumeUnit milliliter = new VolumeUnit(MeasurementSystem.Metric, "milliliter", "mL", new SubUnit(liter, SI_ScalingFactor.milli));
public static final VolumeUnit microliter = new VolumeUnit(MeasurementSystem.Metric, "microliter", "μL", new SubUnit(liter, SI_ScalingFactor.micro));
// British Imperial units // The base SI unit of volume is the liter
public static final VolumeUnit fluid_dram = new VolumeUnit(MeasurementSystem.BritishImperial, "fluid dram", "fl dr", new BaseSIUnitConverter(0.0036967162));
public static final VolumeUnit teaspoon = new VolumeUnit(MeasurementSystem.BritishImperial, "teaspoon", "tsp", new BaseSIUnitConverter(0.0049289216));
public static final VolumeUnit tablespoon = new VolumeUnit(MeasurementSystem.BritishImperial, "tablespoon", "tbsp", new BaseSIUnitConverter(0.0147867648));
public static final VolumeUnit fluid_ounce = new VolumeUnit(MeasurementSystem.BritishImperial, "fluid ounce", "fl oz", new BaseSIUnitConverter(0.0284130625));
public static final VolumeUnit cup = new VolumeUnit(MeasurementSystem.BritishImperial, "cup", "c", new BaseSIUnitConverter(0.2365882365));
public static final VolumeUnit pint = new VolumeUnit(MeasurementSystem.BritishImperial, "pint", "pt", new BaseSIUnitConverter(0.56826125));
public static final VolumeUnit quart = new VolumeUnit(MeasurementSystem.BritishImperial, "quart", "qt", new BaseSIUnitConverter(1.1365225));
public static final VolumeUnit gallon = new VolumeUnit(MeasurementSystem.BritishImperial, "gallon", "gal", new BaseSIUnitConverter(4.54609));
public static final VolumeUnit hogshead = new VolumeUnit(MeasurementSystem.BritishImperial, "hogshead", "hhd", new BaseSIUnitConverter(238.48094239));
//###########################################################################
// CONSTRUCTORS
//###########################################################################
//---------------------------------------------------------------------------
private VolumeUnit(MeasurementSystem inSystem, SubUnit inSubUnit)
{
super(inSystem, QuantityType.VOLUME, inSubUnit);
}
//---------------------------------------------------------------------------
private VolumeUnit(MeasurementSystem inSystem, String inName, String inAbbrev, BaseSIUnitConverter inConversionToBaseSIUnit)
{
this(inSystem, inName, inAbbrev, null, inConversionToBaseSIUnit);
}
//---------------------------------------------------------------------------
private VolumeUnit(MeasurementSystem inSystem, String inName, String inAbbrev, Integer inPow, BaseSIUnitConverter inConversionToBaseSIUnit)
{
super(inSystem, QuantityType.VOLUME, inName, inAbbrev, inPow, inConversionToBaseSIUnit);
sInstances.add(this);
sInstanceMap.put(name(), this);
sInstanceMap.put(getAbbrev(), this);
}
//---------------------------------------------------------------------------
private VolumeUnit(MeasurementSystem inSystem, String inName, String inAbbrev, SubUnit inSubUnit)
{
super(inSystem, QuantityType.VOLUME, inName, inAbbrev, inSubUnit);
sInstances.add(this);
sInstanceMap.put(name(), this);
sInstanceMap.put(getAbbrev(), this);
}
//---------------------------------------------------------------------------
private VolumeUnit(MeasurementSystem inSystem, String inName, String inAbbrev, Integer inPow)
{
super(inSystem, QuantityType.VOLUME, inName, inAbbrev, inPow);
sInstances.add(this);
sInstanceMap.put(name(), this);
sInstanceMap.put(getAbbrev(), this);
}
//---------------------------------------------------------------------------
protected VolumeUnit(List inSubUnits)
{
super(inSubUnits);
// Sanity check that we were give subunits of type 'volume'.
for (SubUnit subUnit : inSubUnits)
{
if (! subUnit.getUnit().getQuantityType().equals(QuantityType.VOLUME))
{
throw new RuntimeException("Cannot construct a " + getClass().getSimpleName() + " for a subunit of type " + subUnit.getUnit().getQuantityType() + "!");
}
}
}
//###########################################################################
// PUBLIC METHODS
//###########################################################################
//---------------------------------------------------------------------------
public static Collection values()
{
return Collections.unmodifiableCollection(sInstances);
}
//---------------------------------------------------------------------------
public static VolumeUnit valueOf(String inString)
{
return StringUtil.isSet(inString) ? sInstanceMap.get(inString) : null;
}
//---------------------------------------------------------------------------
@Override
public VolumeUnit addAlternateName(String inValue)
{
return (VolumeUnit) super.addAlternateName(inValue);
}
}