| 1 | /* |
| 2 | * Copyright 2002-2019 Drew Noakes and contributors |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | * More information about this project is available at: |
| 17 | * |
| 18 | * https://drewnoakes.com/code/exif/ |
| 19 | * https://github.com/drewnoakes/metadata-extractor |
| 20 | */ |
| 21 | |
| 22 | package com.drew.lang; |
| 23 | |
| 24 | import com.drew.lang.annotations.NotNull; |
| 25 | import com.drew.lang.annotations.Nullable; |
| 26 | |
| 27 | import java.io.Serializable; |
| 28 | |
| 29 | /** |
| 30 | * Immutable class for holding a rational number without loss of precision. Provides |
| 31 | * a familiar representation via {@link Rational#toString} in form <code>numerator/denominator</code>. |
| 32 | * |
| 33 | * Note that any value with a numerator of zero will be treated as zero, even if the |
| 34 | * denominator is also zero. |
| 35 | * |
| 36 | * @author Drew Noakes https://drewnoakes.com |
| 37 | */ |
| 38 | @SuppressWarnings("WeakerAccess") |
| 39 | public class Rational extends java.lang.Number implements Comparable<Rational>, Serializable |
| 40 | { |
| 41 | private static final long serialVersionUID = 510688928138848770L; |
| 42 | |
| 43 | /** Holds the numerator. */ |
| 44 | private final long _numerator; |
| 45 | |
| 46 | /** Holds the denominator. */ |
| 47 | private final long _denominator; |
| 48 | |
| 49 | /** |
| 50 | * Creates a new instance of Rational. Rational objects are immutable, so |
| 51 | * once you've set your numerator and denominator values here, you're stuck |
| 52 | * with them! |
| 53 | */ |
| 54 | public Rational(long numerator, long denominator) |
| 55 | { |
| 56 | _numerator = numerator; |
| 57 | _denominator = denominator; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Returns the value of the specified number as a <code>double</code>. |
| 62 | * This may involve rounding. |
| 63 | * |
| 64 | * @return the numeric value represented by this object after conversion |
| 65 | * to type <code>double</code>. |
| 66 | */ |
| 67 | @Override |
| 68 | public double doubleValue() |
| 69 | { |
| 70 | return _numerator == 0 |
| 71 | ? 0.0 |
| 72 | : (double) _numerator / (double) _denominator; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns the value of the specified number as a <code>float</code>. |
| 77 | * This may involve rounding. |
| 78 | * |
| 79 | * @return the numeric value represented by this object after conversion |
| 80 | * to type <code>float</code>. |
| 81 | */ |
| 82 | @Override |
| 83 | public float floatValue() |
| 84 | { |
| 85 | return _numerator == 0 |
| 86 | ? 0.0f |
| 87 | : (float) _numerator / (float) _denominator; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Returns the value of the specified number as a <code>byte</code>. |
| 92 | * This may involve rounding or truncation. This implementation simply |
| 93 | * casts the result of {@link Rational#doubleValue} to <code>byte</code>. |
| 94 | * |
| 95 | * @return the numeric value represented by this object after conversion |
| 96 | * to type <code>byte</code>. |
| 97 | */ |
| 98 | @Override |
| 99 | public final byte byteValue() |
| 100 | { |
| 101 | return (byte) doubleValue(); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Returns the value of the specified number as an <code>int</code>. |
| 106 | * This may involve rounding or truncation. This implementation simply |
| 107 | * casts the result of {@link Rational#doubleValue} to <code>int</code>. |
| 108 | * |
| 109 | * @return the numeric value represented by this object after conversion |
| 110 | * to type <code>int</code>. |
| 111 | */ |
| 112 | @Override |
| 113 | public final int intValue() |
| 114 | { |
| 115 | return (int) doubleValue(); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Returns the value of the specified number as a <code>long</code>. |
| 120 | * This may involve rounding or truncation. This implementation simply |
| 121 | * casts the result of {@link Rational#doubleValue} to <code>long</code>. |
| 122 | * |
| 123 | * @return the numeric value represented by this object after conversion |
| 124 | * to type <code>long</code>. |
| 125 | */ |
| 126 | @Override |
| 127 | public final long longValue() |
| 128 | { |
| 129 | return (long) doubleValue(); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Returns the value of the specified number as a <code>short</code>. |
| 134 | * This may involve rounding or truncation. This implementation simply |
| 135 | * casts the result of {@link Rational#doubleValue} to <code>short</code>. |
| 136 | * |
| 137 | * @return the numeric value represented by this object after conversion |
| 138 | * to type <code>short</code>. |
| 139 | */ |
| 140 | @Override |
| 141 | public final short shortValue() |
| 142 | { |
| 143 | return (short) doubleValue(); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | /** Returns the denominator. */ |
| 148 | public final long getDenominator() |
| 149 | { |
| 150 | return this._denominator; |
| 151 | } |
| 152 | |
| 153 | /** Returns the numerator. */ |
| 154 | public final long getNumerator() |
| 155 | { |
| 156 | return this._numerator; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Returns the reciprocal value of this object as a new Rational. |
| 161 | * |
| 162 | * @return the reciprocal in a new object |
| 163 | */ |
| 164 | @NotNull |
| 165 | public Rational getReciprocal() |
| 166 | { |
| 167 | return new Rational(this._denominator, this._numerator); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Returns the absolute value of this object as a new Rational. |
| 172 | * |
| 173 | * @return the absolute value in a new object |
| 174 | */ |
| 175 | public Rational getAbsolute() |
| 176 | { |
| 177 | return new Rational(Math.abs(this._numerator), Math.abs(this._denominator)); |
| 178 | } |
| 179 | |
| 180 | /** Checks if this {@link Rational} number is an Integer, either positive or negative. */ |
| 181 | public boolean isInteger() |
| 182 | { |
| 183 | return _denominator == 1 || |
| 184 | (_denominator != 0 && (_numerator % _denominator == 0)) || |
| 185 | (_denominator == 0 && _numerator == 0); |
| 186 | } |
| 187 | |
| 188 | /** Checks if either the numerator or denominator are zero. */ |
| 189 | public boolean isZero() |
| 190 | { |
| 191 | return _numerator == 0 || _denominator == 0; |
| 192 | } |
| 193 | |
| 194 | /** True if the value is non-zero and numerator and denominator are either both positive or both negative. */ |
| 195 | public boolean isPositive() |
| 196 | { |
| 197 | return !isZero() && (_numerator > 0 == _denominator > 0); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Returns a string representation of the object of form <code>numerator/denominator</code>. |
| 202 | * |
| 203 | * @return a string representation of the object. |
| 204 | */ |
| 205 | @Override |
| 206 | @NotNull |
| 207 | public String toString() |
| 208 | { |
| 209 | return _numerator + "/" + _denominator; |
| 210 | } |
| 211 | |
| 212 | /** Returns the simplest representation of this {@link Rational}'s value possible. */ |
| 213 | @NotNull |
| 214 | public String toSimpleString(boolean allowDecimal) |
| 215 | { |
| 216 | if (_denominator == 0 && _numerator != 0) { |
| 217 | return toString(); |
| 218 | } else if (isInteger()) { |
| 219 | return Integer.toString(intValue()); |
| 220 | } else { |
| 221 | Rational simplifiedInstance = getSimplifiedInstance(); |
| 222 | if (allowDecimal) { |
| 223 | String doubleString = Double.toString(simplifiedInstance.doubleValue()); |
| 224 | if (doubleString.length() < 5) { |
| 225 | return doubleString; |
| 226 | } |
| 227 | } |
| 228 | return simplifiedInstance.toString(); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Compares two {@link Rational} instances, returning true if they are mathematically |
| 234 | * equivalent (in consistence with {@link Rational#equals(Object)} method). |
| 235 | * |
| 236 | * @param that the {@link Rational} to compare this instance to. |
| 237 | * @return the value {@code 0} if this {@link Rational} is |
| 238 | * equal to the argument {@link Rational} mathematically; a value less |
| 239 | * than {@code 0} if this {@link Rational} is less |
| 240 | * than the argument {@link Rational}; and a value greater |
| 241 | * than {@code 0} if this {@link Rational} is greater than the argument |
| 242 | * {@link Rational}. |
| 243 | */ |
| 244 | public int compareTo(@NotNull Rational that) { |
| 245 | return Double.compare(this.doubleValue(), that.doubleValue()); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Indicates whether this instance and <code>other</code> are numerically equal, |
| 250 | * even if their representations differ. |
| 251 | * |
| 252 | * For example, 1/2 is equal to 10/20 by this method. |
| 253 | * Similarly, 1/0 is equal to 100/0 by this method. |
| 254 | * To test equal representations, use EqualsExact. |
| 255 | * |
| 256 | * @param other The rational value to compare with |
| 257 | */ |
| 258 | public boolean equals(Rational other) { |
| 259 | return other.doubleValue() == doubleValue(); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Indicates whether this instance and <code>other</code> have identical |
| 264 | * Numerator and Denominator. |
| 265 | * <p> |
| 266 | * For example, 1/2 is not equal to 10/20 by this method. |
| 267 | * Similarly, 1/0 is not equal to 100/0 by this method. |
| 268 | * To test numerically equivalence, use Equals(Rational).</p> |
| 269 | * |
| 270 | * @param other The rational value to compare with |
| 271 | */ |
| 272 | public boolean equalsExact(Rational other) { |
| 273 | return getDenominator() == other.getDenominator() && getNumerator() == other.getNumerator(); |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Compares two {@link Rational} instances, returning true if they are mathematically |
| 278 | * equivalent. |
| 279 | * |
| 280 | * @param obj the {@link Rational} to compare this instance to. |
| 281 | * @return true if instances are mathematically equivalent, otherwise false. Will also |
| 282 | * return false if <code>obj</code> is not an instance of {@link Rational}. |
| 283 | */ |
| 284 | @Override |
| 285 | public boolean equals(@Nullable Object obj) |
| 286 | { |
| 287 | if (!(obj instanceof Rational)) |
| 288 | return false; |
| 289 | Rational that = (Rational) obj; |
| 290 | return this.doubleValue() == that.doubleValue(); |
| 291 | } |
| 292 | |
| 293 | @Override |
| 294 | public int hashCode() |
| 295 | { |
| 296 | return (23 * (int)_denominator) + (int)_numerator; |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * <p> |
| 301 | * Simplifies the representation of this {@link Rational} number.</p> |
| 302 | * <p> |
| 303 | * For example, 5/10 simplifies to 1/2 because both Numerator |
| 304 | * and Denominator share a common factor of 5.</p> |
| 305 | * <p> |
| 306 | * Uses the Euclidean Algorithm to find the greatest common divisor.</p> |
| 307 | * |
| 308 | * @return A simplified instance if one exists, otherwise a copy of the original value. |
| 309 | */ |
| 310 | @NotNull |
| 311 | public Rational getSimplifiedInstance() |
| 312 | { |
| 313 | long n = _numerator; |
| 314 | long d = _denominator; |
| 315 | |
| 316 | if (d < 0) { |
| 317 | n = -n; |
| 318 | d = -d; |
| 319 | } |
| 320 | |
| 321 | long gcd = GCD(n, d); |
| 322 | |
| 323 | return new Rational(n / gcd, d / gcd); |
| 324 | } |
| 325 | |
| 326 | private static long GCD(long a, long b) |
| 327 | { |
| 328 | if (a < 0) |
| 329 | a = -a; |
| 330 | if (b < 0) |
| 331 | b = -b; |
| 332 | |
| 333 | while (a != 0 && b != 0) |
| 334 | { |
| 335 | if (a > b) |
| 336 | a %= b; |
| 337 | else |
| 338 | b %= a; |
| 339 | } |
| 340 | |
| 341 | return a == 0 ? b : a; |
| 342 | } |
| 343 | } |