|
| 1 | +package org.json.junit; |
| 2 | + |
| 3 | +import java.math.BigDecimal; |
| 4 | +import java.math.BigInteger; |
| 5 | +import java.math.RoundingMode; |
| 6 | + |
| 7 | +/** |
| 8 | + * basic fraction class, no frills. |
| 9 | + * @author John Aylward |
| 10 | + * |
| 11 | + */ |
| 12 | +public class Fraction extends Number implements Comparable<Fraction> { |
| 13 | + /** |
| 14 | + * serial id. |
| 15 | + */ |
| 16 | + private static final long serialVersionUID = 1L; |
| 17 | + |
| 18 | + /** |
| 19 | + * value as a big decimal. |
| 20 | + */ |
| 21 | + private final BigDecimal bigDecimal; |
| 22 | + |
| 23 | + /** |
| 24 | + * value of the denominator. |
| 25 | + */ |
| 26 | + private final BigInteger denominator; |
| 27 | + /** |
| 28 | + * value of the numerator. |
| 29 | + */ |
| 30 | + private final BigInteger numerator; |
| 31 | + |
| 32 | + /** |
| 33 | + * @param numerator |
| 34 | + * numerator |
| 35 | + * @param denominator |
| 36 | + * denominator |
| 37 | + */ |
| 38 | + public Fraction(final BigInteger numerator, final BigInteger denominator) { |
| 39 | + super(); |
| 40 | + if (numerator == null || denominator == null) { |
| 41 | + throw new IllegalArgumentException("All values must be non-null"); |
| 42 | + } |
| 43 | + if (denominator.compareTo(BigInteger.ZERO)==0) { |
| 44 | + throw new IllegalArgumentException("Divide by zero"); |
| 45 | + } |
| 46 | + |
| 47 | + final BigInteger n; |
| 48 | + final BigInteger d; |
| 49 | + // normalize fraction |
| 50 | + if (denominator.signum()<0) { |
| 51 | + n = numerator.negate(); |
| 52 | + d = denominator.negate(); |
| 53 | + } else { |
| 54 | + n = numerator; |
| 55 | + d = denominator; |
| 56 | + } |
| 57 | + this.numerator = n; |
| 58 | + this.denominator = d; |
| 59 | + if (n.compareTo(BigInteger.ZERO)==0) { |
| 60 | + this.bigDecimal = BigDecimal.ZERO; |
| 61 | + } else if (n.compareTo(d)==0) {// i.e. 4/4, 10/10 |
| 62 | + this.bigDecimal = BigDecimal.ONE; |
| 63 | + } else { |
| 64 | + this.bigDecimal = new BigDecimal(this.numerator).divide(new BigDecimal(this.denominator), |
| 65 | + RoundingMode.HALF_EVEN); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @param numerator |
| 71 | + * numerator |
| 72 | + * @param denominator |
| 73 | + * denominator |
| 74 | + */ |
| 75 | + public Fraction(final long numerator, final long denominator) { |
| 76 | + this(BigInteger.valueOf(numerator),BigInteger.valueOf(denominator)); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @return the decimal |
| 81 | + */ |
| 82 | + public BigDecimal bigDecimalValue() { |
| 83 | + return this.bigDecimal; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public int compareTo(final Fraction o) { |
| 88 | + // .equals call this, so no .equals compare allowed |
| 89 | + |
| 90 | + // if they are the same reference, just return equals |
| 91 | + if (this == o) { |
| 92 | + return 0; |
| 93 | + } |
| 94 | + |
| 95 | + // if my denominators are already equal, just compare the numerators |
| 96 | + if (this.denominator.compareTo(o.denominator)==0) { |
| 97 | + return this.numerator.compareTo(o.numerator); |
| 98 | + } |
| 99 | + |
| 100 | + // get numerators of common denominators |
| 101 | + // a x ay xb |
| 102 | + // --- --- = ---- ---- |
| 103 | + // b y by yb |
| 104 | + final BigInteger thisN = this.numerator.multiply(o.denominator); |
| 105 | + final BigInteger otherN = o.numerator.multiply(this.denominator); |
| 106 | + |
| 107 | + return thisN.compareTo(otherN); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public double doubleValue() { |
| 112 | + return this.bigDecimal.doubleValue(); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @see java.lang.Object#equals(java.lang.Object) |
| 117 | + */ |
| 118 | + @Override |
| 119 | + public boolean equals(final Object obj) { |
| 120 | + if (this == obj) { |
| 121 | + return true; |
| 122 | + } |
| 123 | + if (obj == null) { |
| 124 | + return false; |
| 125 | + } |
| 126 | + if (this.getClass() != obj.getClass()) { |
| 127 | + return false; |
| 128 | + } |
| 129 | + final Fraction other = (Fraction) obj; |
| 130 | + return this.compareTo(other) == 0; |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public float floatValue() { |
| 135 | + return this.bigDecimal.floatValue(); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * @return the denominator |
| 140 | + */ |
| 141 | + public BigInteger getDenominator() { |
| 142 | + return this.denominator; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * @return the numerator |
| 147 | + */ |
| 148 | + public BigInteger getNumerator() { |
| 149 | + return this.numerator; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * @see java.lang.Object#hashCode() |
| 154 | + */ |
| 155 | + @Override |
| 156 | + public int hashCode() { |
| 157 | + final int prime = 31; |
| 158 | + int result = 1; |
| 159 | + result = prime * result + (this.bigDecimal == null ? 0 : this.bigDecimal.hashCode()); |
| 160 | + return result; |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public int intValue() { |
| 165 | + return this.bigDecimal.intValue(); |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public long longValue() { |
| 170 | + return this.bigDecimal.longValue(); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * @see java.lang.Object#toString() |
| 175 | + */ |
| 176 | + @Override |
| 177 | + public String toString() { |
| 178 | + return this.numerator + "/" + this.denominator; |
| 179 | + } |
| 180 | +} |
0 commit comments