Class Fraction

java.lang.Object
de.calamanari.pk.valueobject.Fraction
All Implemented Interfaces:
java.io.Serializable, java.lang.Comparable<Fraction>

public class Fraction
extends java.lang.Object
implements java.lang.Comparable<Fraction>, java.io.Serializable
Fraction demonstrates a VALUE OBJECT, fractions are immutable. When converted to BigDecimal, a scale of 20 will be used.
Author:
Karl Eilebrecht
See Also:
Serialized Form
  • Field Summary

    Fields
    Modifier and Type Field Description
    static java.util.Comparator<Fraction> VIEW_COMPARATOR
    Null-safe comparator for views (null to the bottom).
  • Constructor Summary

    Constructors
    Constructor Description
    Fraction​(double value)
    Creates a fraction from the given double, preserving decimals as possible
    Fraction​(long numerator, long denominator)
    Creates a fraction from two long values
    Fraction​(java.lang.String fraction)
    Recreates the fraction from its string representation, see toString() For convenience this constructor also accepts properly formatted integer and double values
    Fraction​(java.lang.String numerator, java.lang.String denominator)
    Creates a fraction from two Strings (integer values)
    Fraction​(java.math.BigDecimal value)
    Creates a fraction from the given BigDecimal, preserving 20 decimals
    Fraction​(java.math.BigInteger numerator, java.math.BigInteger denominator)
    Creates a fraction from two BigInteger values
    Fraction​(java.math.BigInteger numerator, java.math.BigInteger denominator, boolean reduce)
    Creates a fraction from two BigInteger values
  • Method Summary

    Modifier and Type Method Description
    Fraction add​(Fraction fraction)
    Returns the sum of this fraction and the given one
    int compareTo​(Fraction other)  
    Fraction divide​(Fraction fraction)
    Returns the result of the division of this fraction by the given one
    boolean equals​(java.lang.Object obj)
    Two fractions are equal if their corresponding reduced fractions are equal
    java.math.BigDecimal getBigDecimalValue()
    Returns the BigDecimal value of this fraction, assuming a scale of 20, rounding mode will be half-even
    java.math.BigInteger getDenominator()
    Returns the denominator
    double getDoubleValue()
    Returns the corresponding double value with maximum precision
    Fraction getInverse()
    Returns the inverse of this fraction
    java.math.BigInteger getNumerator()
    Returns the numerator
    int getSignum()
    Determines the signum of this fraction
    int hashCode()  
    boolean isOne()
    Determines if the value of this fractions represents the value (1/1)
    Fraction multiply​(Fraction fraction)
    Returns the product of the two fraction
    Fraction negate()
    Returns a negative copy of this fraction
    Fraction reduce()
    Returns a reduced version of this fraction
    Fraction subtract​(Fraction fraction)
    Returns this fraction minus the given one
    Fraction[] toCommonDenominator​(Fraction fraction1, Fraction fraction2)
    Brings the two fractions on a common denominator (smallest common multiple) and returns the result
    java.lang.String toString()
    Returns the string representation of this fraction of the form '(numerator/denominator)', the returned string may be used with the corresponding constructor to recreate the fraction

    Methods inherited from class java.lang.Object

    getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • VIEW_COMPARATOR

      public static final java.util.Comparator<Fraction> VIEW_COMPARATOR
      Null-safe comparator for views (null to the bottom). If the values of the two fractions are the same, this comparator orders them according to their numerators: 1/3, 2/6, 4/12

      This view behavior must not be implemented in compareTo(Fraction) due to the Comparable interface contract which requires equals() and compareTo() to be implemented consistently.

  • Constructor Details

    • Fraction

      public Fraction​(java.math.BigInteger numerator, java.math.BigInteger denominator, boolean reduce)
      Creates a fraction from two BigInteger values
      Parameters:
      numerator - fraction's numerator, NOT NULL
      denominator - fraction's denominator, NOT NULL, NOT ZERO
      reduce - if true, the result fraction will be reduced automatically
    • Fraction

      public Fraction​(java.math.BigInteger numerator, java.math.BigInteger denominator)
      Creates a fraction from two BigInteger values
      Parameters:
      numerator - fraction's numerator, NOT NULL
      denominator - fraction's denominator, NOT NULL, NOT ZERO
    • Fraction

      public Fraction​(java.lang.String numerator, java.lang.String denominator)
      Creates a fraction from two Strings (integer values)
      Parameters:
      numerator - fraction's numerator, correctly formatted integer number, NOT NULL
      denominator - fraction's denominator, correctly formatted integer number, NOT NULL, NOT ZERO
    • Fraction

      public Fraction​(java.lang.String fraction)
      Recreates the fraction from its string representation, see toString() For convenience this constructor also accepts properly formatted integer and double values
      Parameters:
      fraction - stringified fraction, NOT NULL
    • Fraction

      public Fraction​(long numerator, long denominator)
      Creates a fraction from two long values
      Parameters:
      numerator - fraction's numerator
      denominator - fraction's denominator, NOT ZERO
    • Fraction

      public Fraction​(java.math.BigDecimal value)
      Creates a fraction from the given BigDecimal, preserving 20 decimals
      Parameters:
      value - NOT NULL
    • Fraction

      public Fraction​(double value)
      Creates a fraction from the given double, preserving decimals as possible
      Parameters:
      value - number to be converted into a fraction
  • Method Details

    • getNumerator

      public java.math.BigInteger getNumerator()
      Returns the numerator
      Returns:
      numerator
    • getDenominator

      public java.math.BigInteger getDenominator()
      Returns the denominator
      Returns:
      denominator
    • negate

      public Fraction negate()
      Returns a negative copy of this fraction
      Returns:
      fraction * -1
    • getInverse

      public Fraction getInverse()
      Returns the inverse of this fraction
      Returns:
      (denomninator / numerator)
    • add

      public Fraction add​(Fraction fraction)
      Returns the sum of this fraction and the given one
      Parameters:
      fraction - value to be added
      Returns:
      sum fraction
    • subtract

      public Fraction subtract​(Fraction fraction)
      Returns this fraction minus the given one
      Parameters:
      fraction - value to be subtracted
      Returns:
      this - fraction
    • isOne

      public boolean isOne()
      Determines if the value of this fractions represents the value (1/1)
      Returns:
      true, if this fraction is equal to 1
    • multiply

      public Fraction multiply​(Fraction fraction)
      Returns the product of the two fraction
      Parameters:
      fraction - value to be multiplied with this fraction
      Returns:
      this * fraction
    • divide

      public Fraction divide​(Fraction fraction)
      Returns the result of the division of this fraction by the given one
      Parameters:
      fraction - divisor
      Returns:
      this / fraction
    • getSignum

      public int getSignum()
      Determines the signum of this fraction
      Returns:
      -1, 0 or 1 for negative, zero or positive values
    • reduce

      public Fraction reduce()
      Returns a reduced version of this fraction
      Returns:
      reduced fraction
    • toCommonDenominator

      public Fraction[] toCommonDenominator​(Fraction fraction1, Fraction fraction2)
      Brings the two fractions on a common denominator (smallest common multiple) and returns the result
      Parameters:
      fraction1 - first fraction
      fraction2 - second fraction
      Returns:
      array of two fractions with the same denominator
    • getBigDecimalValue

      public java.math.BigDecimal getBigDecimalValue()
      Returns the BigDecimal value of this fraction, assuming a scale of 20, rounding mode will be half-even
      Returns:
      BigDecimal representation of this fraction
    • getDoubleValue

      public double getDoubleValue()
      Returns the corresponding double value with maximum precision
      Returns:
      double value
    • equals

      public boolean equals​(java.lang.Object obj)
      Two fractions are equal if their corresponding reduced fractions are equal
      Overrides:
      equals in class java.lang.Object
      Parameters:
      obj - another fraction
      Returns:
      true if both fractions represent the same value, otherwise false
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class java.lang.Object
    • toString

      public java.lang.String toString()
      Returns the string representation of this fraction of the form '(numerator/denominator)', the returned string may be used with the corresponding constructor to recreate the fraction
      Overrides:
      toString in class java.lang.Object
      Returns:
      fraction as String
    • compareTo

      public int compareTo​(Fraction other)
      Specified by:
      compareTo in interface java.lang.Comparable<Fraction>