001package com.hfg.units; 002 003 004import java.util.Collection; 005import java.util.Collections; 006import java.util.HashMap; 007import java.util.HashSet; 008import java.util.List; 009import java.util.Map; 010import java.util.Set; 011 012import com.hfg.util.StringUtil; 013 014//------------------------------------------------------------------------------ 015/** 016 Enumeration of standard volume units. 017 <div> 018 @author J. Alex Taylor, hairyfatguy.com 019 </div> 020 */ 021//------------------------------------------------------------------------------ 022// com.hfg XML/HTML Coding Library 023// 024// This library is free software; you can redistribute it and/or 025// modify it under the terms of the GNU Lesser General Public 026// License as published by the Free Software Foundation; either 027// version 2.1 of the License, or (at your option) any later version. 028// 029// This library is distributed in the hope that it will be useful, 030// but WITHOUT ANY WARRANTY; without even the implied warranty of 031// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 032// Lesser General Public License for more details. 033// 034// You should have received a copy of the GNU Lesser General Public 035// License along with this library; if not, write to the Free Software 036// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 037// 038// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 039// jataylor@hairyfatguy.com 040//------------------------------------------------------------------------------ 041 042public class VolumeUnit extends Unit 043{ 044 private static final Set<VolumeUnit> sInstances = new HashSet<>(10); 045 private static final Map<String, VolumeUnit> sInstanceMap = new HashMap<>(15); 046 047 public static final VolumeUnit liter = new VolumeUnit(MeasurementSystem.Metric, "liter", "L", new SubUnit(LengthUnit.meter, SI_ScalingFactor.deci, 3)).addAlternateName("l"); 048 public static final VolumeUnit milliliter = new VolumeUnit(MeasurementSystem.Metric, "milliliter", "mL", new SubUnit(liter, SI_ScalingFactor.milli)); 049 public static final VolumeUnit microliter = new VolumeUnit(MeasurementSystem.Metric, "microliter", "μL", new SubUnit(liter, SI_ScalingFactor.micro)); 050 051 // British Imperial units // The base SI unit of volume is the liter 052 public static final VolumeUnit fluid_dram = new VolumeUnit(MeasurementSystem.BritishImperial, "fluid dram", "fl dr", new BaseSIUnitConverter(0.0036967162)); 053 public static final VolumeUnit teaspoon = new VolumeUnit(MeasurementSystem.BritishImperial, "teaspoon", "tsp", new BaseSIUnitConverter(0.0049289216)); 054 public static final VolumeUnit tablespoon = new VolumeUnit(MeasurementSystem.BritishImperial, "tablespoon", "tbsp", new BaseSIUnitConverter(0.0147867648)); 055 public static final VolumeUnit fluid_ounce = new VolumeUnit(MeasurementSystem.BritishImperial, "fluid ounce", "fl oz", new BaseSIUnitConverter(0.0284130625)); 056 public static final VolumeUnit cup = new VolumeUnit(MeasurementSystem.BritishImperial, "cup", "c", new BaseSIUnitConverter(0.2365882365)); 057 public static final VolumeUnit pint = new VolumeUnit(MeasurementSystem.BritishImperial, "pint", "pt", new BaseSIUnitConverter(0.56826125)); 058 public static final VolumeUnit quart = new VolumeUnit(MeasurementSystem.BritishImperial, "quart", "qt", new BaseSIUnitConverter(1.1365225)); 059 public static final VolumeUnit gallon = new VolumeUnit(MeasurementSystem.BritishImperial, "gallon", "gal", new BaseSIUnitConverter(4.54609)); 060 public static final VolumeUnit hogshead = new VolumeUnit(MeasurementSystem.BritishImperial, "hogshead", "hhd", new BaseSIUnitConverter(238.48094239)); 061 062 //########################################################################### 063 // CONSTRUCTORS 064 //########################################################################### 065 066 //--------------------------------------------------------------------------- 067 private VolumeUnit(MeasurementSystem inSystem, SubUnit inSubUnit) 068 { 069 super(inSystem, QuantityType.VOLUME, inSubUnit); 070 } 071 072 //--------------------------------------------------------------------------- 073 private VolumeUnit(MeasurementSystem inSystem, String inName, String inAbbrev, BaseSIUnitConverter inConversionToBaseSIUnit) 074 { 075 this(inSystem, inName, inAbbrev, null, inConversionToBaseSIUnit); 076 } 077 078 //--------------------------------------------------------------------------- 079 private VolumeUnit(MeasurementSystem inSystem, String inName, String inAbbrev, Integer inPow, BaseSIUnitConverter inConversionToBaseSIUnit) 080 { 081 super(inSystem, QuantityType.VOLUME, inName, inAbbrev, inPow, inConversionToBaseSIUnit); 082 083 sInstances.add(this); 084 sInstanceMap.put(name(), this); 085 sInstanceMap.put(getAbbrev(), this); 086 } 087 088 //--------------------------------------------------------------------------- 089 private VolumeUnit(MeasurementSystem inSystem, String inName, String inAbbrev, SubUnit inSubUnit) 090 { 091 super(inSystem, QuantityType.VOLUME, inName, inAbbrev, inSubUnit); 092 093 sInstances.add(this); 094 sInstanceMap.put(name(), this); 095 sInstanceMap.put(getAbbrev(), this); 096 } 097 098 //--------------------------------------------------------------------------- 099 private VolumeUnit(MeasurementSystem inSystem, String inName, String inAbbrev, Integer inPow) 100 { 101 super(inSystem, QuantityType.VOLUME, inName, inAbbrev, inPow); 102 103 sInstances.add(this); 104 sInstanceMap.put(name(), this); 105 sInstanceMap.put(getAbbrev(), this); 106 } 107 108 //--------------------------------------------------------------------------- 109 protected VolumeUnit(List<SubUnit> inSubUnits) 110 { 111 super(inSubUnits); 112 113 // Sanity check that we were give subunits of type 'volume'. 114 for (SubUnit subUnit : inSubUnits) 115 { 116 if (! subUnit.getUnit().getQuantityType().equals(QuantityType.VOLUME)) 117 { 118 throw new RuntimeException("Cannot construct a " + getClass().getSimpleName() + " for a subunit of type " + subUnit.getUnit().getQuantityType() + "!"); 119 } 120 } 121 } 122 123 //########################################################################### 124 // PUBLIC METHODS 125 //########################################################################### 126 127 //--------------------------------------------------------------------------- 128 public static Collection<VolumeUnit> values() 129 { 130 return Collections.unmodifiableCollection(sInstances); 131 } 132 133 //--------------------------------------------------------------------------- 134 public static VolumeUnit valueOf(String inString) 135 { 136 return StringUtil.isSet(inString) ? sInstanceMap.get(inString) : null; 137 } 138 139 //--------------------------------------------------------------------------- 140 @Override 141 public VolumeUnit addAlternateName(String inValue) 142 { 143 return (VolumeUnit) super.addAlternateName(inValue); 144 } 145}