001/* 002 * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences. 003 * Copyright (C) 2006 - JScience (http://jscience.org/) 004 * All rights reserved. 005 * 006 * Permission to use, copy, modify, and distribute this software is 007 * freely granted, provided that this notice is preserved. 008 */ 009package javax.measure.converter; 010 011/** 012 * <p> This class represents a logarithmic converter. Such converter 013 * is typically used to create logarithmic unit. For example:[code] 014 * Unit<Dimensionless> BEL = Unit.ONE.transform(new LogConverter(10).inverse()); 015 * [/code]</p> 016 * 017 * <p> Instances of this class are immutable.</p> 018 * 019 * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a> 020 * @version 3.1, April 22, 2006 021 */ 022public final class LogConverter extends UnitConverter { 023 024 /** 025 * Holds the logarithmic base. 026 */ 027 private final double _base; 028 029 /** 030 * Holds the natural logarithm of the base. 031 */ 032 private final double _logBase; 033 034 /** 035 * Holds the inverse of the natural logarithm of the base. 036 */ 037 private final double _invLogBase; 038 039 /** 040 * Holds the inverse of this converter. 041 */ 042 private final Inverse _inverse = new Inverse(); 043 044 /** 045 * Creates a logarithmic converter having the specified base. 046 * 047 * @param base the logarithmic base (e.g. <code>Math.E</code> for 048 * the Natural Logarithm). 049 */ 050 public LogConverter(double base) { 051 _base = base; 052 _logBase = Math.log(base); 053 _invLogBase = 1.0 / _logBase; 054 } 055 056 /** 057 * Returns the logarithmic base of this converter. 058 * 059 * @return the logarithmic base (e.g. <code>Math.E</code> for 060 * the Natural Logarithm). 061 */ 062 public double getBase() { 063 return _base; 064 } 065 066 @Override 067 public UnitConverter inverse() { 068 return _inverse; 069 } 070 071 @Override 072 public double convert(double amount) { 073 return _invLogBase * Math.log(amount); 074 } 075 076 @Override 077 public boolean isLinear() { 078 return false; 079 } 080 081 /** 082 * This inner class represents the inverse of the logarithmic converter 083 * (exponentiation converter). 084 */ 085 private class Inverse extends UnitConverter { 086 087 088 @Override 089 public UnitConverter inverse() { 090 return LogConverter.this; 091 } 092 093 @Override 094 public double convert(double amount) { 095 return Math.exp(_logBase * amount); 096 } 097 098 @Override 099 public boolean isLinear() { 100 return false; 101 } 102 103 private static final long serialVersionUID = 1L; 104 } 105 106 private static final long serialVersionUID = 1L; 107}