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