001/*
002 * Units of Measurement Reference Implementation
003 * Copyright (c) 2005-2016, 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 0.5, December 28, 2015
045 */
046public final class LogConverter extends AbstractConverter implements ValueSupplier<String> { // implements Immutable<String> {
047
048  /**
049         * 
050         */
051  // private static final long serialVersionUID = -7584688290961460870L;
052
053  /**
054   * Holds the logarithmic base.
055   */
056  private double base;
057  /**
058   * Holds the natural logarithm of the base.
059   */
060  private double logOfBase;
061
062  /**
063   * Returns a logarithmic converter having the specified base.
064   *
065   * @param base
066   *          the logarithmic base (e.g. <code>Math.E</code> for the Natural Logarithm).
067   */
068  public LogConverter(double base) {
069    this.base = base;
070    this.logOfBase = MathUtil.log(base);
071  }
072
073  /**
074   * Returns the logarithmic base of this converter.
075   *
076   * @return the logarithmic base (e.g. <code>Math.E</code> for the Natural Logarithm).
077   */
078  public double getBase() {
079    return base;
080  }
081
082  @Override
083  public AbstractConverter inverse() {
084    return new ExpConverter(base);
085  }
086
087  @Override
088  public final String toString() {
089    if (base == Math.E) {
090      return "ln";
091    } else {
092      return "Log(" + base + ")";
093    }
094  }
095
096  @Override
097  public boolean equals(Object obj) {
098    if (!(obj instanceof LogConverter))
099      return false;
100    LogConverter that = (LogConverter) obj;
101    return this.base == that.base;
102  }
103
104  @Override
105  public int hashCode() {
106    long bits = Double.doubleToLongBits(base);
107    return (int) (bits ^ (bits >>> 32));
108  }
109
110  @Override
111  public double convert(double amount) {
112    return MathUtil.log(amount) / logOfBase;
113  }
114
115  // @Override
116  // public BigDecimal convert(BigDecimal value, MathContext ctx) throws
117  // ArithmeticException {
118  // return BigDecimal.valueOf(convert(value.doubleValue())); // Reverts to
119  // double conversion.
120  // }
121
122  public boolean isLinear() {
123    return false;
124  }
125
126  public String getValue() {
127    return toString();
128  }
129}