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/**
013 Enumeration of standard temperature units.
014 <div>
015 @author J. Alex Taylor, hairyfatguy.com
016 </div>
017 */
018//------------------------------------------------------------------------------
019// com.hfg XML/HTML Coding Library
020//
021// This library is free software; you can redistribute it and/or
022// modify it under the terms of the GNU Lesser General Public
023// License as published by the Free Software Foundation; either
024// version 2.1 of the License, or (at your option) any later version.
025//
026// This library is distributed in the hope that it will be useful,
027// but WITHOUT ANY WARRANTY; without even the implied warranty of
028// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
029// Lesser General Public License for more details.
030//
031// You should have received a copy of the GNU Lesser General Public
032// License along with this library; if not, write to the Free Software
033// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
034//
035// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
036// jataylor@hairyfatguy.com
037//------------------------------------------------------------------------------
038
039public class TemperatureUnit extends Unit
040{
041   private static Map<String, TemperatureUnit> sInstanceMap = new HashMap<>(3);
042
043   public static final TemperatureUnit Kelvin      = new TemperatureUnit(MeasurementSystem.SI, "Kelvin",  "K");
044
045   public static final TemperatureUnit Celcius     = new TemperatureUnit(MeasurementSystem.Metric, "Celcius",  (char) 0xB0 + "C", new BaseSIUnitConverter(273.15)).addAlternateName("C");
046
047   // British Imperial units
048   public static final TemperatureUnit Farenheit   = new TemperatureUnit(MeasurementSystem.BritishImperial, "Farenheit", (char) 0xB0 + "F", new BaseSIUnitConverter(5d / 9).setOffset(459.67)).addAlternateName("F");
049
050
051   //---------------------------------------------------------------------------
052   private TemperatureUnit(MeasurementSystem inSystem, String inName, String inAbbrev)
053   {
054      this(inSystem, inName, inAbbrev, null);
055   }
056
057   //---------------------------------------------------------------------------
058   private TemperatureUnit(MeasurementSystem inSystem, String inName, String inAbbrev, BaseSIUnitConverter inConversionToBaseSIUnit)
059   {
060      this(inSystem, inName, inAbbrev, null, inConversionToBaseSIUnit);
061   }
062
063   //---------------------------------------------------------------------------
064   private TemperatureUnit(MeasurementSystem inSystem, String inName, String inAbbrev, Integer inPow, BaseSIUnitConverter inConversionToBaseSIUnit)
065   {
066      super(inSystem, QuantityType.TEMPERATURE, inName, inAbbrev, inPow, inConversionToBaseSIUnit);
067
068      sInstanceMap.put(name(), this);
069      sInstanceMap.put(getAbbrev(), this);
070   }
071
072   //---------------------------------------------------------------------------
073   protected TemperatureUnit(List<SubUnit> inSubUnits)
074   {
075      super(inSubUnits);
076
077      // Sanity check that we were give subunits of type 'temperature'.
078      for (SubUnit subUnit : inSubUnits)
079      {
080         if (! subUnit.getUnit().getQuantityType().equals(QuantityType.TEMPERATURE))
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 TemperatureUnit valueOf(String inString)
090   {
091      return StringUtil.isSet(inString) ? sInstanceMap.get(inString) : null;
092   }
093
094   //---------------------------------------------------------------------------
095   @Override
096   public TemperatureUnit addAlternateName(String inValue)
097   {
098      super.addAlternateName(inValue);
099      return this;
100   }
101
102}