src/core/net/sf/basedb/util/units/LinearUnitConverter.java

Code
Comments
Other
Rev Date Author Line
4544 25 Sep 08 nicklas 1 /**
4544 25 Sep 08 nicklas 2   $Id$
4544 25 Sep 08 nicklas 3
4544 25 Sep 08 nicklas 4   Copyright (C) 2008 Nicklas Nordborg
4544 25 Sep 08 nicklas 5
4544 25 Sep 08 nicklas 6   This file is part of BASE - BioArray Software Environment.
4544 25 Sep 08 nicklas 7   Available at http://base.thep.lu.se/
4544 25 Sep 08 nicklas 8
4544 25 Sep 08 nicklas 9   BASE is free software; you can redistribute it and/or
4544 25 Sep 08 nicklas 10   modify it under the terms of the GNU General Public License
4544 25 Sep 08 nicklas 11   as published by the Free Software Foundation; either version 3
4544 25 Sep 08 nicklas 12   of the License, or (at your option) any later version.
4544 25 Sep 08 nicklas 13
4544 25 Sep 08 nicklas 14   BASE is distributed in the hope that it will be useful,
4544 25 Sep 08 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
4544 25 Sep 08 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4544 25 Sep 08 nicklas 17   GNU General Public License for more details.
4544 25 Sep 08 nicklas 18
4544 25 Sep 08 nicklas 19   You should have received a copy of the GNU General Public License
4544 25 Sep 08 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
4544 25 Sep 08 nicklas 21 */
4544 25 Sep 08 nicklas 22 package net.sf.basedb.util.units;
4544 25 Sep 08 nicklas 23
4544 25 Sep 08 nicklas 24 import net.sf.basedb.core.Quantity;
4544 25 Sep 08 nicklas 25 import net.sf.basedb.core.Unit;
4544 25 Sep 08 nicklas 26
4544 25 Sep 08 nicklas 27 /**
4544 25 Sep 08 nicklas 28   A linear unit converter is a converter implementation were all units
4544 25 Sep 08 nicklas 29   can be converted to another unit using a multiplication factor and/or an
4544 25 Sep 08 nicklas 30   offset. The formula used in this converter is:
4544 25 Sep 08 nicklas 31   <p>
4544 25 Sep 08 nicklas 32   <code>value(ref) = value(specific) * factor + offset</code>
4544 25 Sep 08 nicklas 33   
4544 25 Sep 08 nicklas 34   @author Nicklas
4544 25 Sep 08 nicklas 35   @version 2.9
4544 25 Sep 08 nicklas 36   @base.modified $Date$
4544 25 Sep 08 nicklas 37 */
4544 25 Sep 08 nicklas 38 public class LinearUnitConverter
4544 25 Sep 08 nicklas 39   implements UnitConverter
4544 25 Sep 08 nicklas 40 {
4544 25 Sep 08 nicklas 41
4544 25 Sep 08 nicklas 42   private final double factor;
4544 25 Sep 08 nicklas 43   private final double offset;
4544 25 Sep 08 nicklas 44   
4544 25 Sep 08 nicklas 45   /**
4544 25 Sep 08 nicklas 46     Create a new unit converter.
4544 25 Sep 08 nicklas 47     @param factor The factor to use when converting values
4544 25 Sep 08 nicklas 48     @param offset The offset to use when converting values
4544 25 Sep 08 nicklas 49   */
4544 25 Sep 08 nicklas 50   public LinearUnitConverter(double factor, double offset)
4544 25 Sep 08 nicklas 51   {
4544 25 Sep 08 nicklas 52     this.factor = factor;
4544 25 Sep 08 nicklas 53     this.offset = offset;
4544 25 Sep 08 nicklas 54   }
4544 25 Sep 08 nicklas 55   
4544 25 Sep 08 nicklas 56   /**
4544 25 Sep 08 nicklas 57     Create a unit converter that converts between two units that 
4544 25 Sep 08 nicklas 58     are both related to a third "common" unit by given factors and offsets.
4544 25 Sep 08 nicklas 59     The formula used is:
4544 25 Sep 08 nicklas 60     <code>value(ref) * factor(ref) + offset(ref) == 
4544 25 Sep 08 nicklas 61     value(specific) * factor(specfic) + offset(specific)</code>
4544 25 Sep 08 nicklas 62     which is equivalent to :
4544 25 Sep 08 nicklas 63     <pre class="code">
4544 25 Sep 08 nicklas 64 factor = factor(specific) / factor(ref)
4544 25 Sep 08 nicklas 65 offset = (offset(specific) - offset(ref)) / factor(ref)
4544 25 Sep 08 nicklas 66 </pre>
4544 25 Sep 08 nicklas 67
4544 25 Sep 08 nicklas 68     @param factor The factor of the specific unit
4544 25 Sep 08 nicklas 69     @param offset The offset of the specific unit
4544 25 Sep 08 nicklas 70     @param referenceFactor The factor of the reference unit
4544 25 Sep 08 nicklas 71     @param referenceOffset The offset of the specific unit
4544 25 Sep 08 nicklas 72    */
4544 25 Sep 08 nicklas 73   public LinearUnitConverter(double factor, double offset, 
4544 25 Sep 08 nicklas 74     double referenceFactor, double referenceOffset)
4544 25 Sep 08 nicklas 75   {
4544 25 Sep 08 nicklas 76     this(factor / referenceFactor, (offset - referenceOffset) / referenceFactor);
4544 25 Sep 08 nicklas 77   }
4544 25 Sep 08 nicklas 78   
4544 25 Sep 08 nicklas 79   /**
4544 25 Sep 08 nicklas 80     Create a new unit converter that converts to/from a
4544 25 Sep 08 nicklas 81     given unit. The reference unit is the unit given by 
4544 25 Sep 08 nicklas 82     {@link Quantity#getReferenceUnit()} for the given specific 
4544 25 Sep 08 nicklas 83     unit's quantity.
4544 25 Sep 08 nicklas 84     
4544 25 Sep 08 nicklas 85     @param unit The specific unit
4544 25 Sep 08 nicklas 86   */
4544 25 Sep 08 nicklas 87   public LinearUnitConverter(Unit unit)
4544 25 Sep 08 nicklas 88   {
4544 25 Sep 08 nicklas 89     this(unit.getReferenceFactor(), unit.getReferenceOffset());
4544 25 Sep 08 nicklas 90   }
4544 25 Sep 08 nicklas 91   
4544 25 Sep 08 nicklas 92   /**
4544 25 Sep 08 nicklas 93     Create a new unit converter that converts to/from 
4544 25 Sep 08 nicklas 94     given specific and reference unit.
4544 25 Sep 08 nicklas 95     
4544 25 Sep 08 nicklas 96     @param specific The specific unit
4544 25 Sep 08 nicklas 97     @param reference The reference unit
4544 25 Sep 08 nicklas 98   */
4544 25 Sep 08 nicklas 99   public LinearUnitConverter(Unit specific, Unit reference)
4544 25 Sep 08 nicklas 100   {
4544 25 Sep 08 nicklas 101     this(specific.getReferenceFactor(), specific.getReferenceOffset(),
4544 25 Sep 08 nicklas 102       reference.getReferenceFactor(), reference.getReferenceOffset());
4544 25 Sep 08 nicklas 103   }
4544 25 Sep 08 nicklas 104
4544 25 Sep 08 nicklas 105   /**
4544 25 Sep 08 nicklas 106     From the UnitConverter interface
4544 25 Sep 08 nicklas 107     --------------------------------
4544 25 Sep 08 nicklas 108   */
4544 25 Sep 08 nicklas 109   /**
4544 25 Sep 08 nicklas 110     value(ref) = value(specific) * factor + offset
4544 25 Sep 08 nicklas 111   */
6127 14 Sep 12 nicklas 112   @Override
4544 25 Sep 08 nicklas 113   public double convertToReferenceUnit(double specific)
4544 25 Sep 08 nicklas 114   {
4544 25 Sep 08 nicklas 115     return specific * factor + offset;
4544 25 Sep 08 nicklas 116   }
4544 25 Sep 08 nicklas 117   /**
4544 25 Sep 08 nicklas 118     value(specific) = (value(ref) - offset) / factor
4544 25 Sep 08 nicklas 119   */
6127 14 Sep 12 nicklas 120   @Override
4544 25 Sep 08 nicklas 121   public double convertToSpecificUnit(double ref)
4544 25 Sep 08 nicklas 122   {
4544 25 Sep 08 nicklas 123     return (ref - offset)/factor;
4544 25 Sep 08 nicklas 124   }
4544 25 Sep 08 nicklas 125   // -----------------------------------
4544 25 Sep 08 nicklas 126   
4544 25 Sep 08 nicklas 127 }