KickJava   Java API By Example, From Geeks To Geeks.

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


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 a multiply converter. A multiply converter
13  * multiplies numeric values by a constant scale factor.</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 MultiplyConverter extends Converter {
20
21     /**
22      * Holds the scale factor.
23      */

24     private final double _factor;
25
26     /**
27      * Creates a multiply converter with the specified scale factor.
28      *
29      * @param factor the scale factor.
30      */

31     public MultiplyConverter(double factor) {
32         _factor = factor;
33     }
34
35     // Implements abstract method.
36
public Converter inverse() {
37         return new MultiplyConverter(1.0 / _factor);
38     }
39
40     // Implements abstract method.
41
public double convert(double amount) {
42         return _factor * amount;
43     }
44
45     // Implements abstract method.
46
public double derivative(double x) {
47         return _factor;
48     }
49
50     // Implements abstract method.
51
public boolean isLinear() {
52         return true;
53     }
54
55     // Overrides (optimization).
56
public Converter concatenate(Converter converter) {
57         if (converter instanceof MultiplyConverter) {
58             // Optimization (both multiply converters can be merged).
59
double factor = _factor * ((MultiplyConverter)converter)._factor;
60             return new MultiplyConverter(factor);
61         } else {
62             return super.concatenate(converter);
63         }
64     }
65
66     private static final long serialVersionUID = 1L;
67 }
Popular Tags