X-Git-Url: http://gitweb.fperrin.net/?a=blobdiff_plain;f=tim%2Fprune%2Fjpeg%2Fdrew%2FRational.java;fp=tim%2Fprune%2Fjpeg%2Fdrew%2FRational.java;h=3c83b9f5d88595fadb140d4dbb4a82532d6cf495;hb=c0387c124840c9407e040600fda88f3c3e8f6aa6;hp=0000000000000000000000000000000000000000;hpb=1ee49ae3c8ef3aa2e63eadd458531e5f8bd4f92c;p=GpsPrune.git diff --git a/tim/prune/jpeg/drew/Rational.java b/tim/prune/jpeg/drew/Rational.java new file mode 100644 index 0000000..3c83b9f --- /dev/null +++ b/tim/prune/jpeg/drew/Rational.java @@ -0,0 +1,95 @@ +package tim.prune.jpeg.drew; + +/** + * Immutable class for holding a rational number without loss of precision. + * Based on Drew Noakes' Metadata extractor at http://drewnoakes.com + */ +public class Rational +{ + /** Holds the numerator */ + private final int _numerator; + + /** Holds the denominator */ + private final int _denominator; + + /** + * Constructor + * @param inNumerator numerator of fraction (upper number) + * @param inDenominator denominator of fraction (lower number) + */ + public Rational(int inNumerator, int inDenominator) + { + // Could throw exception if denominator is zero + _numerator = inNumerator; + _denominator = inDenominator; + } + + + /** + * @return the value of the specified number as a double. + * This may involve rounding. + */ + public double doubleValue() + { + if (_denominator == 0) return 0.0; + return (double)_numerator / (double)_denominator; + } + + /** + * @return the value of the specified number as an int. + * This may involve rounding or truncation. + */ + public final int intValue() + { + if (_denominator == 0) return 0; + return _numerator / _denominator; + } + + /** + * @return the denominator. + */ + public final int getDenominator() + { + return _denominator; + } + + /** + * @return the numerator. + */ + public final int getNumerator() + { + return _numerator; + } + + /** + * Checks if this rational number is an Integer, either positive or negative + * @return true if an integer + */ + public boolean isInteger() + { + // number is integer if the denominator is 1, or if the remainder is zero + return (_denominator == 1 + || (_denominator != 0 && (_numerator % _denominator == 0))); + } + + + /** + * @return a string representation of the object of form numerator/denominator. + */ + public String toString() + { + return "" + _numerator + "/" + _denominator; + } + + + /** + * Compares two Rational instances, returning true if they are equal + * @param inOther the Rational to compare this instance to. + * @return true if instances are equal, otherwise false. + */ + public boolean equals(Rational inOther) + { + // Could also attempt to simplify fractions to lowest common denominator before compare + return _numerator == inOther._numerator && _denominator == inOther._denominator; + } +} \ No newline at end of file