01: /*
02: * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences.
03: * Copyright (C) 2006 - JScience (http://jscience.org/)
04: * All rights reserved.
05: *
06: * Permission to use, copy, modify, and distribute this software is
07: * freely granted, provided that this notice is preserved.
08: */
09: package javax.measure.converter;
10:
11: /**
12: * <p> This class represents a converter adding a constant offset
13: * (approximated as a <code>double</code>) to numeric values.</p>
14: *
15: * <p> Instances of this class are immutable.</p>
16: *
17: * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
18: * @version 3.1, April 22, 2006
19: */
20: public final class AddConverter extends UnitConverter {
21:
22: /**
23: * Holds the offset.
24: */
25: private final double _offset;
26:
27: /**
28: * Creates an add converter with the specified offset.
29: *
30: * @param offset the offset value.
31: * @throws IllegalArgumentException if offset is zero (or close to zero).
32: */
33: public AddConverter(double offset) {
34: if ((float) offset == 0.0)
35: throw new IllegalArgumentException(
36: "Identity converter not allowed");
37: _offset = offset;
38: }
39:
40: /**
41: * Returns the offset value for this add converter.
42: *
43: * @return the offset value.
44: */
45: public double getOffset() {
46: return _offset;
47: }
48:
49: @Override
50: public UnitConverter inverse() {
51: return new AddConverter(-_offset);
52: }
53:
54: @Override
55: public double convert(double amount) {
56: return amount + _offset;
57: }
58:
59: @Override
60: public boolean isLinear() {
61: return false;
62: }
63:
64: @Override
65: public UnitConverter concatenate(UnitConverter converter) {
66: if (converter instanceof AddConverter) {
67: double offset = _offset
68: + ((AddConverter) converter)._offset;
69: return valueOf(offset);
70: } else {
71: return super .concatenate(converter);
72: }
73: }
74:
75: private static UnitConverter valueOf(double offset) {
76: float asFloat = (float) offset;
77: return asFloat == 0.0f ? UnitConverter.IDENTITY
78: : new AddConverter(offset);
79: }
80:
81: private static final long serialVersionUID = 1L;
82: }
|