All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.units.VolumeUnit Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
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 uk_teaspoon = new VolumeUnit(MeasurementSystem.BritishImperial, "imperial teaspoon", "uk tsp", new BaseSIUnitConverter(0.00591939)); public static final VolumeUnit uk_tablespoon = new VolumeUnit(MeasurementSystem.BritishImperial, "imperial tablespoon", "uk tbsp", new BaseSIUnitConverter(0.0177582)); public static final VolumeUnit uk_fluid_ounce = new VolumeUnit(MeasurementSystem.BritishImperial, "imperial fluid ounce", "uk fl oz", new BaseSIUnitConverter(0.0284130625)); public static final VolumeUnit uk_cup = new VolumeUnit(MeasurementSystem.BritishImperial, "imperial cup", "uk c", new BaseSIUnitConverter(0.284130625)); public static final VolumeUnit uk_pint = new VolumeUnit(MeasurementSystem.BritishImperial, "imperial pint", "uk pt", new BaseSIUnitConverter(0.56826125)); public static final VolumeUnit uk_quart = new VolumeUnit(MeasurementSystem.BritishImperial, "imperial quart", "uk qt", new BaseSIUnitConverter(1.1365225)); public static final VolumeUnit uk_gallon = new VolumeUnit(MeasurementSystem.BritishImperial, "imperial gallon", "uk gal", new BaseSIUnitConverter(4.54609)); public static final VolumeUnit hogshead = new VolumeUnit(MeasurementSystem.BritishImperial, "hogshead", "hhd", new BaseSIUnitConverter(238.48094239)); //American units public static final VolumeUnit fluid_dram = new VolumeUnit(MeasurementSystem.American, "fluid dram", "fl dr", new BaseSIUnitConverter(0.00369669)); public static final VolumeUnit teaspoon = new VolumeUnit(MeasurementSystem.American, "teaspoon", "tsp", new BaseSIUnitConverter(0.0049289216)); public static final VolumeUnit tablespoon = new VolumeUnit(MeasurementSystem.American, "tablespoon", "tbsp", new BaseSIUnitConverter(0.0147867648)); public static final VolumeUnit fluid_ounce = new VolumeUnit(MeasurementSystem.American, "fluid ounce", "fl oz", new BaseSIUnitConverter(0.0295735296)); public static final VolumeUnit cup = new VolumeUnit(MeasurementSystem.American, "cup", "c", new BaseSIUnitConverter(0.2365882365)); public static final VolumeUnit pint = new VolumeUnit(MeasurementSystem.American, "pint", "pt", new BaseSIUnitConverter(0.473176473)); public static final VolumeUnit quart = new VolumeUnit(MeasurementSystem.American, "quart", "qt", new BaseSIUnitConverter(0.946352946)); public static final VolumeUnit gallon = new VolumeUnit(MeasurementSystem.American, "gallon", "gal", new BaseSIUnitConverter(3.785411784)); //########################################################################### // 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); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy