001package com.hfg.units; 002 003 004import java.util.HashMap; 005import java.util.List; 006import java.util.Map; 007 008import com.hfg.util.StringUtil; 009 010//------------------------------------------------------------------------------ 011/** 012 Enumeration of standard mass units. 013 <div> 014 @author J. Alex Taylor, hairyfatguy.com 015 </div> 016 */ 017//------------------------------------------------------------------------------ 018// com.hfg XML/HTML Coding Library 019// 020// This library is free software; you can redistribute it and/or 021// modify it under the terms of the GNU Lesser General Public 022// License as published by the Free Software Foundation; either 023// version 2.1 of the License, or (at your option) any later version. 024// 025// This library is distributed in the hope that it will be useful, 026// but WITHOUT ANY WARRANTY; without even the implied warranty of 027// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 028// Lesser General Public License for more details. 029// 030// You should have received a copy of the GNU Lesser General Public 031// License along with this library; if not, write to the Free Software 032// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 033// 034// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 035// jataylor@hairyfatguy.com 036//------------------------------------------------------------------------------ 037 038public class MassUnit extends Unit 039{ 040 private static final Map<String, MassUnit> sInstanceMap = new HashMap<>(6); 041 // The base SI unit of mass is the kilogram 042 public static final MassUnit gram = new MassUnit(MeasurementSystem.Metric, "gram", "g"); 043 public static final MassUnit amu = new MassUnit(MeasurementSystem.Metric, "atomic mass unit", "Da", new BaseSIUnitConverter(1.660539040E-24), new SubUnit(MassUnit.gram), new SubUnit(AmountOfSubstanceUnit.mole, -1)).addAlternateName("dalton").addAlternateName("u"); 044 public static final MassUnit dalton = MassUnit.amu; 045 046 // British Imperial units 047 public static final MassUnit ounce = new MassUnit(MeasurementSystem.BritishImperial, "ounce", "oz", new BaseSIUnitConverter(453.59237 / 16)); 048 public static final MassUnit pound = new MassUnit(MeasurementSystem.BritishImperial, "pound", "lb", new BaseSIUnitConverter(453.59237)); 049 public static final MassUnit stone = new MassUnit(MeasurementSystem.BritishImperial, "stone", "st", new BaseSIUnitConverter(6350.29318)); 050 051 //--------------------------------------------------------------------------- 052 private MassUnit(MeasurementSystem inSystem, String inName, String inAbbrev) 053 { 054 this(inSystem, inName, inAbbrev, null); 055 } 056 057 //--------------------------------------------------------------------------- 058 private MassUnit(MeasurementSystem inSystem, String inName, String inAbbrev, BaseSIUnitConverter inConversionToBaseSIUnit, SubUnit... inSubUnits) 059 { 060 this(inSystem, inName, inAbbrev, null, inConversionToBaseSIUnit, inSubUnits); 061 } 062 063 //--------------------------------------------------------------------------- 064 private MassUnit(MeasurementSystem inSystem, String inName, String inAbbrev, Integer inPow, BaseSIUnitConverter inConversionToBaseSIUnit, SubUnit[] inSubUnits) 065 { 066 super(inSystem, QuantityType.MASS, inName, inAbbrev, inPow, inConversionToBaseSIUnit, inSubUnits); 067 068 sInstanceMap.put(name(), this); 069 sInstanceMap.put(getAbbrev(), this); 070 } 071 072 //--------------------------------------------------------------------------- 073 protected MassUnit(List<SubUnit> inSubUnits) 074 { 075 super(inSubUnits); 076 077 // Sanity check that we were give subunits of type 'mass'. 078 for (SubUnit subUnit : inSubUnits) 079 { 080 if (! subUnit.getUnit().getQuantityType().equals(QuantityType.MASS)) 081 { 082 throw new RuntimeException("Cannot construct a " + getClass().getSimpleName() + " for a subunit of type " + subUnit.getUnit().getQuantityType() + "!"); 083 } 084 } 085 } 086 087 088 //--------------------------------------------------------------------------- 089 public static MassUnit valueOf(String inString) 090 { 091 return StringUtil.isSet(inString) ? sInstanceMap.get(inString) : null; 092 } 093 094 //--------------------------------------------------------------------------- 095 @Override 096 public MassUnit addAlternateName(String inValue) 097 { 098 return (MassUnit) super.addAlternateName(inValue); 099 } 100}