001/*
002 * Units of Measurement Reference Implementation
003 * Copyright (c) 2005-2018, Jean-Marie Dautelle, Werner Keil, Otavio Santana.
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-385, Indriya nor the names of their 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 tech.units.indriya.function;
031
032import java.math.BigDecimal;
033import java.math.MathContext;
034import java.util.Objects;
035
036import javax.measure.UnitConverter;
037
038import tech.units.indriya.AbstractConverter;
039import tech.uom.lib.common.function.ValueSupplier;
040
041/**
042 * <p>
043 * This class represents a converter adding a constant offset to numeric values (<code>double</code> based).
044 * </p>
045 *
046 * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
047 * @author Werner Keil
048 * @version 1.0, Oct 10, 2016
049 */
050public final class AddConverter extends AbstractConverter implements ValueSupplier<Double> {
051
052  /**
053     * 
054     */
055  private static final long serialVersionUID = -2981335308595652284L;
056  /**
057   * Holds the offset.
058   */
059  private final double offset;
060
061  /**
062   * Creates an additive converter having the specified offset.
063   *
064   * @param offset
065   *          the offset value.
066   */
067  public AddConverter(double offset) {
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 boolean isIdentity() {
082    return offset == 0.0;
083  }
084
085  @Override
086  protected boolean isSimpleCompositionWith(AbstractConverter that) {
087        return that instanceof AddConverter;
088  }
089
090  @Override
091  protected AbstractConverter simpleCompose(AbstractConverter that) {
092    return new AddConverter(offset + ((AddConverter)that).offset);
093  }
094  
095  @Override
096  public AddConverter inverseWhenNotIdentity() {
097    return new AddConverter(-offset);
098  }
099
100  @Override
101  public double convertWhenNotIdentity(double value) {
102    return value + offset;
103  }
104
105  @Override
106  public BigDecimal convertWhenNotIdentity(BigDecimal value, MathContext ctx) throws ArithmeticException {
107    return value.add(BigDecimal.valueOf(offset), ctx);
108  }
109
110  @Override
111  public String transformationLiteral() {
112    return String.format("x -> x %s %s", offset < 0 ? "-" : "+", Math.abs(offset));
113  }
114
115  @Override
116  public boolean equals(Object obj) {
117    if (this == obj) {
118      return true;
119    }
120    if (obj instanceof AddConverter) {
121      AddConverter other = (AddConverter) obj;
122      return Objects.equals(offset, other.offset);
123    }
124
125    return false;
126  }
127
128  @Override
129  public int hashCode() {
130    return Objects.hashCode(offset);
131  }
132
133  @Override
134  public boolean isLinear() {
135    return isIdentity();
136  }
137
138  public Double getValue() {
139    return offset;
140  }
141
142  @Override
143  public int compareTo(UnitConverter o) {
144    if (this == o) {
145      return 0;
146    }
147    if (o instanceof AddConverter) {
148      return getValue().compareTo(((AddConverter) o).getValue());
149    }
150    return -1;
151  }
152
153
154}