001package com.hfg.units;
002
003import java.util.Collection;
004import java.util.Collections;
005import java.util.HashMap;
006import java.util.HashSet;
007import java.util.List;
008import java.util.Map;
009import java.util.Set;
010
011//------------------------------------------------------------------------------
012/**
013 Enumeration of concentration types.
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
039// https://en.wikipedia.org/wiki/Concentration
040
041public class ConcentrationUnit extends Unit
042{
043   private static Set<ConcentrationUnit> sInstances = new HashSet<>(10);
044   private static final Map<String, ConcentrationUnit> sInstanceMap = new HashMap<>(10);
045
046   public static final ConcentrationUnit molarity      = new ConcentrationUnit(MeasurementSystem.SI, "molarity",    "M", new SubUnit(AmountOfSubstanceUnit.mole), new SubUnit(VolumeUnit.liter, -1));
047   public static final ConcentrationUnit molality      = new ConcentrationUnit(MeasurementSystem.SI, "molality",    "", new SubUnit(AmountOfSubstanceUnit.mole), new SubUnit(MassUnit.gram, SI_ScalingFactor.kilo, -1));
048   /**
049    * Normality is used for acid or base and is defined as the mole equivalents of H+ or OH- ions per liter.
050    */
051   public static final ConcentrationUnit normality     = new ConcentrationUnit(MeasurementSystem.SI, "normality",   "N", new SubUnit(AmountOfSubstanceUnit.mole_equivalent), new SubUnit(VolumeUnit.liter, -1));
052   /** Volume fraction is the volume of a substance divided by the volume of the total mixture */
053   public static final ConcentrationUnit volume_fraction = new ConcentrationUnit(MeasurementSystem.SI, "volume fraction", "", new SubUnit(VolumeUnit.liter), new SubUnit(VolumeUnit.liter, -1));
054   /** Percent by volume is the percent ratio of the volume to the volume of the total mixture */
055   public static final ConcentrationUnit pct_by_volume = new ConcentrationUnit(MeasurementSystem.SI, "% by volume", "%(v/v)", new SubUnit(VolumeUnit.liter, SI_ScalingFactor.centi), new SubUnit(VolumeUnit.liter, -1))
056                                                                   .addAlternateName("% v/v");
057   /** Mass fraction is the mass of a substance divided by the mass of the total mixture */
058   public static final ConcentrationUnit mass_fraction = new ConcentrationUnit(MeasurementSystem.SI, "mass fraction", "", new SubUnit(MassUnit.gram, SI_ScalingFactor.kilo), new SubUnit(MassUnit.gram, SI_ScalingFactor.kilo, -1));
059   /** Percent by weight is the percent ratio of the volume to the volume of the total mixture */
060   public static final ConcentrationUnit pct_by_weight = new ConcentrationUnit(MeasurementSystem.SI, "% by weight", "%(w/w)", new SubUnit(MassUnit.gram, SI_ScalingFactor.centi), new SubUnit(MassUnit.gram, SI_ScalingFactor.kilo, -1))
061                                                                   .addAlternateName("% w/w");
062
063
064   //###########################################################################
065   // CONSTRUCTORS
066   //###########################################################################
067
068   //---------------------------------------------------------------------------
069   private ConcentrationUnit(MeasurementSystem inSystem, String inName, String inAbbrev, SubUnit... inSubUnits)
070   {
071      this(inSystem, inName, inAbbrev, null, null, inSubUnits);
072   }
073
074   //---------------------------------------------------------------------------
075   private ConcentrationUnit(MeasurementSystem inSystem, String inName, String inAbbrev, Integer inPow, BaseSIUnitConverter inConversionToBaseSIUnit, SubUnit[] inSubUnits)
076   {
077      super(inSystem, QuantityType.CONCENTRATION, inName, inAbbrev, inPow, inConversionToBaseSIUnit, inSubUnits);
078
079      sInstances.add(this);
080      sInstanceMap.put(name(), this);
081      sInstanceMap.put(getAbbrev(), this);
082   }
083
084   //---------------------------------------------------------------------------
085   protected ConcentrationUnit(List<SubUnit> inSubUnits)
086   {
087      super(inSubUnits);
088
089      // Sanity check that we were give subunits of type 'concentration'.
090      for (SubUnit subUnit : inSubUnits)
091      {
092         if (! subUnit.getUnit().getQuantityType().equals(QuantityType.CONCENTRATION))
093         {
094            throw new RuntimeException("Cannot construct a " + getClass().getSimpleName() + " for a subunit of type " + subUnit.getUnit().getQuantityType() + "!");
095         }
096      }
097   }
098
099   //###########################################################################
100   // PUBLIC METHODS
101   //###########################################################################
102
103   //---------------------------------------------------------------------------
104   public static Collection<ConcentrationUnit> values()
105   {
106      return Collections.unmodifiableCollection(sInstances);
107   }
108
109   //---------------------------------------------------------------------------
110   @Override
111   public ConcentrationUnit addAlternateName(String inValue)
112   {
113      return (ConcentrationUnit) super.addAlternateName(inValue);
114   }
115
116}