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 javax.measure.UnitConverter;
033
034import tec.units.ri.AbstractConverter;
035import tec.uom.lib.common.function.ValueSupplier;
036
037/**
038 * <p>
039 * This class represents a converter adding a constant offset to numeric values (<code>double</code> based).
040 * </p>
041 *
042 * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
043 * @author Werner Keil
044 * @version 0.4, Dec 28, 2015
045 */
046public final class AddConverter extends AbstractConverter implements ValueSupplier<Double> {
047
048  /**
049         * 
050         */
051  // private static final long serialVersionUID = -2981335308595652284L;
052  /**
053   * Holds the offset.
054   */
055  private double offset;
056
057  /**
058   * Creates an additive converter having the specified offset.
059   *
060   * @param offset
061   *          the offset value.
062   * @throws IllegalArgumentException
063   *           if offset is <code>0.0</code> (would result in identity converter).
064   */
065  public AddConverter(double offset) {
066    if (offset == 0.0)
067      throw new IllegalArgumentException("Would result in identity converter");
068    this.offset = offset;
069  }
070
071  /**
072   * Returns the offset value for this add converter.
073   *
074   * @return the offset value.
075   */
076  public double getOffset() {
077    return offset;
078  }
079
080  @Override
081  public UnitConverter concatenate(UnitConverter converter) {
082    if (!(converter instanceof AddConverter))
083      return super.concatenate(converter);
084    double newOffset = offset + ((AddConverter) converter).offset;
085    return newOffset == 0.0 ? IDENTITY : new AddConverter(newOffset);
086  }
087
088  @Override
089  public AddConverter inverse() {
090    return new AddConverter(-offset);
091  }
092
093  @Override
094  public double convert(double value) {
095    return value + offset;
096  }
097
098  // @Override
099  // public BigDecimal convert(BigDecimal value, MathContext ctx) throws
100  // ArithmeticException {
101  // return value.add(BigDecimal.valueOf(offset), ctx);
102  // }
103
104  @Override
105  public final String toString() {
106    return "AddConverter(" + offset + ")";
107  }
108
109  @Override
110  public boolean equals(Object obj) {
111    if (!(obj instanceof AddConverter)) {
112      return false;
113    }
114    AddConverter that = (AddConverter) obj;
115    return this.offset == that.offset;
116  }
117
118  @Override
119  public int hashCode() {
120    long bits = Double.doubleToLongBits(offset);
121    return (int) (bits ^ (bits >>> 32));
122  }
123
124  public boolean isLinear() {
125    return false;
126  }
127
128  public Double getValue() {
129    return offset;
130  }
131
132}