001package com.hfg.units;
002
003
004import java.math.BigDecimal;
005import java.math.MathContext;
006import java.math.RoundingMode;
007
008public class BaseSIUnitConverter
009{
010   private double mScalingFactor;
011   private Double mOffset;
012   private MathContext mMathContext = sDefaultMathContext;
013
014   private static MathContext sDefaultMathContext = new MathContext(16, RoundingMode.HALF_UP);
015
016   //###########################################################################
017   // CONSTRUCTORS
018   //###########################################################################
019
020   //---------------------------------------------------------------------------
021   public BaseSIUnitConverter(double inScalingFactor)
022   {
023      mScalingFactor = inScalingFactor;
024   }
025
026   //###########################################################################
027   // PUBLIC METHODS
028   //###########################################################################
029
030   //---------------------------------------------------------------------------
031   public static void setDefaultMathContext(MathContext inValue)
032   {
033      sDefaultMathContext = inValue;
034   }
035
036   //---------------------------------------------------------------------------
037   public BaseSIUnitConverter setMathContext(MathContext inValue)
038   {
039      mMathContext = inValue;
040      return this;
041   }
042
043   //---------------------------------------------------------------------------
044   public BaseSIUnitConverter setOffset(Double inValue)
045   {
046      mOffset = inValue;
047      return this;
048   }
049
050   //---------------------------------------------------------------------------
051   public Double apply(Double inValue)
052   {
053      BigDecimal bigDecimalValue = allocateBigDecimal(inValue).multiply(allocateBigDecimal(mScalingFactor));
054
055      if (mOffset != null)
056      {
057         bigDecimalValue = bigDecimalValue.add(allocateBigDecimal(mOffset));
058      }
059
060      return bigDecimalValue.doubleValue();
061   }
062
063   //---------------------------------------------------------------------------
064   public Double reverse(Double inValue)
065   {
066      BigDecimal bigDecimalValue = allocateBigDecimal(inValue).divide(allocateBigDecimal(mScalingFactor), mMathContext);
067
068      if (mOffset != null)
069      {
070         bigDecimalValue = bigDecimalValue.subtract(allocateBigDecimal(mOffset));
071      }
072
073      return bigDecimalValue.doubleValue();
074   }
075
076   //###########################################################################
077   // PRIVATE METHODS
078   //###########################################################################
079
080   //---------------------------------------------------------------------------
081   private BigDecimal allocateBigDecimal(double inValue)
082   {
083      return new BigDecimal(Double.toString(inValue), mMathContext);
084   }
085}