KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > measure > converters > AddConverter


1 /*
2  * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences.
3  * Copyright (C) 2006 - JScience (http://jscience.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package javax.measure.converters;
10
11
12 /**
13  * <p> This class represents a converter adding a constant offset
14  * (approximated as a <code>double</code>) to numeric values.</p>
15  *
16  * <p> Instances of this class are immutable.</p>
17  *
18  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
19  * @version 3.1, April 22, 2006
20  */

21 public final class AddConverter extends UnitConverter {
22
23     /**
24      * Holds the offset.
25      */

26     private final double _offset;
27
28     /**
29      * Creates an add converter with the specified offset.
30      *
31      * @param offset the offset value.
32      * @throws IllegalArgumentException if offset is zero (or close to zero).
33      */

34     public AddConverter(double offset) {
35         if ((float)offset == 0.0)
36             throw new IllegalArgumentException JavaDoc("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 JavaDoc
50     public UnitConverter inverse() {
51         return new AddConverter(- _offset);
52     }
53
54     @Override JavaDoc
55     public double convert(double amount) {
56         return amount + _offset;
57     }
58
59     @Override JavaDoc
60     public boolean isLinear() {
61         return false;
62     }
63
64     @Override JavaDoc
65     public boolean equals(Object JavaDoc obj) {
66         if (!(obj instanceof AddConverter)) return false;
67         AddConverter ac = (AddConverter) obj;
68         return (float)_offset == (float) ac._offset;
69     }
70
71     @Override JavaDoc
72     public int hashCode() {
73         int h = Float.floatToIntBits((float)_offset);
74         h += ~(h << 9);
75         h ^= (h >>> 14);
76         h += (h << 4);
77         return h ^ (h >>> 10);
78     }
79     
80     @Override JavaDoc
81     public UnitConverter concatenate(UnitConverter converter) {
82         if (converter instanceof AddConverter) {
83             double offset = _offset + ((AddConverter)converter)._offset;
84             return valueOf(offset);
85         } else {
86             return super.concatenate(converter);
87         }
88     }
89
90     private static UnitConverter valueOf(double offset) {
91         float asFloat = (float) offset;
92         return asFloat == 0.0f ? UnitConverter.IDENTITY : new AddConverter(offset);
93     }
94     
95     private static final long serialVersionUID = 1L;
96 }
Popular Tags