001/* 002 * Units of Measurement Reference Implementation 003 * Copyright (c) 2005-2017, Jean-Marie Dautelle, Werner Keil, V2COM. 004 * 005 * All rights reserved. 006 * 007 * Redistribution and use in source and binary forms, with or without modification, 008 * are permitted provided that the following conditions are met: 009 * 010 * 1. Redistributions of source code must retain the above copyright notice, 011 * this list of conditions and the following disclaimer. 012 * 013 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions 014 * and the following disclaimer in the documentation and/or other materials provided with the distribution. 015 * 016 * 3. Neither the name of JSR-363 nor the names of its contributors may be used to endorse or promote products 017 * derived from this software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 021 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 022 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 023 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 026 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 028 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package tec.units.ri.function; 031 032import tec.units.ri.AbstractConverter; 033import tec.units.ri.internal.MathUtil; 034import tec.uom.lib.common.function.ValueSupplier; 035 036/** 037 * <p> 038 * This class represents a logarithmic converter of limited precision. Such converter is typically used to create logarithmic unit. For example:[code] 039 * Unit<Dimensionless> BEL = Units.ONE.transform(new LogConverter(10).inverse()); [/code] 040 * </p> 041 * 042 * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a> 043 * @author <a href="mailto:units@catmedia.us">Werner Keil</a> 044 * @version 1.0, October 6, 2016 045 * @since 1.0 046 */ 047public final class LogConverter extends AbstractConverter implements ValueSupplier<String> { // implements Immutable<String> { 048 049 /** 050 * 051 */ 052 // private static final long serialVersionUID = -7584688290961460870L; 053 054 /** 055 * Holds the logarithmic base. 056 */ 057 private double base; 058 /** 059 * Holds the natural logarithm of the base. 060 */ 061 private double logOfBase; 062 063 /** 064 * Returns a logarithmic converter having the specified base. 065 * 066 * @param base 067 * the logarithmic base (e.g. <code>Math.E</code> for the Natural Logarithm). 068 */ 069 public LogConverter(double base) { 070 this.base = base; 071 this.logOfBase = MathUtil.log(base); 072 } 073 074 /** 075 * Returns the logarithmic base of this converter. 076 * 077 * @return the logarithmic base (e.g. <code>Math.E</code> for the Natural Logarithm). 078 */ 079 public double getBase() { 080 return base; 081 } 082 083 @Override 084 public AbstractConverter inverse() { 085 return new ExpConverter(base); 086 } 087 088 @Override 089 public final String toString() { 090 if (base == Math.E) { 091 return "ln"; 092 } else { 093 return "Log(" + base + ")"; 094 } 095 } 096 097 @Override 098 public boolean equals(Object obj) { 099 if (!(obj instanceof LogConverter)) 100 return false; 101 LogConverter that = (LogConverter) obj; 102 return this.base == that.base; 103 } 104 105 @Override 106 public int hashCode() { 107 long bits = Double.doubleToLongBits(base); 108 return (int) (bits ^ (bits >>> 32)); 109 } 110 111 @Override 112 public double convert(double amount) { 113 return MathUtil.log(amount) / logOfBase; 114 } 115 116 // @Override 117 // public BigDecimal convert(BigDecimal value, MathContext ctx) throws 118 // ArithmeticException { 119 // return BigDecimal.valueOf(convert(value.doubleValue())); // Reverts to 120 // double conversion. 121 // } 122 123 public boolean isLinear() { 124 return false; 125 } 126 127 public String getValue() { 128 return toString(); 129 } 130}