You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by tr...@apache.org on 2006/01/20 14:48:55 UTC
svn commit: r370807 [18/22] - in /directory/sandbox/trustin/mina-spi: ./
core/src/main/java/org/apache/mina/common/
core/src/main/java/org/apache/mina/common/support/
core/src/main/java/org/apache/mina/common/support/discovery/
core/src/main/java/org/a...
Added: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/math/Fraction.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/math/Fraction.java?rev=370807&view=auto
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/math/Fraction.java (added)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/math/Fraction.java Fri Jan 20 05:47:50 2006
@@ -0,0 +1,921 @@
+/*
+ * Copyright 2002-2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.mina.common.support.lang.math;
+
+import java.io.Serializable;
+import java.math.BigInteger;
+
+/**
+ * <p><code>Fraction</code> is a <code>Number</code> implementation that
+ * stores fractions accurately.</p>
+ *
+ * <p>This class is immutable, and interoperable with most methods that accept
+ * a <code>Number</code>.</p>
+ *
+ * @author Travis Reeder
+ * @author Stephen Colebourne
+ * @author Tim O'Brien
+ * @author Pete Gieser
+ * @author C. Scott Ananian
+ * @since 2.0
+ * @version $Id$
+ */
+public final class Fraction extends Number implements Serializable, Comparable {
+
+ /** Serialization lock, Lang version 2.0 */
+ private static final long serialVersionUID = 65382027393090L;
+
+ /**
+ * <code>Fraction</code> representation of 0.
+ */
+ public static final Fraction ZERO = new Fraction(0, 1);
+ /**
+ * <code>Fraction</code> representation of 1.
+ */
+ public static final Fraction ONE = new Fraction(1, 1);
+ /**
+ * <code>Fraction</code> representation of 1/2.
+ */
+ public static final Fraction ONE_HALF = new Fraction(1, 2);
+ /**
+ * <code>Fraction</code> representation of 1/3.
+ */
+ public static final Fraction ONE_THIRD = new Fraction(1, 3);
+ /**
+ * <code>Fraction</code> representation of 2/3.
+ */
+ public static final Fraction TWO_THIRDS = new Fraction(2, 3);
+ /**
+ * <code>Fraction</code> representation of 1/4.
+ */
+ public static final Fraction ONE_QUARTER = new Fraction(1, 4);
+ /**
+ * <code>Fraction</code> representation of 2/4.
+ */
+ public static final Fraction TWO_QUARTERS = new Fraction(2, 4);
+ /**
+ * <code>Fraction</code> representation of 3/4.
+ */
+ public static final Fraction THREE_QUARTERS = new Fraction(3, 4);
+ /**
+ * <code>Fraction</code> representation of 1/5.
+ */
+ public static final Fraction ONE_FIFTH = new Fraction(1, 5);
+ /**
+ * <code>Fraction</code> representation of 2/5.
+ */
+ public static final Fraction TWO_FIFTHS = new Fraction(2, 5);
+ /**
+ * <code>Fraction</code> representation of 3/5.
+ */
+ public static final Fraction THREE_FIFTHS = new Fraction(3, 5);
+ /**
+ * <code>Fraction</code> representation of 4/5.
+ */
+ public static final Fraction FOUR_FIFTHS = new Fraction(4, 5);
+
+
+ /**
+ * The numerator number part of the fraction (the three in three sevenths).
+ */
+ private final int numerator;
+ /**
+ * The denominator number part of the fraction (the seven in three sevenths).
+ */
+ private final int denominator;
+
+ /**
+ * Cached output hashCode (class is immutable).
+ */
+ private transient int hashCode = 0;
+ /**
+ * Cached output toString (class is immutable).
+ */
+ private transient String toString = null;
+ /**
+ * Cached output toProperString (class is immutable).
+ */
+ private transient String toProperString = null;
+
+ /**
+ * <p>Constructs a <code>Fraction</code> instance with the 2 parts
+ * of a fraction Y/Z.</p>
+ *
+ * @param numerator the numerator, for example the three in 'three sevenths'
+ * @param denominator the denominator, for example the seven in 'three sevenths'
+ */
+ private Fraction(int numerator, int denominator) {
+ super();
+ this.numerator = numerator;
+ this.denominator = denominator;
+ }
+
+ /**
+ * <p>Creates a <code>Fraction</code> instance with the 2 parts
+ * of a fraction Y/Z.</p>
+ *
+ * <p>Any negative signs are resolved to be on the numerator.</p>
+ *
+ * @param numerator the numerator, for example the three in 'three sevenths'
+ * @param denominator the denominator, for example the seven in 'three sevenths'
+ * @return a new fraction instance
+ * @throws ArithmeticException if the denomiator is <code>zero</code>
+ */
+ public static Fraction getFraction(int numerator, int denominator) {
+ if (denominator == 0) {
+ throw new ArithmeticException("The denominator must not be zero");
+ }
+ if (denominator < 0) {
+ if (numerator==Integer.MIN_VALUE ||
+ denominator==Integer.MIN_VALUE) {
+ throw new ArithmeticException("overflow: can't negate");
+ }
+ numerator = -numerator;
+ denominator = -denominator;
+ }
+ return new Fraction(numerator, denominator);
+ }
+
+ /**
+ * <p>Creates a <code>Fraction</code> instance with the 3 parts
+ * of a fraction X Y/Z.</p>
+ *
+ * <p>The negative sign must be passed in on the whole number part.</p>
+ *
+ * @param whole the whole number, for example the one in 'one and three sevenths'
+ * @param numerator the numerator, for example the three in 'one and three sevenths'
+ * @param denominator the denominator, for example the seven in 'one and three sevenths'
+ * @return a new fraction instance
+ * @throws ArithmeticException if the denomiator is <code>zero</code>
+ * @throws ArithmeticException if the denominator is negative
+ * @throws ArithmeticException if the numerator is negative
+ * @throws ArithmeticException if the resulting numerator exceeds
+ * <code>Integer.MAX_VALUE</code>
+ */
+ public static Fraction getFraction(int whole, int numerator, int denominator) {
+ if (denominator == 0) {
+ throw new ArithmeticException("The denominator must not be zero");
+ }
+ if (denominator < 0) {
+ throw new ArithmeticException("The denominator must not be negative");
+ }
+ if (numerator < 0) {
+ throw new ArithmeticException("The numerator must not be negative");
+ }
+ long numeratorValue;
+ if (whole < 0) {
+ numeratorValue = whole * (long)denominator - numerator;
+ } else {
+ numeratorValue = whole * (long)denominator + numerator;
+ }
+ if (numeratorValue < Integer.MIN_VALUE ||
+ numeratorValue > Integer.MAX_VALUE) {
+ throw new ArithmeticException("Numerator too large to represent as an Integer.");
+ }
+ return new Fraction((int) numeratorValue, denominator);
+ }
+
+ /**
+ * <p>Creates a <code>Fraction</code> instance with the 2 parts
+ * of a fraction Y/Z.</p>
+ *
+ * <p>Any negative signs are resolved to be on the numerator.</p>
+ *
+ * @param numerator the numerator, for example the three in 'three sevenths'
+ * @param denominator the denominator, for example the seven in 'three sevenths'
+ * @return a new fraction instance, with the numerator and denominator reduced
+ * @throws ArithmeticException if the denominator is <code>zero</code>
+ */
+ public static Fraction getReducedFraction(int numerator, int denominator) {
+ if (denominator == 0) {
+ throw new ArithmeticException("The denominator must not be zero");
+ }
+ if (numerator==0) {
+ return ZERO; // normalize zero.
+ }
+ // allow 2^k/-2^31 as a valid fraction (where k>0)
+ if (denominator==Integer.MIN_VALUE && (numerator&1)==0) {
+ numerator/=2; denominator/=2;
+ }
+ if (denominator < 0) {
+ if (numerator==Integer.MIN_VALUE ||
+ denominator==Integer.MIN_VALUE) {
+ throw new ArithmeticException("overflow: can't negate");
+ }
+ numerator = -numerator;
+ denominator = -denominator;
+ }
+ // simplify fraction.
+ int gcd = greatestCommonDivisor(numerator, denominator);
+ numerator /= gcd;
+ denominator /= gcd;
+ return new Fraction(numerator, denominator);
+ }
+
+ /**
+ * <p>Creates a <code>Fraction</code> instance from a <code>double</code> value.</p>
+ *
+ * <p>This method uses the <a href="http://archives.math.utk.edu/articles/atuyl/confrac/">
+ * continued fraction algorithm</a>, computing a maximum of
+ * 25 convergents and bounding the denominator by 10,000.</p>
+ *
+ * @param value the double value to convert
+ * @return a new fraction instance that is close to the value
+ * @throws ArithmeticException if <code>|value| > Integer.MAX_VALUE</code>
+ * or <code>value = NaN</code>
+ * @throws ArithmeticException if the calculated denominator is <code>zero</code>
+ * @throws ArithmeticException if the the algorithm does not converge
+ */
+ public static Fraction getFraction(double value) {
+ int sign = (value < 0 ? -1 : 1);
+ value = Math.abs(value);
+ if (value > Integer.MAX_VALUE || Double.isNaN(value)) {
+ throw new ArithmeticException
+ ("The value must not be greater than Integer.MAX_VALUE or NaN");
+ }
+ int wholeNumber = (int) value;
+ value -= wholeNumber;
+
+ int numer0 = 0; // the pre-previous
+ int denom0 = 1; // the pre-previous
+ int numer1 = 1; // the previous
+ int denom1 = 0; // the previous
+ int numer2 = 0; // the current, setup in calculation
+ int denom2 = 0; // the current, setup in calculation
+ int a1 = (int) value;
+ int a2 = 0;
+ double x1 = 1;
+ double x2 = 0;
+ double y1 = value - a1;
+ double y2 = 0;
+ double delta1, delta2 = Double.MAX_VALUE;
+ double fraction;
+ int i = 1;
+// System.out.println("---");
+ do {
+ delta1 = delta2;
+ a2 = (int) (x1 / y1);
+ x2 = y1;
+ y2 = x1 - a2 * y1;
+ numer2 = a1 * numer1 + numer0;
+ denom2 = a1 * denom1 + denom0;
+ fraction = (double) numer2 / (double) denom2;
+ delta2 = Math.abs(value - fraction);
+// System.out.println(numer2 + " " + denom2 + " " + fraction + " " + delta2 + " " + y1);
+ a1 = a2;
+ x1 = x2;
+ y1 = y2;
+ numer0 = numer1;
+ denom0 = denom1;
+ numer1 = numer2;
+ denom1 = denom2;
+ i++;
+// System.out.println(">>" + delta1 +" "+ delta2+" "+(delta1 > delta2)+" "+i+" "+denom2);
+ } while ((delta1 > delta2) && (denom2 <= 10000) && (denom2 > 0) && (i < 25));
+ if (i == 25) {
+ throw new ArithmeticException("Unable to convert double to fraction");
+ }
+ return getReducedFraction((numer0 + wholeNumber * denom0) * sign, denom0);
+ }
+
+ /**
+ * <p>Creates a Fraction from a <code>String</code>.</p>
+ *
+ * <p>The formats accepted are:</p>
+ *
+ * <ol>
+ * <li><code>double</code> String containing a dot</li>
+ * <li>'X Y/Z'</li>
+ * <li>'Y/Z'</li>
+ * <li>'X' (a simple whole number)</li>
+ * </ol>
+ * and a .</p>
+ *
+ * @param str the string to parse, must not be <code>null</code>
+ * @return the new <code>Fraction</code> instance
+ * @throws IllegalArgumentException if the string is <code>null</code>
+ * @throws NumberFormatException if the number format is invalid
+ */
+ public static Fraction getFraction(String str) {
+ if (str == null) {
+ throw new IllegalArgumentException("The string must not be null");
+ }
+ // parse double format
+ int pos = str.indexOf('.');
+ if (pos >= 0) {
+ return getFraction(Double.parseDouble(str));
+ }
+
+ // parse X Y/Z format
+ pos = str.indexOf(' ');
+ if (pos > 0) {
+ int whole = Integer.parseInt(str.substring(0, pos));
+ str = str.substring(pos + 1);
+ pos = str.indexOf('/');
+ if (pos < 0) {
+ throw new NumberFormatException("The fraction could not be parsed as the format X Y/Z");
+ } else {
+ int numer = Integer.parseInt(str.substring(0, pos));
+ int denom = Integer.parseInt(str.substring(pos + 1));
+ return getFraction(whole, numer, denom);
+ }
+ }
+
+ // parse Y/Z format
+ pos = str.indexOf('/');
+ if (pos < 0) {
+ // simple whole number
+ return getFraction(Integer.parseInt(str), 1);
+ } else {
+ int numer = Integer.parseInt(str.substring(0, pos));
+ int denom = Integer.parseInt(str.substring(pos + 1));
+ return getFraction(numer, denom);
+ }
+ }
+
+ // Accessors
+ //-------------------------------------------------------------------
+
+ /**
+ * <p>Gets the numerator part of the fraction.</p>
+ *
+ * <p>This method may return a value greater than the denominator, an
+ * improper fraction, such as the seven in 7/4.</p>
+ *
+ * @return the numerator fraction part
+ */
+ public int getNumerator() {
+ return numerator;
+ }
+
+ /**
+ * <p>Gets the denominator part of the fraction.</p>
+ *
+ * @return the denominator fraction part
+ */
+ public int getDenominator() {
+ return denominator;
+ }
+
+ /**
+ * <p>Gets the proper numerator, always positive.</p>
+ *
+ * <p>An improper fraction 7/4 can be resolved into a proper one, 1 3/4.
+ * This method returns the 3 from the proper fraction.</p>
+ *
+ * <p>If the fraction is negative such as -7/4, it can be resolved into
+ * -1 3/4, so this method returns the positive proper numerator, 3.</p>
+ *
+ * @return the numerator fraction part of a proper fraction, always positive
+ */
+ public int getProperNumerator() {
+ return Math.abs(numerator % denominator);
+ }
+
+ /**
+ * <p>Gets the proper whole part of the fraction.</p>
+ *
+ * <p>An improper fraction 7/4 can be resolved into a proper one, 1 3/4.
+ * This method returns the 1 from the proper fraction.</p>
+ *
+ * <p>If the fraction is negative such as -7/4, it can be resolved into
+ * -1 3/4, so this method returns the positive whole part -1.</p>
+ *
+ * @return the whole fraction part of a proper fraction, that includes the sign
+ */
+ public int getProperWhole() {
+ return numerator / denominator;
+ }
+
+ // Number methods
+ //-------------------------------------------------------------------
+
+ /**
+ * <p>Gets the fraction as an <code>int</code>. This returns the whole number
+ * part of the fraction.</p>
+ *
+ * @return the whole number fraction part
+ */
+ public int intValue() {
+ return numerator / denominator;
+ }
+
+ /**
+ * <p>Gets the fraction as a <code>long</code>. This returns the whole number
+ * part of the fraction.</p>
+ *
+ * @return the whole number fraction part
+ */
+ public long longValue() {
+ return (long) numerator / denominator;
+ }
+
+ /**
+ * <p>Gets the fraction as a <code>float</code>. This calculates the fraction
+ * as the numerator divided by denominator.</p>
+ *
+ * @return the fraction as a <code>float</code>
+ */
+ public float floatValue() {
+ return ((float) numerator) / ((float) denominator);
+ }
+
+ /**
+ * <p>Gets the fraction as a <code>double</code>. This calculates the fraction
+ * as the numerator divided by denominator.</p>
+ *
+ * @return the fraction as a <code>double</code>
+ */
+ public double doubleValue() {
+ return ((double) numerator) / ((double) denominator);
+ }
+
+ // Calculations
+ //-------------------------------------------------------------------
+
+ /**
+ * <p>Reduce the fraction to the smallest values for the numerator and
+ * denominator, returning the result..</p>
+ *
+ * @return a new reduce fraction instance, or this if no simplification possible
+ */
+ public Fraction reduce() {
+ int gcd = greatestCommonDivisor(Math.abs(numerator), denominator);
+ return Fraction.getFraction(numerator / gcd, denominator / gcd);
+ }
+
+ /**
+ * <p>Gets a fraction that is the inverse (1/fraction) of this one.</p>
+ *
+ * <p>The returned fraction is not reduced.</p>
+ *
+ * @return a new fraction instance with the numerator and denominator
+ * inverted.
+ * @throws ArithmeticException if the fraction represents zero.
+ */
+ public Fraction invert() {
+ if (numerator == 0) {
+ throw new ArithmeticException("Unable to invert zero.");
+ }
+ if (numerator==Integer.MIN_VALUE) {
+ throw new ArithmeticException("overflow: can't negate numerator");
+ }
+ if (numerator<0) {
+ return new Fraction(-denominator, -numerator);
+ } else {
+ return new Fraction(denominator, numerator);
+ }
+ }
+
+ /**
+ * <p>Gets a fraction that is the negative (-fraction) of this one.</p>
+ *
+ * <p>The returned fraction is not reduced.</p>
+ *
+ * @return a new fraction instance with the opposite signed numerator
+ */
+ public Fraction negate() {
+ // the positive range is one smaller than the negative range of an int.
+ if (numerator==Integer.MIN_VALUE) {
+ throw new ArithmeticException("overflow: too large to negate");
+ }
+ return new Fraction(-numerator, denominator);
+ }
+
+ /**
+ * <p>Gets a fraction that is the positive equivalent of this one.</p>
+ * <p>More precisely: <code>(fraction >= 0 ? this : -fraction)</code></p>
+ *
+ * <p>The returned fraction is not reduced.</p>
+ *
+ * @return <code>this</code> if it is positive, or a new positive fraction
+ * instance with the opposite signed numerator
+ */
+ public Fraction abs() {
+ if (numerator >= 0) {
+ return this;
+ }
+ return negate();
+ }
+
+ /**
+ * <p>Gets a fraction that is raised to the passed in power.</p>
+ *
+ * <p>The returned fraction is in reduced form.</p>
+ *
+ * @param power the power to raise the fraction to
+ * @return <code>this</code> if the power is one, <code>ONE</code> if the power
+ * is zero (even if the fraction equals ZERO) or a new fraction instance
+ * raised to the appropriate power
+ * @throws ArithmeticException if the resulting numerator or denominator exceeds
+ * <code>Integer.MAX_VALUE</code>
+ */
+ public Fraction pow(int power) {
+ if (power == 1) {
+ return this;
+ } else if (power == 0) {
+ return ONE;
+ } else if (power < 0) {
+ if (power==Integer.MIN_VALUE) { // MIN_VALUE can't be negated.
+ return this.invert().pow(2).pow(-(power/2));
+ }
+ return this.invert().pow(-power);
+ } else {
+ Fraction f = this.multiplyBy(this);
+ if ((power % 2) == 0) { // if even...
+ return f.pow(power/2);
+ } else { // if odd...
+ return f.pow(power/2).multiplyBy(this);
+ }
+ }
+ }
+
+ /**
+ * <p>Gets the greatest common divisor of the absolute value of
+ * two numbers, using the "binary gcd" method which avoids
+ * division and modulo operations. See Knuth 4.5.2 algorithm B.
+ * This algorithm is due to Josef Stein (1961).</p>
+ *
+ * @param u a non-zero number
+ * @param v a non-zero number
+ * @return the greatest common divisor, never zero
+ */
+ private static int greatestCommonDivisor(int u, int v) {
+ // keep u and v negative, as negative integers range down to
+ // -2^31, while positive numbers can only be as large as 2^31-1
+ // (i.e. we can't necessarily negate a negative number without
+ // overflow)
+ /* assert u!=0 && v!=0; */
+ if (u>0) { u=-u; } // make u negative
+ if (v>0) { v=-v; } // make v negative
+ // B1. [Find power of 2]
+ int k=0;
+ while ((u&1)==0 && (v&1)==0 && k<31) { // while u and v are both even...
+ u/=2; v/=2; k++; // cast out twos.
+ }
+ if (k==31) {
+ throw new ArithmeticException("overflow: gcd is 2^31");
+ }
+ // B2. Initialize: u and v have been divided by 2^k and at least
+ // one is odd.
+ int t = ((u&1)==1) ? v : -(u/2)/*B3*/;
+ // t negative: u was odd, v may be even (t replaces v)
+ // t positive: u was even, v is odd (t replaces u)
+ do {
+ /* assert u<0 && v<0; */
+ // B4/B3: cast out twos from t.
+ while ((t&1)==0) { // while t is even..
+ t/=2; // cast out twos
+ }
+ // B5 [reset max(u,v)]
+ if (t>0) {
+ u = -t;
+ } else {
+ v = t;
+ }
+ // B6/B3. at this point both u and v should be odd.
+ t = (v - u)/2;
+ // |u| larger: t positive (replace u)
+ // |v| larger: t negative (replace v)
+ } while (t!=0);
+ return -u*(1<<k); // gcd is u*2^k
+ }
+
+ // Arithmetic
+ //-------------------------------------------------------------------
+
+ /**
+ * Multiply two integers, checking for overflow.
+ *
+ * @param x a factor
+ * @param y a factor
+ * @return the product <code>x*y</code>
+ * @throws ArithmeticException if the result can not be represented as
+ * an int
+ */
+ private static int mulAndCheck(int x, int y) {
+ long m = ((long)x)*((long)y);
+ if (m < Integer.MIN_VALUE ||
+ m > Integer.MAX_VALUE) {
+ throw new ArithmeticException("overflow: mul");
+ }
+ return (int)m;
+ }
+
+ /**
+ * Multiply two non-negative integers, checking for overflow.
+ *
+ * @param x a non-negative factor
+ * @param y a non-negative factor
+ * @return the product <code>x*y</code>
+ * @throws ArithmeticException if the result can not be represented as
+ * an int
+ */
+ private static int mulPosAndCheck(int x, int y) {
+ /* assert x>=0 && y>=0; */
+ long m = ((long)x)*((long)y);
+ if (m > Integer.MAX_VALUE) {
+ throw new ArithmeticException("overflow: mulPos");
+ }
+ return (int)m;
+ }
+
+ /**
+ * Add two integers, checking for overflow.
+ *
+ * @param x an addend
+ * @param y an addend
+ * @return the sum <code>x+y</code>
+ * @throws ArithmeticException if the result can not be represented as
+ * an int
+ */
+ private static int addAndCheck(int x, int y) {
+ long s = (long)x+(long)y;
+ if (s < Integer.MIN_VALUE ||
+ s > Integer.MAX_VALUE) {
+ throw new ArithmeticException("overflow: add");
+ }
+ return (int)s;
+ }
+
+ /**
+ * Subtract two integers, checking for overflow.
+ *
+ * @param x the minuend
+ * @param y the subtrahend
+ * @return the difference <code>x-y</code>
+ * @throws ArithmeticException if the result can not be represented as
+ * an int
+ */
+ private static int subAndCheck(int x, int y) {
+ long s = (long)x-(long)y;
+ if (s < Integer.MIN_VALUE ||
+ s > Integer.MAX_VALUE) {
+ throw new ArithmeticException("overflow: add");
+ }
+ return (int)s;
+ }
+
+ /**
+ * <p>Adds the value of this fraction to another, returning the result in reduced form.
+ * The algorithm follows Knuth, 4.5.1.</p>
+ *
+ * @param fraction the fraction to add, must not be <code>null</code>
+ * @return a <code>Fraction</code> instance with the resulting values
+ * @throws IllegalArgumentException if the fraction is <code>null</code>
+ * @throws ArithmeticException if the resulting numerator or denominator exceeds
+ * <code>Integer.MAX_VALUE</code>
+ */
+ public Fraction add(Fraction fraction) {
+ return addSub(fraction, true /* add */);
+ }
+
+ /**
+ * <p>Subtracts the value of another fraction from the value of this one,
+ * returning the result in reduced form.</p>
+ *
+ * @param fraction the fraction to subtract, must not be <code>null</code>
+ * @return a <code>Fraction</code> instance with the resulting values
+ * @throws IllegalArgumentException if the fraction is <code>null</code>
+ * @throws ArithmeticException if the resulting numerator or denominator
+ * cannot be represented in an <code>int</code>.
+ */
+ public Fraction subtract(Fraction fraction) {
+ return addSub(fraction, false /* subtract */);
+ }
+
+ /**
+ * Implement add and subtract using algorithm described in Knuth 4.5.1.
+ *
+ * @param fraction the fraction to subtract, must not be <code>null</code>
+ * @param isAdd true to add, false to subtract
+ * @return a <code>Fraction</code> instance with the resulting values
+ * @throws IllegalArgumentException if the fraction is <code>null</code>
+ * @throws ArithmeticException if the resulting numerator or denominator
+ * cannot be represented in an <code>int</code>.
+ */
+ private Fraction addSub(Fraction fraction, boolean isAdd) {
+ if (fraction == null) {
+ throw new IllegalArgumentException("The fraction must not be null");
+ }
+ // zero is identity for addition.
+ if (numerator == 0) {
+ return isAdd ? fraction : fraction.negate();
+ }
+ if (fraction.numerator == 0) {
+ return this;
+ }
+ // if denominators are randomly distributed, d1 will be 1 about 61%
+ // of the time.
+ int d1 = greatestCommonDivisor(denominator, fraction.denominator);
+ if (d1==1) {
+ // result is ( (u*v' +/- u'v) / u'v')
+ int uvp = mulAndCheck(numerator, fraction.denominator);
+ int upv = mulAndCheck(fraction.numerator, denominator);
+ return new Fraction
+ (isAdd ? addAndCheck(uvp, upv) : subAndCheck(uvp, upv),
+ mulPosAndCheck(denominator, fraction.denominator));
+ }
+ // the quantity 't' requires 65 bits of precision; see knuth 4.5.1
+ // exercise 7. we're going to use a BigInteger.
+ // t = u(v'/d1) +/- v(u'/d1)
+ BigInteger uvp = BigInteger.valueOf(numerator)
+ .multiply(BigInteger.valueOf(fraction.denominator/d1));
+ BigInteger upv = BigInteger.valueOf(fraction.numerator)
+ .multiply(BigInteger.valueOf(denominator/d1));
+ BigInteger t = isAdd ? uvp.add(upv) : uvp.subtract(upv);
+ // but d2 doesn't need extra precision because
+ // d2 = gcd(t,d1) = gcd(t mod d1, d1)
+ int tmodd1 = t.mod(BigInteger.valueOf(d1)).intValue();
+ int d2 = (tmodd1==0)?d1:greatestCommonDivisor(tmodd1, d1);
+
+ // result is (t/d2) / (u'/d1)(v'/d2)
+ BigInteger w = t.divide(BigInteger.valueOf(d2));
+ if (w.bitLength() > 31) {
+ throw new ArithmeticException
+ ("overflow: numerator too large after multiply");
+ }
+ return new Fraction
+ (w.intValue(),
+ mulPosAndCheck(denominator/d1, fraction.denominator/d2));
+ }
+
+ /**
+ * <p>Multiplies the value of this fraction by another, returning the
+ * result in reduced form.</p>
+ *
+ * @param fraction the fraction to multiply by, must not be <code>null</code>
+ * @return a <code>Fraction</code> instance with the resulting values
+ * @throws IllegalArgumentException if the fraction is <code>null</code>
+ * @throws ArithmeticException if the resulting numerator or denominator exceeds
+ * <code>Integer.MAX_VALUE</code>
+ */
+ public Fraction multiplyBy(Fraction fraction) {
+ if (fraction == null) {
+ throw new IllegalArgumentException("The fraction must not be null");
+ }
+ if (numerator == 0 || fraction.numerator == 0) {
+ return ZERO;
+ }
+ // knuth 4.5.1
+ // make sure we don't overflow unless the result *must* overflow.
+ int d1 = greatestCommonDivisor(numerator, fraction.denominator);
+ int d2 = greatestCommonDivisor(fraction.numerator, denominator);
+ return getReducedFraction
+ (mulAndCheck(numerator/d1, fraction.numerator/d2),
+ mulPosAndCheck(denominator/d2, fraction.denominator/d1));
+ }
+
+ /**
+ * <p>Divide the value of this fraction by another.</p>
+ *
+ * @param fraction the fraction to divide by, must not be <code>null</code>
+ * @return a <code>Fraction</code> instance with the resulting values
+ * @throws IllegalArgumentException if the fraction is <code>null</code>
+ * @throws ArithmeticException if the fraction to divide by is zero
+ * @throws ArithmeticException if the resulting numerator or denominator exceeds
+ * <code>Integer.MAX_VALUE</code>
+ */
+ public Fraction divideBy(Fraction fraction) {
+ if (fraction == null) {
+ throw new IllegalArgumentException("The fraction must not be null");
+ }
+ if (fraction.numerator == 0) {
+ throw new ArithmeticException("The fraction to divide by must not be zero");
+ }
+ return multiplyBy(fraction.invert());
+ }
+
+ // Basics
+ //-------------------------------------------------------------------
+
+ /**
+ * <p>Compares this fraction to another object to test if they are equal.</p>.
+ *
+ * <p>To be equal, both values must be equal. Thus 2/4 is not equal to 1/2.</p>
+ *
+ * @param obj the reference object with which to compare
+ * @return <code>true</code> if this object is equal
+ */
+ public boolean equals(Object obj) {
+ if (obj == this) {
+ return true;
+ }
+ if (obj instanceof Fraction == false) {
+ return false;
+ }
+ Fraction other = (Fraction) obj;
+ return (getNumerator() == other.getNumerator() &&
+ getDenominator() == other.getDenominator());
+ }
+
+ /**
+ * <p>Gets a hashCode for the fraction.</p>
+ *
+ * @return a hash code value for this object
+ */
+ public int hashCode() {
+ if (hashCode == 0) {
+ // hashcode update should be atomic.
+ hashCode = 37 * (37 * 17 + getNumerator()) + getDenominator();
+ }
+ return hashCode;
+ }
+
+ /**
+ * <p>Compares this object to another based on size.</p>
+ *
+ * @param object the object to compare to
+ * @return -1 if this is less, 0 if equal, +1 if greater
+ * @throws ClassCastException if the object is not a <code>Fraction</code>
+ * @throws NullPointerException if the object is <code>null</code>
+ */
+ public int compareTo(Object object) {
+ Fraction other = (Fraction) object;
+ if (this==other) {
+ return 0;
+ }
+ if (numerator == other.numerator && denominator == other.denominator) {
+ return 0;
+ }
+
+ // otherwise see which is less
+ long first = (long) numerator * (long) other.denominator;
+ long second = (long) other.numerator * (long) denominator;
+ if (first == second) {
+ return 0;
+ } else if (first < second) {
+ return -1;
+ } else {
+ return 1;
+ }
+ }
+
+ /**
+ * <p>Gets the fraction as a <code>String</code>.</p>
+ *
+ * <p>The format used is '<i>numerator</i>/<i>denominator</i>' always.
+ *
+ * @return a <code>String</code> form of the fraction
+ */
+ public String toString() {
+ if (toString == null) {
+ toString = new StringBuffer(32)
+ .append(getNumerator())
+ .append('/')
+ .append(getDenominator()).toString();
+ }
+ return toString;
+ }
+
+ /**
+ * <p>Gets the fraction as a proper <code>String</code> in the format X Y/Z.</p>
+ *
+ * <p>The format used in '<i>wholeNumber</i> <i>numerator</i>/<i>denominator</i>'.
+ * If the whole number is zero it will be ommitted. If the numerator is zero,
+ * only the whole number is returned.</p>
+ *
+ * @return a <code>String</code> form of the fraction
+ */
+ public String toProperString() {
+ if (toProperString == null) {
+ if (numerator == 0) {
+ toProperString = "0";
+ } else if (numerator == denominator) {
+ toProperString = "1";
+ } else if ((numerator>0?-numerator:numerator) < -denominator) {
+ // note that we do the magnitude comparison test above with
+ // NEGATIVE (not positive) numbers, since negative numbers
+ // have a larger range. otherwise numerator==Integer.MIN_VALUE
+ // is handled incorrectly.
+ int properNumerator = getProperNumerator();
+ if (properNumerator == 0) {
+ toProperString = Integer.toString(getProperWhole());
+ } else {
+ toProperString = new StringBuffer(32)
+ .append(getProperWhole()).append(' ')
+ .append(properNumerator).append('/')
+ .append(getDenominator()).toString();
+ }
+ } else {
+ toProperString = new StringBuffer(32)
+ .append(getNumerator()).append('/')
+ .append(getDenominator()).toString();
+ }
+ }
+ return toProperString;
+ }
+}
Propchange: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/math/Fraction.java
------------------------------------------------------------------------------
svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision
Added: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/math/IntRange.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/math/IntRange.java?rev=370807&view=auto
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/math/IntRange.java (added)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/math/IntRange.java Fri Jan 20 05:47:50 2006
@@ -0,0 +1,378 @@
+/*
+ * Copyright 2002-2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.mina.common.support.lang.math;
+
+import java.io.Serializable;
+
+/**
+ * <p><code>IntRange</code> represents an inclusive range of <code>int</code>s.</p>
+ *
+ * @author Stephen Colebourne
+ * @since 2.0
+ * @version $Id$
+ */
+public final class IntRange extends Range implements Serializable {
+
+ private static final long serialVersionUID = 71849363892730L;
+
+ /**
+ * The minimum number in this range (inclusive).
+ */
+ private final int min;
+ /**
+ * The maximum number in this range (inclusive).
+ */
+ private final int max;
+
+ /**
+ * Cached output minObject (class is immutable).
+ */
+ private transient Integer minObject = null;
+ /**
+ * Cached output maxObject (class is immutable).
+ */
+ private transient Integer maxObject = null;
+ /**
+ * Cached output hashCode (class is immutable).
+ */
+ private transient int hashCode = 0;
+ /**
+ * Cached output toString (class is immutable).
+ */
+ private transient String toString = null;
+
+ /**
+ * <p>Constructs a new <code>IntRange</code> using the specified
+ * number as both the minimum and maximum in this range.</p>
+ *
+ * @param number the number to use for this range
+ */
+ public IntRange(int number) {
+ super();
+ this.min = number;
+ this.max = number;
+ }
+
+ /**
+ * <p>Constructs a new <code>IntRange</code> using the specified
+ * number as both the minimum and maximum in this range.</p>
+ *
+ * @param number the number to use for this range, must not be <code>null</code>
+ * @throws IllegalArgumentException if the number is <code>null</code>
+ */
+ public IntRange(Number number) {
+ super();
+ if (number == null) {
+ throw new IllegalArgumentException("The number must not be null");
+ }
+ this.min = number.intValue();
+ this.max = number.intValue();
+ if (number instanceof Integer) {
+ this.minObject = (Integer) number;
+ this.maxObject = (Integer) number;
+ }
+ }
+
+ /**
+ * <p>Constructs a new <code>IntRange</code> with the specified
+ * minimum and maximum numbers (both inclusive).</p>
+ *
+ * <p>The arguments may be passed in the order (min,max) or (max,min). The
+ * getMinimum and getMaximum methods will return the correct values.</p>
+ *
+ * @param number1 first number that defines the edge of the range, inclusive
+ * @param number2 second number that defines the edge of the range, inclusive
+ */
+ public IntRange(int number1, int number2) {
+ super();
+ if (number2 < number1) {
+ this.min = number2;
+ this.max = number1;
+ } else {
+ this.min = number1;
+ this.max = number2;
+ }
+ }
+
+ /**
+ * <p>Constructs a new <code>IntRange</code> with the specified
+ * minimum and maximum numbers (both inclusive).</p>
+ *
+ * <p>The arguments may be passed in the order (min,max) or (max,min). The
+ * getMinimum and getMaximum methods will return the correct values.</p>
+ *
+ * @param number1 first number that defines the edge of the range, inclusive
+ * @param number2 second number that defines the edge of the range, inclusive
+ * @throws IllegalArgumentException if either number is <code>null</code>
+ */
+ public IntRange(Number number1, Number number2) {
+ super();
+ if (number1 == null || number2 == null) {
+ throw new IllegalArgumentException("The numbers must not be null");
+ }
+ int number1val = number1.intValue();
+ int number2val = number2.intValue();
+ if (number2val < number1val) {
+ this.min = number2val;
+ this.max = number1val;
+ if (number2 instanceof Integer) {
+ this.minObject = (Integer) number2;
+ }
+ if (number1 instanceof Integer) {
+ this.maxObject = (Integer) number1;
+ }
+ } else {
+ this.min = number1val;
+ this.max = number2val;
+ if (number1 instanceof Integer) {
+ this.minObject = (Integer) number1;
+ }
+ if (number2 instanceof Integer) {
+ this.maxObject = (Integer) number2;
+ }
+ }
+ }
+
+ // Accessors
+ //--------------------------------------------------------------------
+
+ /**
+ * <p>Returns the minimum number in this range.</p>
+ *
+ * @return the minimum number in this range
+ */
+ public Number getMinimumNumber() {
+ if (minObject == null) {
+ minObject = new Integer(min);
+ }
+ return minObject;
+ }
+
+ /**
+ * <p>Gets the minimum number in this range as a <code>long</code>.</p>
+ *
+ * @return the minimum number in this range
+ */
+ public long getMinimumLong() {
+ return min;
+ }
+
+ /**
+ * <p>Gets the minimum number in this range as a <code>int</code>.</p>
+ *
+ * @return the minimum number in this range
+ */
+ public int getMinimumInteger() {
+ return min;
+ }
+
+ /**
+ * <p>Gets the minimum number in this range as a <code>double</code>.</p>
+ *
+ * @return the minimum number in this range
+ */
+ public double getMinimumDouble() {
+ return min;
+ }
+
+ /**
+ * <p>Gets the minimum number in this range as a <code>float</code>.</p>
+ *
+ * @return the minimum number in this range
+ */
+ public float getMinimumFloat() {
+ return min;
+ }
+
+ /**
+ * <p>Returns the maximum number in this range.</p>
+ *
+ * @return the maximum number in this range
+ */
+ public Number getMaximumNumber() {
+ if (maxObject == null) {
+ maxObject = new Integer(max);
+ }
+ return maxObject;
+ }
+
+ /**
+ * <p>Gets the maximum number in this range as a <code>long</code>.</p>
+ *
+ * @return the maximum number in this range
+ */
+ public long getMaximumLong() {
+ return max;
+ }
+
+ /**
+ * <p>Gets the maximum number in this range as a <code>int</code>.</p>
+ *
+ * @return the maximum number in this range
+ */
+ public int getMaximumInteger() {
+ return max;
+ }
+
+ /**
+ * <p>Gets the maximum number in this range as a <code>double</code>.</p>
+ *
+ * @return the maximum number in this range
+ */
+ public double getMaximumDouble() {
+ return max;
+ }
+
+ /**
+ * <p>Gets the maximum number in this range as a <code>float</code>.</p>
+ *
+ * @return the maximum number in this range
+ */
+ public float getMaximumFloat() {
+ return max;
+ }
+
+ // Tests
+ //--------------------------------------------------------------------
+
+ /**
+ * <p>Tests whether the specified <code>number</code> occurs within
+ * this range using <code>int</code> comparison.</p>
+ *
+ * <p><code>null</code> is handled and returns <code>false</code>.</p>
+ *
+ * @param number the number to test, may be <code>null</code>
+ * @return <code>true</code> if the specified number occurs within this range
+ */
+ public boolean containsNumber(Number number) {
+ if (number == null) {
+ return false;
+ }
+ return containsInteger(number.intValue());
+ }
+
+ /**
+ * <p>Tests whether the specified <code>int</code> occurs within
+ * this range using <code>int</code> comparison.</p>
+ *
+ * <p>This implementation overrides the superclass for performance as it is
+ * the most common case.</p>
+ *
+ * @param value the int to test
+ * @return <code>true</code> if the specified number occurs within this
+ * range by <code>int</code> comparison
+ */
+ public boolean containsInteger(int value) {
+ return value >= min && value <= max;
+ }
+
+ // Range tests
+ //--------------------------------------------------------------------
+
+ /**
+ * <p>Tests whether the specified range occurs entirely within this range
+ * using <code>int</code> comparison.</p>
+ *
+ * <p><code>null</code> is handled and returns <code>false</code>.</p>
+ *
+ * @param range the range to test, may be <code>null</code>
+ * @return <code>true</code> if the specified range occurs entirely within this range
+ * @throws IllegalArgumentException if the range is not of this type
+ */
+ public boolean containsRange(Range range) {
+ if (range == null) {
+ return false;
+ }
+ return containsInteger(range.getMinimumInteger()) &&
+ containsInteger(range.getMaximumInteger());
+ }
+
+ /**
+ * <p>Tests whether the specified range overlaps with this range
+ * using <code>int</code> comparison.</p>
+ *
+ * <p><code>null</code> is handled and returns <code>false</code>.</p>
+ *
+ * @param range the range to test, may be <code>null</code>
+ * @return <code>true</code> if the specified range overlaps with this range
+ */
+ public boolean overlapsRange(Range range) {
+ if (range == null) {
+ return false;
+ }
+ return range.containsInteger(min) ||
+ range.containsInteger(max) ||
+ containsInteger(range.getMinimumInteger());
+ }
+
+ // Basics
+ //--------------------------------------------------------------------
+
+ /**
+ * <p>Compares this range to another object to test if they are equal.</p>.
+ *
+ * <p>To be equal, the class, minimum and maximum must be equal.</p>
+ *
+ * @param obj the reference object with which to compare
+ * @return <code>true</code> if this object is equal
+ */
+ public boolean equals(Object obj) {
+ if (obj == this) {
+ return true;
+ }
+ if (obj instanceof IntRange == false) {
+ return false;
+ }
+ IntRange range = (IntRange) obj;
+ return min == range.min && max == range.max;
+ }
+
+ /**
+ * <p>Gets a hashCode for the range.</p>
+ *
+ * @return a hash code value for this object
+ */
+ public int hashCode() {
+ if (hashCode == 0) {
+ hashCode = 17;
+ hashCode = 37 * hashCode + getClass().hashCode();
+ hashCode = 37 * hashCode + min;
+ hashCode = 37 * hashCode + max;
+ }
+ return hashCode;
+ }
+
+ /**
+ * <p>Gets the range as a <code>String</code>.</p>
+ *
+ * <p>The format of the String is 'Range[<i>min</i>,<i>max</i>]'.</p>
+ *
+ * @return the <code>String</code> representation of this range
+ */
+ public String toString() {
+ if (toString == null) {
+ StringBuffer buf = new StringBuffer(32);
+ buf.append("Range[");
+ buf.append(min);
+ buf.append(',');
+ buf.append(max);
+ buf.append(']');
+ toString = buf.toString();
+ }
+ return toString;
+ }
+
+}
Propchange: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/math/IntRange.java
------------------------------------------------------------------------------
svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision
Added: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/math/JVMRandom.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/math/JVMRandom.java?rev=370807&view=auto
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/math/JVMRandom.java (added)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/math/JVMRandom.java Fri Jan 20 05:47:50 2006
@@ -0,0 +1,160 @@
+/*
+ * Copyright 2002,2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.mina.common.support.lang.math;
+
+import java.util.Random;
+
+/**
+ * <p><code>JVMRandom</code> is a wrapper that supports all possible
+ * Random methods via the {@link java.lang.Math#random()} method
+ * and its system-wide {@link Random} object.</p>
+ *
+ * @author Henri Yandell
+ * @since 2.0
+ * @version $Id$
+ */
+public final class JVMRandom extends Random {
+
+ /**
+ * Ensures that only the constructor can call reseed.
+ */
+ private boolean constructed = false;
+
+ /**
+ * Constructs a new instance.
+ */
+ public JVMRandom() {
+ this.constructed = true;
+ }
+
+ /**
+ * Unsupported in 2.0.
+ *
+ * @param seed ignored
+ * @throws UnsupportedOperationException
+ */
+ public synchronized void setSeed(long seed) {
+ if (this.constructed) {
+ throw new UnsupportedOperationException();
+ }
+ }
+
+ /**
+ * Unsupported in 2.0.
+ *
+ * @return Nothing, this method always throws an UnsupportedOperationException.
+ * @throws UnsupportedOperationException
+ */
+ public synchronized double nextGaussian() {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * Unsupported in 2.0.
+ *
+ * @param byteArray ignored
+ * @throws UnsupportedOperationException
+ */
+ public void nextBytes(byte[] byteArray) {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * <p>Returns the next pseudorandom, uniformly distributed int value
+ * from the Math.random() sequence.</p>
+ *
+ * @return the random int
+ */
+ public int nextInt() {
+ return nextInt(Integer.MAX_VALUE);
+ }
+ /**
+ * <p>Returns a pseudorandom, uniformly distributed int value between
+ * <code>0</code> (inclusive) and the specified value (exclusive), from
+ * the Math.random() sequence.</p>
+ *
+ * @param n the specified exclusive max-value
+ * @return the random int
+ * @throws IllegalArgumentException when <code>n <= 0</code>
+ */
+ public int nextInt(int n) {
+ if (n <= 0) {
+ throw new IllegalArgumentException(
+ "Upper bound for nextInt must be positive"
+ );
+ }
+ // TODO: check this cannot return 'n'
+ return (int)(Math.random() * n);
+ }
+ /**
+ * <p>Returns the next pseudorandom, uniformly distributed long value
+ * from the Math.random() sequence.</p>
+ * @return the random long
+ */
+ public long nextLong() {
+ // possible loss of precision?
+ return nextLong(Long.MAX_VALUE);
+ }
+
+
+ /**
+ * <p>Returns a pseudorandom, uniformly distributed long value between
+ * <code>0</code> (inclusive) and the specified value (exclusive), from
+ * the Math.random() sequence.</p>
+ *
+ * @param n the specified exclusive max-value
+ * @return the random long
+ * @throws IllegalArgumentException when <code>n <= 0</code>
+ */
+ public static long nextLong(long n) {
+ if (n <= 0) {
+ throw new IllegalArgumentException(
+ "Upper bound for nextInt must be positive"
+ );
+ }
+ // TODO: check this cannot return 'n'
+ return (long)(Math.random() * n);
+ }
+
+ /**
+ * <p>Returns the next pseudorandom, uniformly distributed boolean value
+ * from the Math.random() sequence.</p>
+ *
+ * @return the random boolean
+ */
+ public boolean nextBoolean() {
+ return Math.random() > 0.5;
+ }
+ /**
+ * <p>Returns the next pseudorandom, uniformly distributed float value
+ * between <code>0.0</code> and <code>1.0</code> from the Math.random()
+ * sequence.</p>
+ *
+ * @return the random float
+ */
+ public float nextFloat() {
+ return (float)Math.random();
+ }
+ /**
+ * <p>Synonymous to the Math.random() call.</p>
+ *
+ * @return the random double
+ */
+ public double nextDouble() {
+ return Math.random();
+ }
+
+}
Propchange: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/math/JVMRandom.java
------------------------------------------------------------------------------
svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision
Added: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/math/LongRange.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/math/LongRange.java?rev=370807&view=auto
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/math/LongRange.java (added)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/math/LongRange.java Fri Jan 20 05:47:50 2006
@@ -0,0 +1,391 @@
+/*
+ * Copyright 2002-2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.mina.common.support.lang.math;
+
+import java.io.Serializable;
+
+/**
+ * <p><code>LongRange</code> represents an inclusive range of <code>long</code>s.</p>
+ *
+ * @author Stephen Colebourne
+ * @since 2.0
+ * @version $Id$
+ */
+public final class LongRange extends Range implements Serializable {
+
+ private static final long serialVersionUID = 71849363892720L;
+
+ /**
+ * The minimum number in this range (inclusive).
+ */
+ private final long min;
+ /**
+ * The maximum number in this range (inclusive).
+ */
+ private final long max;
+
+ /**
+ * Cached output minObject (class is immutable).
+ */
+ private transient Long minObject = null;
+ /**
+ * Cached output maxObject (class is immutable).
+ */
+ private transient Long maxObject = null;
+ /**
+ * Cached output hashCode (class is immutable).
+ */
+ private transient int hashCode = 0;
+ /**
+ * Cached output toString (class is immutable).
+ */
+ private transient String toString = null;
+
+ /**
+ * <p>Constructs a new <code>LongRange</code> using the specified
+ * number as both the minimum and maximum in this range.</p>
+ *
+ * @param number the number to use for this range
+ */
+ public LongRange(long number) {
+ super();
+ this.min = number;
+ this.max = number;
+ }
+
+ /**
+ * <p>Constructs a new <code>LongRange</code> using the specified
+ * number as both the minimum and maximum in this range.</p>
+ *
+ * @param number the number to use for this range, must not
+ * be <code>null</code>
+ * @throws IllegalArgumentException if the number is <code>null</code>
+ */
+ public LongRange(Number number) {
+ super();
+ if (number == null) {
+ throw new IllegalArgumentException("The number must not be null");
+ }
+ this.min = number.longValue();
+ this.max = number.longValue();
+ if (number instanceof Long) {
+ this.minObject = (Long) number;
+ this.maxObject = (Long) number;
+ }
+ }
+
+ /**
+ * <p>Constructs a new <code>LongRange</code> with the specified
+ * minimum and maximum numbers (both inclusive).</p>
+ *
+ * <p>The arguments may be passed in the order (min,max) or (max,min). The
+ * getMinimum and getMaximum methods will return the correct values.</p>
+ *
+ * @param number1 first number that defines the edge of the range, inclusive
+ * @param number2 second number that defines the edge of the range, inclusive
+ */
+ public LongRange(long number1, long number2) {
+ super();
+ if (number2 < number1) {
+ this.min = number2;
+ this.max = number1;
+ } else {
+ this.min = number1;
+ this.max = number2;
+ }
+ }
+
+ /**
+ * <p>Constructs a new <code>LongRange</code> with the specified
+ * minimum and maximum numbers (both inclusive).</p>
+ *
+ * <p>The arguments may be passed in the order (min,max) or (max,min). The
+ * getMinimum and getMaximum methods will return the correct values.</p>
+ *
+ * @param number1 first number that defines the edge of the range, inclusive
+ * @param number2 second number that defines the edge of the range, inclusive
+ * @throws IllegalArgumentException if either number is <code>null</code>
+ */
+ public LongRange(Number number1, Number number2) {
+ super();
+ if (number1 == null || number2 == null) {
+ throw new IllegalArgumentException("The numbers must not be null");
+ }
+ long number1val = number1.longValue();
+ long number2val = number2.longValue();
+ if (number2val < number1val) {
+ this.min = number2val;
+ this.max = number1val;
+ if (number2 instanceof Long) {
+ this.minObject = (Long) number2;
+ }
+ if (number1 instanceof Long) {
+ this.maxObject = (Long) number1;
+ }
+ } else {
+ this.min = number1val;
+ this.max = number2val;
+ if (number1 instanceof Long) {
+ this.minObject = (Long) number1;
+ }
+ if (number2 instanceof Long) {
+ this.maxObject = (Long) number2;
+ }
+ }
+ }
+
+ // Accessors
+ //--------------------------------------------------------------------
+
+ /**
+ * <p>Returns the minimum number in this range.</p>
+ *
+ * @return the minimum number in this range
+ */
+ public Number getMinimumNumber() {
+ if (minObject == null) {
+ minObject = new Long(min);
+ }
+ return minObject;
+ }
+
+ /**
+ * <p>Gets the minimum number in this range as a <code>long</code>.</p>
+ *
+ * @return the minimum number in this range
+ */
+ public long getMinimumLong() {
+ return min;
+ }
+
+ /**
+ * <p>Gets the minimum number in this range as a <code>int</code>.</p>
+ *
+ * <p>This conversion can lose information for large values.</p>
+ *
+ * @return the minimum number in this range
+ */
+ public int getMinimumInteger() {
+ return (int) min;
+ }
+
+ /**
+ * <p>Gets the minimum number in this range as a <code>double</code>.</p>
+ *
+ * <p>This conversion can lose information for large values.</p>
+ *
+ * @return the minimum number in this range
+ */
+ public double getMinimumDouble() {
+ return min;
+ }
+
+ /**
+ * <p>Gets the minimum number in this range as a <code>float</code>.</p>
+ *
+ * <p>This conversion can lose information for large values.</p>
+ *
+ * @return the minimum number in this range
+ */
+ public float getMinimumFloat() {
+ return min;
+ }
+
+ /**
+ * <p>Returns the maximum number in this range.</p>
+ *
+ * @return the maximum number in this range
+ */
+ public Number getMaximumNumber() {
+ if (maxObject == null) {
+ maxObject = new Long(max);
+ }
+ return maxObject;
+ }
+
+ /**
+ * <p>Gets the maximum number in this range as a <code>long</code>.</p>
+ *
+ * @return the maximum number in this range
+ */
+ public long getMaximumLong() {
+ return max;
+ }
+
+ /**
+ * <p>Gets the maximum number in this range cast to an <code>int</code>.</p>
+ *
+ * <p>This conversion can lose information for large values.</p>
+ *
+ * @return the maximum number in this range cast to an <code>int</code>.
+ */
+ public int getMaximumInteger() {
+ return (int) max;
+ }
+
+ /**
+ * <p>Gets the maximum number in this range as a <code>double</code>.</p>
+ *
+ * <p>This conversion can lose information for large values.</p>
+ *
+ * @return The maximum number in this range as a <code>double</code>.
+ */
+ public double getMaximumDouble() {
+ return max;
+ }
+
+ /**
+ * <p>Gets the maximum number in this range as a <code>float</code>.</p>
+ *
+ * <p>This conversion can lose information for large values.</p>
+ *
+ * @return The maximum number in this range as a <code>float</code>.
+ */
+ public float getMaximumFloat() {
+ return max;
+ }
+
+ // Tests
+ //--------------------------------------------------------------------
+
+ /**
+ * <p>Tests whether the specified <code>number</code> occurs within
+ * this range using <code>long</code> comparison.</p>
+ *
+ * <p><code>null</code> is handled and returns <code>false</code>.</p>
+ *
+ * @param number the number to test, may be <code>null</code>
+ * @return <code>true</code> if the specified number occurs within this range
+ */
+ public boolean containsNumber(Number number) {
+ if (number == null) {
+ return false;
+ }
+ return containsLong(number.longValue());
+ }
+
+ /**
+ * <p>Tests whether the specified <code>long</code> occurs within
+ * this range using <code>long</code> comparison.</p>
+ *
+ * <p>This implementation overrides the superclass for performance as it is
+ * the most common case.</p>
+ *
+ * @param value the long to test
+ * @return <code>true</code> if the specified number occurs within this
+ * range by <code>long</code> comparison
+ */
+ public boolean containsLong(long value) {
+ return value >= min && value <= max;
+ }
+
+ // Range tests
+ //--------------------------------------------------------------------
+
+ /**
+ * <p>Tests whether the specified range occurs entirely within this range
+ * using <code>long</code> comparison.</p>
+ *
+ * <p><code>null</code> is handled and returns <code>false</code>.</p>
+ *
+ * @param range the range to test, may be <code>null</code>
+ * @return <code>true</code> if the specified range occurs entirely within this range
+ * @throws IllegalArgumentException if the range is not of this type
+ */
+ public boolean containsRange(Range range) {
+ if (range == null) {
+ return false;
+ }
+ return containsLong(range.getMinimumLong()) &&
+ containsLong(range.getMaximumLong());
+ }
+
+ /**
+ * <p>Tests whether the specified range overlaps with this range
+ * using <code>long</code> comparison.</p>
+ *
+ * <p><code>null</code> is handled and returns <code>false</code>.</p>
+ *
+ * @param range the range to test, may be <code>null</code>
+ * @return <code>true</code> if the specified range overlaps with this range
+ */
+ public boolean overlapsRange(Range range) {
+ if (range == null) {
+ return false;
+ }
+ return range.containsLong(min) ||
+ range.containsLong(max) ||
+ containsLong(range.getMinimumLong());
+ }
+
+ // Basics
+ //--------------------------------------------------------------------
+
+ /**
+ * <p>Compares this range to another object to test if they are equal.</p>.
+ *
+ * <p>To be equal, the class, minimum and maximum must be equal.</p>
+ *
+ * @param obj the reference object with which to compare
+ * @return <code>true</code> if this object is equal
+ */
+ public boolean equals(Object obj) {
+ if (obj == this) {
+ return true;
+ }
+ if (obj instanceof LongRange == false) {
+ return false;
+ }
+ LongRange range = (LongRange) obj;
+ return min == range.min && max == range.max;
+ }
+
+ /**
+ * <p>Gets a hashCode for the range.</p>
+ *
+ * @return a hash code value for this object
+ */
+ public int hashCode() {
+ if (hashCode == 0) {
+ hashCode = 17;
+ hashCode = 37 * hashCode + getClass().hashCode();
+ hashCode = 37 * hashCode + ((int) (min ^ (min >> 32)));
+ hashCode = 37 * hashCode + ((int) (max ^ (max >> 32)));
+ }
+ return hashCode;
+ }
+
+ /**
+ * <p>Gets the range as a <code>String</code>.</p>
+ *
+ * <p>The format of the String is 'Range[<i>min</i>,<i>max</i>]'.</p>
+ *
+ * @return the <code>String</code> representation of this range
+ */
+ public String toString() {
+ if (toString == null) {
+ StringBuffer buf = new StringBuffer(32);
+ buf.append("Range[");
+ buf.append(min);
+ buf.append(',');
+ buf.append(max);
+ buf.append(']');
+ toString = buf.toString();
+ }
+ return toString;
+ }
+
+}
Propchange: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/math/LongRange.java
------------------------------------------------------------------------------
svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision
Added: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/math/NumberRange.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/math/NumberRange.java?rev=370807&view=auto
==============================================================================
--- directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/math/NumberRange.java (added)
+++ directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/math/NumberRange.java Fri Jan 20 05:47:50 2006
@@ -0,0 +1,236 @@
+/*
+ * Copyright 2002-2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.mina.common.support.lang.math;
+
+import java.io.Serializable;
+
+/**
+ * <p><code>NumberRange</code> represents an inclusive range of
+ * {@link java.lang.Number} objects of the same type.</p>
+ *
+ * @author <a href="mailto:chrise@esha.com">Christopher Elkins</a>
+ * @author Stephen Colebourne
+ * @since 2.0 (previously in org.apache.mina.common.support.lang)
+ * @version $Id$
+ */
+public final class NumberRange extends Range implements Serializable {
+
+ private static final long serialVersionUID = 71849363892710L;
+
+ /**
+ * The minimum number in this range.
+ */
+ private final Number min;
+ /**
+ * The maximum number in this range.
+ */
+ private final Number max;
+
+ /**
+ * Cached output hashCode (class is immutable).
+ */
+ private transient int hashCode = 0;
+ /**
+ * Cached output toString (class is immutable).
+ */
+ private transient String toString = null;
+
+ /**
+ * <p>Constructs a new <code>NumberRange</code> using the specified
+ * number as both the minimum and maximum in this range.</p>
+ *
+ * @param num the number to use for this range
+ * @throws IllegalArgumentException if the number is <code>null</code>
+ * @throws IllegalArgumentException if the number doesn't implement <code>Comparable</code>
+ * @throws IllegalArgumentException if the number is <code>Double.NaN</code> or <code>Float.NaN</code>
+ */
+ public NumberRange(Number num) {
+ if (num == null) {
+ throw new IllegalArgumentException("The number must not be null");
+ }
+ if (num instanceof Comparable == false) {
+ throw new IllegalArgumentException("The number must implement Comparable");
+ }
+ if (num instanceof Double && ((Double) num).isNaN()) {
+ throw new IllegalArgumentException("The number must not be NaN");
+ }
+ if (num instanceof Float && ((Float) num).isNaN()) {
+ throw new IllegalArgumentException("The number must not be NaN");
+ }
+
+ this.min = num;
+ this.max = num;
+ }
+
+ /**
+ * <p>Constructs a new <code>NumberRange</code> with the specified
+ * minimum and maximum numbers (both inclusive).</p>
+ *
+ * <p>The arguments may be passed in the order (min,max) or (max,min). The
+ * {@link #getMinimumNumber()} and {@link #getMaximumNumber()} methods will return the
+ * correct value.</p>
+ *
+ * <p>This constructor is designed to be used with two <code>Number</code>
+ * objects of the same type. If two objects of different types are passed in,
+ * an exception is thrown.</p>
+ *
+ * @param num1 first number that defines the edge of the range, inclusive
+ * @param num2 second number that defines the edge of the range, inclusive
+ * @throws IllegalArgumentException if either number is <code>null</code>
+ * @throws IllegalArgumentException if the numbers are of different types
+ * @throws IllegalArgumentException if the numbers don't implement <code>Comparable</code>
+ */
+ public NumberRange(Number num1, Number num2) {
+ if (num1 == null || num2 == null) {
+ throw new IllegalArgumentException("The numbers must not be null");
+ }
+ if (num1.getClass() != num2.getClass()) {
+ throw new IllegalArgumentException("The numbers must be of the same type");
+ }
+ if (num1 instanceof Comparable == false) {
+ throw new IllegalArgumentException("The numbers must implement Comparable");
+ }
+ if (num1 instanceof Double) {
+ if (((Double) num1).isNaN() || ((Double) num2).isNaN()) {
+ throw new IllegalArgumentException("The number must not be NaN");
+ }
+ } else if (num1 instanceof Float) {
+ if (((Float) num1).isNaN() || ((Float) num2).isNaN()) {
+ throw new IllegalArgumentException("The number must not be NaN");
+ }
+ }
+
+ int compare = ((Comparable) num1).compareTo(num2);
+ if (compare == 0) {
+ this.min = num1;
+ this.max = num1;
+ } else if (compare > 0) {
+ this.min = num2;
+ this.max = num1;
+ } else {
+ this.min = num1;
+ this.max = num2;
+ }
+ }
+
+ // Accessors
+ //--------------------------------------------------------------------
+
+ /**
+ * <p>Returns the minimum number in this range.</p>
+ *
+ * @return the minimum number in this range
+ */
+ public Number getMinimumNumber() {
+ return min;
+ }
+
+ /**
+ * <p>Returns the maximum number in this range.</p>
+ *
+ * @return the maximum number in this range
+ */
+ public Number getMaximumNumber() {
+ return max;
+ }
+
+ // Tests
+ //--------------------------------------------------------------------
+
+ /**
+ * <p>Tests whether the specified <code>number</code> occurs within
+ * this range.</p>
+ *
+ * <p><code>null</code> is handled and returns <code>false</code>.</p>
+ *
+ * @param number the number to test, may be <code>null</code>
+ * @return <code>true</code> if the specified number occurs within this range
+ * @throws IllegalArgumentException if the number is of a different type to the range
+ */
+ public boolean containsNumber(Number number) {
+ if (number == null) {
+ return false;
+ }
+ if (number.getClass() != min.getClass()) {
+ throw new IllegalArgumentException("The number must be of the same type as the range numbers");
+ }
+ int compareMin = ((Comparable) min).compareTo(number);
+ int compareMax = ((Comparable) max).compareTo(number);
+ return compareMin <= 0 && compareMax >= 0;
+ }
+
+ // Range tests
+ //--------------------------------------------------------------------
+ // use Range implementations
+
+ // Basics
+ //--------------------------------------------------------------------
+
+ /**
+ * <p>Compares this range to another object to test if they are equal.</p>.
+ *
+ * <p>To be equal, the class, minimum and maximum must be equal.</p>
+ *
+ * @param obj the reference object with which to compare
+ * @return <code>true</code> if this object is equal
+ */
+ public boolean equals(Object obj) {
+ if (obj == this) {
+ return true;
+ }
+ if (obj instanceof NumberRange == false) {
+ return false;
+ }
+ NumberRange range = (NumberRange) obj;
+ return min.equals(range.min) && max.equals(range.max);
+ }
+
+ /**
+ * <p>Gets a hashCode for the range.</p>
+ *
+ * @return a hash code value for this object
+ */
+ public int hashCode() {
+ if (hashCode == 0) {
+ hashCode = 17;
+ hashCode = 37 * hashCode + getClass().hashCode();
+ hashCode = 37 * hashCode + min.hashCode();
+ hashCode = 37 * hashCode + max.hashCode();
+ }
+ return hashCode;
+ }
+
+ /**
+ * <p>Gets the range as a <code>String</code>.</p>
+ *
+ * <p>The format of the String is 'Range[<i>min</i>,<i>max</i>]'.</p>
+ *
+ * @return the <code>String</code> representation of this range
+ */
+ public String toString() {
+ if (toString == null) {
+ StringBuffer buf = new StringBuffer(32);
+ buf.append("Range[");
+ buf.append(min);
+ buf.append(',');
+ buf.append(max);
+ buf.append(']');
+ toString = buf.toString();
+ }
+ return toString;
+ }
+
+}
Propchange: directory/sandbox/trustin/mina-spi/core/src/main/java/org/apache/mina/common/support/lang/math/NumberRange.java
------------------------------------------------------------------------------
svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision