001/*
002 * Units of Measurement Reference Implementation
003 * Copyright (c) 2005-2020, Units of Measurement project.
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.util.Objects;
033
034import javax.measure.UnitConverter;
035
036import tech.units.indriya.internal.function.calc.Calculator;
037import tech.units.indriya.spi.NumberSystem;
038import tech.uom.lib.common.function.ValueSupplier;
039
040/**
041 * <p>
042 * This class represents a converter adding a constant offset to numeric values (<code>double</code> based).
043 * </p>
044 *
045 * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
046 * @author Werner Keil
047 * @author Andi Huber
048 * @version 1.2, Jun 21, 2019
049 */
050public final class AddConverter extends AbstractConverter implements ValueSupplier<Number> {
051
052  /**
053     * 
054     */
055  private static final long serialVersionUID = -2981335308595652284L;
056  /**
057   * Holds the offset.
058   */
059  private final Number offset;
060
061  /**
062   * Creates an additive converter having the specified offset.
063   *
064   * @param offset
065   *          the offset value.
066   */
067  public AddConverter(Number offset) {
068    this.offset = Calculus.currentNumberSystem().narrow(offset);
069  }
070
071  /**
072   * Returns the offset value for this add converter.
073   *
074   * @return the offset value.
075   */
076  public Number getOffset() {
077    return offset;
078  }
079  
080  @Override
081  public boolean isIdentity() {
082    return Calculus.currentNumberSystem().isZero(offset);
083  }
084
085  @Override
086  protected boolean canReduceWith(AbstractConverter that) {
087        return that instanceof AddConverter;
088  }
089
090  @Override
091  protected AbstractConverter reduce(AbstractConverter that) {
092    NumberSystem ns = Calculus.currentNumberSystem();
093    Number newOffset = ns.add(offset, ((AddConverter)that).offset);
094    return new AddConverter(newOffset);
095  }
096  
097  @Override
098  public AddConverter inverseWhenNotIdentity() {
099    NumberSystem ns = Calculus.currentNumberSystem();
100    Number newOffset = ns.negate(offset);
101    return new AddConverter(newOffset);
102  }
103
104  @Override
105  protected Number convertWhenNotIdentity(Number value) {
106      return Calculator.of(offset)
107              .add(value)
108              .peek();
109  }
110
111  @Override
112  public String transformationLiteral() {
113    NumberSystem ns = Calculus.currentNumberSystem();
114    int signum = ns.signum(offset);
115    return String.format("x -> x %s %s", signum < 0 ? "-" : "+", ns.abs(offset));
116  }
117
118  @Override
119  public boolean equals(Object obj) {
120    if (this == obj) {
121      return true;
122    }
123    if (obj instanceof AddConverter) {
124      AddConverter other = (AddConverter) obj;
125      return Objects.equals(offset, other.offset);
126    }
127
128    return false;
129  }
130
131  @Override
132  public int hashCode() {
133    return Objects.hashCode(offset);
134  }
135
136  @Override
137  public boolean isLinear() {
138    return isIdentity();
139  }
140
141  @Override
142  public Number getValue() {
143    return offset;
144  }
145
146  @Override
147  public int compareTo(UnitConverter o) {
148    if (this == o) {
149      return 0;
150    }
151    if (o instanceof AddConverter) {
152      NumberSystem ns = Calculus.currentNumberSystem();  
153      return ns.compare(this.getValue(), ((AddConverter) o).getValue());
154    }
155    return -1;
156  }
157
158
159
160
161}