KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jscience > physics > units > LogConverter


1 /*
2  * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences.
3  * Copyright (C) 2005 - JScience (http://jscience.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package org.jscience.physics.units;
10
11 import javolution.lang.MathLib;
12
13 /**
14  * <p> This class represents a logarithmic converter. Such converter
15  * is typically used to create logarithmic unit. For example:<pre>
16  * Unit BEL = Unit.ONE.transform(new LogConverter(10).inverse());
17  * </pre></p>
18  * <p> Instances of this class are immutable.</p>
19  *
20  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
21  * @version 1.0, October 24, 2004
22  */

23 public final class LogConverter extends Converter {
24
25     /**
26      * Holds the logarithmic base.
27      */

28     private final double _base;
29
30     /**
31      * Holds the natural logarithm of the base.
32      */

33     private final double _logBase;
34
35     /**
36      * Holds the inverse of the natural logarithm of the base.
37      */

38     private final double _invLogBase;
39
40     /**
41      * Holds the inverse of this converter.
42      */

43     private final Inverse _inverse = new Inverse();
44
45     /**
46      * Creates a logarithmic converter having the specified base.
47      *
48      * @param base the logarithmic base (e.g. <code>Math.E</code> for
49      * the Natural Logarithm).
50      */

51     public LogConverter(double base) {
52         _base = base;
53         _logBase = MathLib.log(base);
54         _invLogBase = 1.0 / _logBase;
55     }
56
57     /**
58      * Returns the logarithmic base of this converter.
59      *
60      * @return the logarithmic base (e.g. <code>Math.E</code> for
61      * the Natural Logarithm).
62      */

63     public double getBase() {
64         return _base;
65     }
66
67     // Implements abstract method.
68
public Converter inverse() {
69         return _inverse;
70     }
71
72     // Implements abstract method.
73
public double convert(double x) {
74         return _invLogBase * MathLib.log(x);
75     }
76
77     // Implements abstract method.
78
public double derivative(double x) {
79         return _invLogBase / x;
80     }
81
82     // Implements abstract method.
83
public boolean isLinear() {
84         return false;
85     }
86
87     // Overrides.
88
public boolean equals(Object JavaDoc obj) {
89         // Check equality to float precision (allows for some inaccuracies)
90
return (obj instanceof LogConverter)
91                 && (Float.floatToIntBits((float) ((LogConverter) obj)._base) == Float
92                         .floatToIntBits((float) _base));
93     }
94
95     // Overrides.
96
public int hashCode() {
97         return Float.floatToIntBits((float) _base);
98     }
99
100     /**
101      * This inner class represents the inverse of the logarithmic converter
102      * (exponentiation converter).
103      */

104     private class Inverse extends Converter {
105
106         // Implements abstract method.
107
public Converter inverse() {
108             return LogConverter.this;
109         }
110
111         // Implements abstract method.
112
public double convert(double x) {
113             return MathLib.exp(_logBase * x);
114         }
115
116         // Implements abstract method.
117
public double derivative(double x) {
118             return _logBase * MathLib.exp(x);
119         }
120
121         // Implements abstract method.
122
public boolean isLinear() {
123             return false;
124         }
125
126         // Overrides.
127
public boolean equals(Object JavaDoc obj) {
128             return (obj instanceof Inverse)
129                     && LogConverter.this.equals(((Inverse) obj).inverse());
130         }
131
132         // Overrides.
133
public int hashCode() {
134             return LogConverter.this.hashCode() * 31;
135         }
136
137         private static final long serialVersionUID = 1L;
138     }
139
140     private static final long serialVersionUID = 1L;
141 }
Popular Tags