All Downloads are FREE. Search and download functionalities are using the official Maven repository.

javax.measure.converter.AddConverter Maven / Gradle / Ivy

The newest version!
/*
 * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences.
 * Copyright (C) 2006 - JScience (http://jscience.org/)
 * All rights reserved.
 * 
 * Permission to use, copy, modify, and distribute this software is
 * freely granted, provided that this notice is preserved.
 */
package javax.measure.converter;


/**
 * 

This class represents a converter adding a constant offset * (approximated as a double) to numeric values.

* *

Instances of this class are immutable.

* * @author Jean-Marie Dautelle * @version 3.1, April 22, 2006 */ public final class AddConverter extends UnitConverter { /** * Holds the offset. */ private final double _offset; /** * Creates an add converter with the specified offset. * * @param offset the offset value. * @throws IllegalArgumentException if offset is zero (or close to zero). */ public AddConverter(double offset) { if ((float)offset == 0.0) throw new IllegalArgumentException("Identity converter not allowed"); _offset = offset; } /** * Returns the offset value for this add converter. * * @return the offset value. */ public double getOffset() { return _offset; } @Override public UnitConverter inverse() { return new AddConverter(- _offset); } @Override public double convert(double amount) { return amount + _offset; } @Override public boolean isLinear() { return false; } @Override public UnitConverter concatenate(UnitConverter converter) { if (converter instanceof AddConverter) { double offset = _offset + ((AddConverter)converter)._offset; return valueOf(offset); } else { return super.concatenate(converter); } } private static UnitConverter valueOf(double offset) { float asFloat = (float) offset; return asFloat == 0.0f ? UnitConverter.IDENTITY : new AddConverter(offset); } private static final long serialVersionUID = 1L; }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy