KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jscience > physics > units > AddConverter


1 /*
2  * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences.
3  * Copyright (C) 2005 - 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 org.jscience.physics.units;
10
11 /**
12  * <p> This class represents an add converter. An add converter adds
13  * a constant offset to numeric values.</p>
14  * <p> Instances of this class are immutable.</p>
15  *
16  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
17  * @version 1.0, October 24, 2004
18  */

19 public final class AddConverter extends Converter {
20
21     /**
22      * Holds the offset.
23      */

24     private final double _offset;
25
26     /**
27      * Creates an add converter with the specified offset.
28      *
29      * @param offset the offset value.
30      */

31     public AddConverter(double offset) {
32         _offset = offset;
33     }
34
35     /**
36      * Returns the offset value for this add converter.
37      *
38      * @return the offset value.
39      */

40     public double getOffset() {
41         return _offset;
42     }
43
44     // Implements abstract method.
45
public Converter inverse() {
46         return new AddConverter(-_offset);
47     }
48
49     // Implements abstract method.
50
public double convert(double x) {
51         return x + _offset;
52     }
53
54     // Implements abstract method.
55
public double derivative(double x) {
56         return 1.0;
57     }
58
59     // Implements abstract method.
60
public boolean isLinear() {
61         return false;
62     }
63
64     // Overrides (optimization).
65
public Converter concatenate(Converter converter) {
66         if (converter instanceof AddConverter) {
67             // Optimization (both adding converters can be merged).
68
double offset
69                 = _offset + ((AddConverter)converter).getOffset();
70             return new AddConverter(offset);
71         } else {
72             return super.concatenate(converter);
73         }
74     }
75
76     // Overrides.
77
public boolean equals(Object JavaDoc obj) {
78         // Check equality to float precision (allows for some inaccuracies)
79
return (obj instanceof AddConverter) &&
80             (Float.floatToIntBits((float)((AddConverter)obj)._offset) ==
81              Float.floatToIntBits((float)_offset));
82     }
83
84     // Overrides.
85
public int hashCode() {
86         int h = Float.floatToIntBits((float)_offset);
87         h += ~(h << 9);
88         h ^= (h >>> 14);
89         h += (h << 4);
90         return h ^ (h >>> 10);
91     }
92
93     private static final long serialVersionUID = 1L;
94 }
Popular Tags