KickJava   Java API By Example, From Geeks To Geeks.

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


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  * <p> This class represents a converter multiplying numeric values by a
13  * constant scaling factor (approximated as a <code>double</code>).
14  * For exact scaling conversions {@link RationalConverter} is preferred.</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 MultiplyConverter extends UnitConverter {
22
23     /**
24      * Holds the scale factor.
25      */

26     private final double _factor;
27
28     /**
29      * Creates a multiply converter with the specified scale factor.
30      *
31      * @param factor the scale factor.
32      * @throws IllegalArgumentException if offset is one (or close to one).
33      */

34     public MultiplyConverter(double factor) {
35         if ((float)factor == 1.0)
36             throw new IllegalArgumentException JavaDoc("Identity converter not allowed");
37         _factor = factor;
38     }
39
40     /**
41      * Returns the scale factor.
42      *
43      * @return the scale factor.
44      */

45     public double getFactor() {
46         return _factor;
47     }
48
49     @Override JavaDoc
50     public UnitConverter inverse() {
51         return new MultiplyConverter(1.0 / _factor);
52     }
53
54     @Override JavaDoc
55     public double convert(double amount) {
56         return _factor * amount;
57     }
58
59     @Override JavaDoc
60     public boolean isLinear() {
61         return true;
62     }
63
64     @Override JavaDoc
65     public boolean equals(Object JavaDoc obj) {
66         if (!(obj instanceof MultiplyConverter))
67             return false;
68         MultiplyConverter mc = (MultiplyConverter) obj;
69         return (float) _factor == (float) mc._factor;
70     }
71
72     @Override JavaDoc
73     public int hashCode() {
74         int h = Float.floatToIntBits((float) _factor);
75         h += ~(h << 9);
76         h ^= (h >>> 14);
77         h += (h << 4);
78         return h ^ (h >>> 10);
79     }
80
81     @Override JavaDoc
82     public UnitConverter concatenate(UnitConverter converter) {
83         if (converter instanceof MultiplyConverter) {
84             double factor = _factor * ((MultiplyConverter) converter)._factor;
85             return valueOf(factor);
86         } else if (converter instanceof RationalConverter) {
87             double factor = _factor
88                     * ((RationalConverter) converter).getDividend()
89                     / ((RationalConverter) converter).getDivisor();
90             return valueOf(factor);
91         } else {
92             return super.concatenate(converter);
93         }
94     }
95
96     private static UnitConverter valueOf(double factor) {
97         float asFloat = (float) factor;
98         return asFloat == 1.0f ? UnitConverter.IDENTITY
99                 : new MultiplyConverter(factor);
100     }
101
102     private static final long serialVersionUID = 1L;
103 }
Popular Tags