4544 |
25 Sep 08 |
nicklas |
1 |
/** |
4544 |
25 Sep 08 |
nicklas |
$Id$ |
4544 |
25 Sep 08 |
nicklas |
3 |
|
4544 |
25 Sep 08 |
nicklas |
Copyright (C) 2008 Nicklas Nordborg |
4544 |
25 Sep 08 |
nicklas |
5 |
|
4544 |
25 Sep 08 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
4544 |
25 Sep 08 |
nicklas |
Available at http://base.thep.lu.se/ |
4544 |
25 Sep 08 |
nicklas |
8 |
|
4544 |
25 Sep 08 |
nicklas |
BASE is free software; you can redistribute it and/or |
4544 |
25 Sep 08 |
nicklas |
modify it under the terms of the GNU General Public License |
4544 |
25 Sep 08 |
nicklas |
as published by the Free Software Foundation; either version 3 |
4544 |
25 Sep 08 |
nicklas |
of the License, or (at your option) any later version. |
4544 |
25 Sep 08 |
nicklas |
13 |
|
4544 |
25 Sep 08 |
nicklas |
BASE is distributed in the hope that it will be useful, |
4544 |
25 Sep 08 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
4544 |
25 Sep 08 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
4544 |
25 Sep 08 |
nicklas |
GNU General Public License for more details. |
4544 |
25 Sep 08 |
nicklas |
18 |
|
4544 |
25 Sep 08 |
nicklas |
You should have received a copy of the GNU General Public License |
4544 |
25 Sep 08 |
nicklas |
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 |
A linear unit converter is a converter implementation were all units |
4544 |
25 Sep 08 |
nicklas |
can be converted to another unit using a multiplication factor and/or an |
4544 |
25 Sep 08 |
nicklas |
offset. The formula used in this converter is: |
4544 |
25 Sep 08 |
nicklas |
<p> |
4544 |
25 Sep 08 |
nicklas |
<code>value(ref) = value(specific) * factor + offset</code> |
4544 |
25 Sep 08 |
nicklas |
33 |
|
4544 |
25 Sep 08 |
nicklas |
@author Nicklas |
4544 |
25 Sep 08 |
nicklas |
@version 2.9 |
4544 |
25 Sep 08 |
nicklas |
@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 |
Create a new unit converter. |
4544 |
25 Sep 08 |
nicklas |
@param factor The factor to use when converting values |
4544 |
25 Sep 08 |
nicklas |
@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 |
Create a unit converter that converts between two units that |
4544 |
25 Sep 08 |
nicklas |
are both related to a third "common" unit by given factors and offsets. |
4544 |
25 Sep 08 |
nicklas |
The formula used is: |
4544 |
25 Sep 08 |
nicklas |
<code>value(ref) * factor(ref) + offset(ref) == |
4544 |
25 Sep 08 |
nicklas |
value(specific) * factor(specfic) + offset(specific)</code> |
4544 |
25 Sep 08 |
nicklas |
which is equivalent to : |
4544 |
25 Sep 08 |
nicklas |
<pre class="code"> |
4544 |
25 Sep 08 |
nicklas |
factor = factor(specific) / factor(ref) |
4544 |
25 Sep 08 |
nicklas |
offset = (offset(specific) - offset(ref)) / factor(ref) |
4544 |
25 Sep 08 |
nicklas |
</pre> |
4544 |
25 Sep 08 |
nicklas |
67 |
|
4544 |
25 Sep 08 |
nicklas |
@param factor The factor of the specific unit |
4544 |
25 Sep 08 |
nicklas |
@param offset The offset of the specific unit |
4544 |
25 Sep 08 |
nicklas |
@param referenceFactor The factor of the reference unit |
4544 |
25 Sep 08 |
nicklas |
@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 |
Create a new unit converter that converts to/from a |
4544 |
25 Sep 08 |
nicklas |
given unit. The reference unit is the unit given by |
4544 |
25 Sep 08 |
nicklas |
{@link Quantity#getReferenceUnit()} for the given specific |
4544 |
25 Sep 08 |
nicklas |
unit's quantity. |
4544 |
25 Sep 08 |
nicklas |
84 |
|
4544 |
25 Sep 08 |
nicklas |
@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 |
Create a new unit converter that converts to/from |
4544 |
25 Sep 08 |
nicklas |
given specific and reference unit. |
4544 |
25 Sep 08 |
nicklas |
95 |
|
4544 |
25 Sep 08 |
nicklas |
@param specific The specific unit |
4544 |
25 Sep 08 |
nicklas |
@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 |
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 |
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 |
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 |
} |