| 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.converter; |
| 10 | |
| 11 | /** |
| 12 | * <p> This class represents a converter multiplying numeric values by an |
| 13 | * exact scaling factor (represented as the quotient of two |
| 14 | * <code>long</code> numbers).</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 RationalConverter extends UnitConverter { |
| 22 | |
| 23 | /** |
| 24 | * Holds the converter dividend. |
| 25 | */ |
| 26 | private final long _dividend; |
| 27 | |
| 28 | /** |
| 29 | * Holds the converter divisor (always positive). |
| 30 | */ |
| 31 | private final long _divisor; |
| 32 | |
| 33 | /** |
| 34 | * Creates a rational converter with the specified dividend and |
| 35 | * divisor. |
| 36 | * |
| 37 | * @param dividend the dividend. |
| 38 | * @param divisor the positive divisor. |
| 39 | * @throws IllegalArgumentException if <code>divisor < 0</code> |
| 40 | * @throws IllegalArgumentException if <code>dividend == divisor</code> |
| 41 | */ |
| 42 | public RationalConverter(long dividend, long divisor) { |
| 43 | if (divisor < 0) |
| 44 | throw new IllegalArgumentException("Negative divisor"); |
| 45 | if (dividend == divisor) |
| 46 | throw new IllegalArgumentException("Identity converter not allowed"); |
| 47 | _dividend = dividend; |
| 48 | _divisor = divisor; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Returns the dividend for this rational converter. |
| 53 | * |
| 54 | * @return this converter dividend. |
| 55 | */ |
| 56 | public long getDividend() { |
| 57 | return _dividend; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Returns the positive divisor for this rational converter. |
| 62 | * |
| 63 | * @return this converter divisor. |
| 64 | */ |
| 65 | public long getDivisor() { |
| 66 | return _divisor; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public UnitConverter inverse() { |
| 71 | return _dividend < 0 ? new RationalConverter(-_divisor, -_dividend) |
| 72 | : new RationalConverter(_divisor, _dividend); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public double convert(double amount) { |
| 77 | return amount * _dividend / _divisor; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public boolean isLinear() { |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public UnitConverter concatenate(UnitConverter converter) { |
| 87 | if (converter instanceof RationalConverter) { |
| 88 | RationalConverter that = (RationalConverter) converter; |
| 89 | long dividendLong = this._dividend * that._dividend; |
| 90 | long divisorLong = this._divisor * that._divisor; |
| 91 | double dividendDouble = ((double)this._dividend) * that._dividend; |
| 92 | double divisorDouble = ((double)this._divisor) * that._divisor; |
| 93 | if ((dividendLong != dividendDouble) || |
| 94 | (divisorLong != divisorDouble)) { // Long overflows. |
| 95 | return new MultiplyConverter(dividendDouble / divisorDouble); |
| 96 | } |
| 97 | long gcd = gcd(dividendLong, divisorLong); |
| 98 | return RationalConverter.valueOf(dividendLong / gcd, divisorLong / gcd); |
| 99 | } else if (converter instanceof MultiplyConverter) { |
| 100 | return converter.concatenate(this); |
| 101 | } else { |
| 102 | return super.concatenate(converter); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | private static UnitConverter valueOf(long dividend, long divisor) { |
| 107 | return (dividend == 1L) && (divisor == 1L) ? UnitConverter.IDENTITY |
| 108 | : new RationalConverter(dividend, divisor); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Returns the greatest common divisor (Euclid's algorithm). |
| 113 | * |
| 114 | * @param m the first number. |
| 115 | * @param nn the second number. |
| 116 | * @return the greatest common divisor. |
| 117 | */ |
| 118 | private static long gcd(long m, long n) { |
| 119 | if (n == 0L) { |
| 120 | return m; |
| 121 | } else { |
| 122 | return gcd(n, m % n); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | private static final long serialVersionUID = 1L; |
| 127 | } |