Package gov.nih.mipav.model.file
Class MetadataExtractor.Rational
- java.lang.Object
-
- java.lang.Number
-
- gov.nih.mipav.model.file.MetadataExtractor.Rational
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Comparable<MetadataExtractor.Rational>
- Enclosing class:
- MetadataExtractor
public class MetadataExtractor.Rational extends java.lang.Number implements java.lang.Comparable<MetadataExtractor.Rational>, java.io.Serializable
Immutable class for holding a rational number without loss of precision. Provides a familiar representation viatoString()
in formnumerator/denominator
. Note that any value with a numerator of zero will be treated as zero, even if the denominator is also zero.- Author:
- Drew Noakes https://drewnoakes.com
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private long
_denominator
Holds the denominator.private long
_numerator
Holds the numerator.private static long
serialVersionUID
-
Constructor Summary
Constructors Constructor Description Rational(long numerator, long denominator)
Creates a new instance of Rational.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description byte
byteValue()
Returns the value of the specified number as abyte
.int
compareTo(MetadataExtractor.Rational that)
Compares twoMetadataExtractor.Rational
instances, returning true if they are mathematically equivalent (in consistence withequals(Object)
method).double
doubleValue()
Returns the value of the specified number as adouble
.boolean
equals(MetadataExtractor.Rational other)
Indicates whether this instance andother
are numerically equal, even if their representations differ.boolean
equals(java.lang.Object obj)
Compares twoMetadataExtractor.Rational
instances, returning true if they are mathematically equivalent.boolean
equalsExact(MetadataExtractor.Rational other)
Indicates whether this instance andother
have identical Numerator and Denominator.float
floatValue()
Returns the value of the specified number as afloat
.private long
GCD(long a, long b)
MetadataExtractor.Rational
getAbsolute()
Returns the absolute value of this object as a new Rational.long
getDenominator()
Returns the denominator.long
getNumerator()
Returns the numerator.MetadataExtractor.Rational
getReciprocal()
Returns the reciprocal value of this object as a new Rational.MetadataExtractor.Rational
getSimplifiedInstance()
Simplifies the representation of thisMetadataExtractor.Rational
number.int
hashCode()
int
intValue()
Returns the value of the specified number as anint
.boolean
isInteger()
Checks if thisMetadataExtractor.Rational
number is an Integer, either positive or negative.boolean
isPositive()
True if the value is non-zero and numerator and denominator are either both positive or both negative.boolean
isZero()
Checks if either the numerator or denominator are zero.long
longValue()
Returns the value of the specified number as along
.short
shortValue()
Returns the value of the specified number as ashort
.java.lang.String
toSimpleString(boolean allowDecimal)
Returns the simplest representation of thisMetadataExtractor.Rational
's value possible.java.lang.String
toString()
Returns a string representation of the object of formnumerator/denominator
.
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
_numerator
private final long _numerator
Holds the numerator.
-
_denominator
private final long _denominator
Holds the denominator.
-
-
Method Detail
-
doubleValue
public double doubleValue()
Returns the value of the specified number as adouble
. This may involve rounding.- Specified by:
doubleValue
in classjava.lang.Number
- Returns:
- the numeric value represented by this object after conversion
to type
double
.
-
floatValue
public float floatValue()
Returns the value of the specified number as afloat
. This may involve rounding.- Specified by:
floatValue
in classjava.lang.Number
- Returns:
- the numeric value represented by this object after conversion
to type
float
.
-
byteValue
public final byte byteValue()
Returns the value of the specified number as abyte
. This may involve rounding or truncation. This implementation simply casts the result ofdoubleValue()
tobyte
.- Overrides:
byteValue
in classjava.lang.Number
- Returns:
- the numeric value represented by this object after conversion
to type
byte
.
-
intValue
public final int intValue()
Returns the value of the specified number as anint
. This may involve rounding or truncation. This implementation simply casts the result ofdoubleValue()
toint
.- Specified by:
intValue
in classjava.lang.Number
- Returns:
- the numeric value represented by this object after conversion
to type
int
.
-
longValue
public final long longValue()
Returns the value of the specified number as along
. This may involve rounding or truncation. This implementation simply casts the result ofdoubleValue()
tolong
.- Specified by:
longValue
in classjava.lang.Number
- Returns:
- the numeric value represented by this object after conversion
to type
long
.
-
shortValue
public final short shortValue()
Returns the value of the specified number as ashort
. This may involve rounding or truncation. This implementation simply casts the result ofdoubleValue()
toshort
.- Overrides:
shortValue
in classjava.lang.Number
- Returns:
- the numeric value represented by this object after conversion
to type
short
.
-
getDenominator
public final long getDenominator()
Returns the denominator.
-
getNumerator
public final long getNumerator()
Returns the numerator.
-
getReciprocal
public MetadataExtractor.Rational getReciprocal()
Returns the reciprocal value of this object as a new Rational.- Returns:
- the reciprocal in a new object
-
getAbsolute
public MetadataExtractor.Rational getAbsolute()
Returns the absolute value of this object as a new Rational.- Returns:
- the absolute value in a new object
-
isInteger
public boolean isInteger()
Checks if thisMetadataExtractor.Rational
number is an Integer, either positive or negative.
-
isZero
public boolean isZero()
Checks if either the numerator or denominator are zero.
-
isPositive
public boolean isPositive()
True if the value is non-zero and numerator and denominator are either both positive or both negative.
-
toString
public java.lang.String toString()
Returns a string representation of the object of formnumerator/denominator
.- Overrides:
toString
in classjava.lang.Object
- Returns:
- a string representation of the object.
-
toSimpleString
public java.lang.String toSimpleString(boolean allowDecimal)
Returns the simplest representation of thisMetadataExtractor.Rational
's value possible.
-
compareTo
public int compareTo(MetadataExtractor.Rational that)
Compares twoMetadataExtractor.Rational
instances, returning true if they are mathematically equivalent (in consistence withequals(Object)
method).- Specified by:
compareTo
in interfacejava.lang.Comparable<MetadataExtractor.Rational>
- Parameters:
that
- theMetadataExtractor.Rational
to compare this instance to.- Returns:
- the value
0
if thisMetadataExtractor.Rational
is equal to the argumentMetadataExtractor.Rational
mathematically; a value less than0
if thisMetadataExtractor.Rational
is less than the argumentMetadataExtractor.Rational
; and a value greater than0
if thisMetadataExtractor.Rational
is greater than the argumentMetadataExtractor.Rational
.
-
equals
public boolean equals(MetadataExtractor.Rational other)
Indicates whether this instance andother
are numerically equal, even if their representations differ. For example, 1/2 is equal to 10/20 by this method. Similarly, 1/0 is equal to 100/0 by this method. To test equal representations, use EqualsExact.- Parameters:
other
- The rational value to compare with
-
equalsExact
public boolean equalsExact(MetadataExtractor.Rational other)
Indicates whether this instance andother
have identical Numerator and Denominator.For example, 1/2 is not equal to 10/20 by this method. Similarly, 1/0 is not equal to 100/0 by this method. To test numerically equivalence, use Equals(Rational).
- Parameters:
other
- The rational value to compare with
-
equals
public boolean equals(java.lang.Object obj)
Compares twoMetadataExtractor.Rational
instances, returning true if they are mathematically equivalent.- Overrides:
equals
in classjava.lang.Object
- Parameters:
obj
- theMetadataExtractor.Rational
to compare this instance to.- Returns:
- true if instances are mathematically equivalent, otherwise false. Will also
return false if
obj
is not an instance ofMetadataExtractor.Rational
.
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
getSimplifiedInstance
public MetadataExtractor.Rational getSimplifiedInstance()
Simplifies the representation of this
MetadataExtractor.Rational
number.For example, 5/10 simplifies to 1/2 because both Numerator and Denominator share a common factor of 5.
Uses the Euclidean Algorithm to find the greatest common divisor.
- Returns:
- A simplified instance if one exists, otherwise a copy of the original value.
-
GCD
private long GCD(long a, long b)
-
-