Creation of math subpackage

Addition of Range class, and specific subclasses
Tests for Range


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137196 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2002-12-22 16:20:29 +00:00
parent 0018fa8273
commit 491b26ddd1
13 changed files with 3880 additions and 0 deletions

View File

@ -0,0 +1,435 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.lang.math;
import java.io.Serializable;
/**
* <p><code>DoubleRange</code> represents an inclusive range of <code>double</code>s.</p>
*
* @author Stephen Colebourne
* @since 2.0
* @version $Id: DoubleRange.java,v 1.1 2002/12/22 16:20:29 scolebourne Exp $
*/
public final class DoubleRange extends Range implements Serializable {
private static final long serialVersionUID = 71849363892740L;
/* The minimum number in this range (inclusive). */
private final double min;
/* The maximum number in this range (inclusive). */
private final double max;
/** Cached output minObject (class is immutable) */
private transient Double minObject = null;
/** Cached output maxObject (class is immutable) */
private transient Double 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>DoubleRange</code> using the specified
* number as both the minimum and maximum in this range.</p>
*
* @param number the number to use for this range
* @throws IllegalArgumentException if the number is <code>NaN</code>
*/
public DoubleRange(double number) {
super();
if (Double.isNaN(number)) {
throw new IllegalArgumentException("The number must not be NaN");
}
this.min = number;
this.max = number;
}
/**
* <p>Constructs a new <code>DoubleRange</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 null
* @throws IllegalArgumentException if the number is <code>null</code>
* @throws IllegalArgumentException if the number is <code>NaN</code>
*/
public DoubleRange(Number number) {
super();
if (number == null) {
throw new IllegalArgumentException("The number must not be null");
}
this.min = number.doubleValue();
this.max = number.doubleValue();
if (Double.isNaN(min) || Double.isNaN(max)) {
throw new IllegalArgumentException("The number must not be NaN");
}
if (number instanceof Double) {
this.minObject = (Double) number;
this.maxObject = (Double) number;
}
}
/**
* <p>Constructs a new <code>DoubleRange</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>NaN</code>
*/
public DoubleRange(double number1, double number2) {
super();
if (Double.isNaN(number1) || Double.isNaN(number2)) {
throw new IllegalArgumentException("The numbers must not be NaN");
}
if (number2 < number1) {
this.min = number2;
this.max = number1;
} else {
this.min = number1;
this.max = number2;
}
}
/**
* <p>Constructs a new <code>DoubleRange</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>
* @throws IllegalArgumentException if either number is <code>NaN</code>
*/
public DoubleRange(Number number1, Number number2) {
super();
if (number1 == null || number2 == null) {
throw new IllegalArgumentException("The numbers must not be null");
}
double number1val = number1.doubleValue();
double number2val = number2.doubleValue();
if (Double.isNaN(number1val) || Double.isNaN(number2val)) {
throw new IllegalArgumentException("The numbers must not be NaN");
}
if (number2val < number1val) {
this.min = number2val;
this.max = number1val;
if (number2 instanceof Double) {
this.minObject = (Double) number2;
}
if (number1 instanceof Double) {
this.maxObject = (Double) number1;
}
} else {
this.min = number1val;
this.max = number2val;
if (number1 instanceof Double) {
this.minObject = (Double) number1;
}
if (number2 instanceof Double) {
this.maxObject = (Double) 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 Double(min);
}
return minObject;
}
/**
* <p>Gets the minimum number in this range as a <code>long</code>.</p>
*
* <p>This conversion can lose information for large values or decimals.</p>
*
* @return the minimum number in this range
*/
public long getMinimumLong() {
return (long) min;
}
/**
* <p>Gets the minimum number in this range as a <code>int</code>.</p>
*
* <p>This conversion can lose information for large values or decimals.</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>
*
* @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 (float) 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 Double(max);
}
return maxObject;
}
/**
* <p>Gets the maximum number in this range as a <code>long</code>.</p>
*
* <p>This conversion can lose information for large values or decimals.</p>
*
* @return the maximum number in this range
*/
public long getMaximumLong() {
return (long) max;
}
/**
* <p>Gets the maximum number in this range as a <code>int</code>.</p>
*
* <p>This conversion can lose information for large values or decimals.</p>
*
* @return the maximum number in this range
*/
public int getMaximumInteger() {
return (int) 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>
*
* <p>This conversion can lose information for large values.</p>
*
* @return the maximum number in this range
*/
public float getMaximumFloat() {
return (float) max;
}
// Tests
//--------------------------------------------------------------------
/**
* <p>Tests whether the specified <code>number</code> occurs within
* this range using <code>double</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 includesNumber(Number number) {
if (number == null) {
return false;
}
return includesDouble(number.doubleValue());
}
/**
* <p>Tests whether the specified <code>double</code> occurs within
* this range using <code>double</code> comparison.</p>
*
* <p>This implementation overrides the superclass for performance as it is
* the most common case.</p>
*
* @param value the double to test
* @return <code>true</code> if the specified number occurs within this
* range by <code>double</code> comparison
*/
public boolean includesDouble(double value) {
return (value >= min && value <= max);
}
// Range tests
//--------------------------------------------------------------------
/**
* <p>Tests whether the specified range occurs entirely within this range
* using <code>double</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 includesRange(Range range) {
if (range == null) {
return false;
}
return includesDouble(range.getMinimumDouble()) &&
includesDouble(range.getMaximumDouble());
}
/**
* <p>Tests whether the specified range overlaps with this range
* using <code>double</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.includesDouble(min) ||
range.includesDouble(max) ||
includesDouble(range.getMinimumDouble());
}
// 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 DoubleRange == false) {
return false;
}
DoubleRange range = (DoubleRange) obj;
return (Double.doubleToLongBits(min) == Double.doubleToLongBits(range.min) &&
Double.doubleToLongBits(max) == Double.doubleToLongBits(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();
long lng = Double.doubleToLongBits(min);
hashCode = 37 * hashCode + ((int) (lng ^ (lng >> 32)));
lng = Double.doubleToLongBits(max);
hashCode = 37 * hashCode + ((int) (lng ^ (lng >> 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;
}
}

View File

@ -0,0 +1,429 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.lang.math;
import java.io.Serializable;
/**
* <p><code>FloatRange</code> represents an inclusive range of <code>float</code>s.</p>
*
* @author Stephen Colebourne
* @since 2.0
* @version $Id: FloatRange.java,v 1.1 2002/12/22 16:20:29 scolebourne Exp $
*/
public final class FloatRange extends Range implements Serializable {
private static final long serialVersionUID = 71849363892750L;
/* The minimum number in this range (inclusive). */
private final float min;
/* The maximum number in this range (inclusive). */
private final float max;
/** Cached output minObject (class is immutable) */
private transient Float minObject = null;
/** Cached output maxObject (class is immutable) */
private transient Float 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>FloatRange</code> using the specified
* number as both the minimum and maximum in this range.</p>
*
* @param number the number to use for this range
* @throws IllegalArgumentException if the number is <code>NaN</code>
*/
public FloatRange(float number) {
super();
if (Float.isNaN(number)) {
throw new IllegalArgumentException("The number must not be NaN");
}
this.min = number;
this.max = number;
}
/**
* <p>Constructs a new <code>FloatRange</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 null
* @throws IllegalArgumentException if the number is <code>null</code>
* @throws IllegalArgumentException if the number is <code>NaN</code>
*/
public FloatRange(Number number) {
super();
if (number == null) {
throw new IllegalArgumentException("The number must not be null");
}
this.min = number.floatValue();
this.max = number.floatValue();
if (Float.isNaN(min) || Float.isNaN(max)) {
throw new IllegalArgumentException("The number must not be NaN");
}
if (number instanceof Float) {
this.minObject = (Float) number;
this.maxObject = (Float) number;
}
}
/**
* <p>Constructs a new <code>FloatRange</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>NaN</code>
*/
public FloatRange(float number1, float number2) {
super();
if (Float.isNaN(number1) || Float.isNaN(number2)) {
throw new IllegalArgumentException("The numbers must not be NaN");
}
if (number2 < number1) {
this.min = number2;
this.max = number1;
} else {
this.min = number1;
this.max = number2;
}
}
/**
* <p>Constructs a new <code>FloatRange</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>
* @throws IllegalArgumentException if either number is <code>NaN</code>
*/
public FloatRange(Number number1, Number number2) {
super();
if (number1 == null || number2 == null) {
throw new IllegalArgumentException("The numbers must not be null");
}
float number1val = number1.floatValue();
float number2val = number2.floatValue();
if (Float.isNaN(number1val) || Float.isNaN(number2val)) {
throw new IllegalArgumentException("The numbers must not be NaN");
}
if (number2val < number1val) {
this.min = number2val;
this.max = number1val;
if (number2 instanceof Float) {
this.minObject = (Float) number2;
}
if (number1 instanceof Float) {
this.maxObject = (Float) number1;
}
} else {
this.min = number1val;
this.max = number2val;
if (number1 instanceof Float) {
this.minObject = (Float) number1;
}
if (number2 instanceof Float) {
this.maxObject = (Float) 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 Float(min);
}
return minObject;
}
/**
* <p>Gets the minimum number in this range as a <code>long</code>.</p>
*
* <p>This conversion can lose information for large values or decimals.</p>
*
* @return the minimum number in this range
*/
public long getMinimumLong() {
return (long) min;
}
/**
* <p>Gets the minimum number in this range as a <code>int</code>.</p>
*
* <p>This conversion can lose information for large values or decimals.</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>
*
* @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 Float(max);
}
return maxObject;
}
/**
* <p>Gets the maximum number in this range as a <code>long</code>.</p>
*
* <p>This conversion can lose information for large values or decimals.</p>
*
* @return the maximum number in this range
*/
public long getMaximumLong() {
return (long) max;
}
/**
* <p>Gets the maximum number in this range as a <code>int</code>.</p>
*
* <p>This conversion can lose information for large values or decimals.</p>
*
* @return the maximum number in this range
*/
public int getMaximumInteger() {
return (int) 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>float</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 includesNumber(Number number) {
if (number == null) {
return false;
}
return includesFloat(number.floatValue());
}
/**
* <p>Tests whether the specified <code>float</code> occurs within
* this range using <code>float</code> comparison.</p>
*
* <p>This implementation overrides the superclass for performance as it is
* the most common case.</p>
*
* @param value the float to test
* @return <code>true</code> if the specified number occurs within this
* range by <code>float</code> comparison
*/
public boolean includesFloat(float value) {
return (value >= min && value <= max);
}
// Range tests
//--------------------------------------------------------------------
/**
* <p>Tests whether the specified range occurs entirely within this range
* using <code>float</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 includesRange(Range range) {
if (range == null) {
return false;
}
return includesFloat(range.getMinimumFloat()) &&
includesFloat(range.getMaximumFloat());
}
/**
* <p>Tests whether the specified range overlaps with this range
* using <code>float</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.includesFloat(min) ||
range.includesFloat(max) ||
includesFloat(range.getMinimumFloat());
}
// 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 FloatRange == false) {
return false;
}
FloatRange range = (FloatRange) obj;
return (Float.floatToIntBits(min) == Float.floatToIntBits(range.min) &&
Float.floatToIntBits(max) == Float.floatToIntBits(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 + Float.floatToIntBits(min);
hashCode = 37 * hashCode + Float.floatToIntBits(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;
}
}

View File

@ -0,0 +1,404 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.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: IntRange.java,v 1.1 2002/12/22 16:20:29 scolebourne Exp $
*/
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 null
* @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 includesNumber(Number number) {
if (number == null) {
return false;
}
return includesInteger(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 includesInteger(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 includesRange(Range range) {
if (range == null) {
return false;
}
return includesInteger(range.getMinimumInteger()) &&
includesInteger(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.includesInteger(min) ||
range.includesInteger(max) ||
includesInteger(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;
}
}

View File

@ -0,0 +1,410 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.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: LongRange.java,v 1.1 2002/12/22 16:20:29 scolebourne Exp $
*/
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 null
* @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 as a <code>int</code>.</p>
*
* <p>This conversion can lose information for large values.</p>
*/
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>
*/
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>
*/
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 includesNumber(Number number) {
if (number == null) {
return false;
}
return includesLong(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 includesLong(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 includesRange(Range range) {
if (range == null) {
return false;
}
return includesLong(range.getMinimumLong()) &&
includesLong(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.includesLong(min) ||
range.includesLong(max) ||
includesLong(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;
}
}

View File

@ -0,0 +1,266 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.lang.math;
import java.io.Serializable;
/**
* <p><code>NumberRange</code> represents an inclusive range of
* {@link java.lang.Number 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.commons.lang)
* @version $Id: NumberRange.java,v 1.1 2002/12/22 16:20:29 scolebourne Exp $
*/
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 #getMinimum()} and {@link #getMaximum()} 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 includesNumber(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;
}
}

View File

@ -0,0 +1,470 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.lang.math;
import org.apache.commons.lang.NumberUtils;
/**
* <p><code>Range</code> represents a range of numbers of the same type.</p>
*
* <p>Specific subclasses hold the range values as different types. Each
* subclass should be immutable and {@link java.io.Serializable Serializable}
* if possible.</p>
*
* @author Stephen Colebourne
* @since 2.0
* @version $Id: Range.java,v 1.1 2002/12/22 16:20:29 scolebourne Exp $
*/
public abstract class Range {
/**
* <p>Constructs a new range.</p>
*/
public Range() {
super();
}
// Accessors
//--------------------------------------------------------------------
/**
* <p>Gets the minimum number in this range.</p>
*
* @return the minimum number in this range
*/
public abstract Number getMinimumNumber();
/**
* <p>Gets the minimum number in this range as a <code>long</code>.</p>
*
* <p>This implementation uses the {@link #getMinimumNumber()} method.
* Subclasses may be able to optimise this.</p>
*
* @return the minimum number in this range
*/
public long getMinimumLong() {
return getMinimumNumber().longValue();
}
/**
* <p>Gets the minimum number in this range as a <code>int</code>.</p>
*
* <p>This implementation uses the {@link #getMinimumNumber()} method.
* Subclasses may be able to optimise this.</p>
*
* @return the minimum number in this range
*/
public int getMinimumInteger() {
return getMinimumNumber().intValue();
}
/**
* <p>Gets the minimum number in this range as a <code>double</code>.</p>
*
* <p>This implementation uses the {@link #getMinimumNumber()} method.
* Subclasses may be able to optimise this.</p>
*
* @return the minimum number in this range
*/
public double getMinimumDouble() {
return getMinimumNumber().doubleValue();
}
/**
* <p>Gets the minimum number in this range as a <code>float</code>.</p>
*
* <p>This implementation uses the {@link #getMinimumNumber()} method.
* Subclasses may be able to optimise this.</p>
*
* @return the minimum number in this range
*/
public float getMinimumFloat() {
return getMinimumNumber().floatValue();
}
/**
* <p>Gets the maximum number in this range.</p>
*
* @return the maximum number in this range
*/
public abstract Number getMaximumNumber();
/**
* <p>Gets the maximum number in this range as a <code>long</code>.</p>
*
* <p>This implementation uses the {@link #getMaximumNumber()} method.
* Subclasses may be able to optimise this.</p>
*
* @return the maximum number in this range
*/
public long getMaximumLong() {
return getMaximumNumber().longValue();
}
/**
* <p>Gets the maximum number in this range as a <code>int</code>.</p>
*
* <p>This implementation uses the {@link #getMaximumNumber()} method.
* Subclasses may be able to optimise this.</p>
*
* @return the maximum number in this range
*/
public int getMaximumInteger() {
return getMaximumNumber().intValue();
}
/**
* <p>Gets the maximum number in this range as a <code>double</code>.</p>
*
* <p>This implementation uses the {@link #getMaximumNumber()} method.
* Subclasses may be able to optimise this.</p>
*
* @return the maximum number in this range
*/
public double getMaximumDouble() {
return getMaximumNumber().doubleValue();
}
/**
* <p>Gets the maximum number in this range as a <code>float</code>.</p>
*
* <p>This implementation uses the {@link #getMaximumNumber()} method.
* Subclasses may be able to optimise this.</p>
*
* @return the maximum number in this range
*/
public float getMaximumFloat() {
return getMaximumNumber().floatValue();
}
// Include tests
//--------------------------------------------------------------------
/**
* <p>Tests whether the specified <code>Number</code> occurs within
* this range.</p>
*
* <p>The exact comparison implementation varies by subclass. It is
* intended that an <code>int</code> specific subclass will compare 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
* @throws IllegalArgumentException if the <code>Number</code> cannot be compared
*/
public abstract boolean includesNumber(Number number);
/**
* <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>
*
* <p>This implementation forwards to the {@link #includesLong(long)} method.</p>
*
* @param value the long to test, may be <code>null</code>
* @return <code>true</code> if the specified number occurs within this
* range by <code>long</code> comparison
*/
public boolean includesLong(Number value) {
if (value == null) {
return false;
}
return includesLong(value.longValue());
}
/**
* <p>Tests whether the specified <code>long</code> occurs within
* this range using <code>long</code> comparison.</p>
*
* <p>This implementation uses the {@link #getMinimumLong()} and
* {@link #getMaximumLong()} methods and should be good for most uses.</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 includesLong(long value) {
return (value >= getMinimumLong() && value <= getMaximumLong());
}
/**
* <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>
*
* <p>This implementation forwards to the {@link #includesInteger(int)} method.</p>
*
* @param value the integer to test, may be <code>null</code>
* @return <code>true</code> if the specified number occurs within this
* range by <code>int</code> comparison
*/
public boolean includesInteger(Number value) {
if (value == null) {
return false;
}
return includesInteger(value.intValue());
}
/**
* <p>Tests whether the specified <code>int</code> occurs within
* this range using <code>int</code> comparison.</p>
*
* <p>This implementation uses the {@link #getMinimumInteger()} and
* {@link #getMaximumInteger()} methods and should be good for most uses.</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 includesInteger(int value) {
return (value >= getMinimumInteger() && value <= getMaximumInteger());
}
/**
* <p>Tests whether the specified <code>Number</code> occurs within
* this range using <code>double</code> comparison..</p>
*
* <p><code>null</code> is handled and returns <code>false</code>.</p>
*
* <p>This implementation forwards to the {@link #includesDouble(double)} method.</p>
*
* @param value the double to test, may be <code>null</code>
* @return <code>true</code> if the specified number occurs within this
* range by <code>double</code> comparison
*/
public boolean includesDouble(Number value) {
if (value == null) {
return false;
}
return includesDouble(value.doubleValue());
}
/**
* <p>Tests whether the specified <code>double</code> occurs within
* this range using <code>double</code> comparison.</p>
*
* <p>This implementation uses the {@link #getMinimumDouble()} and
* {@link #getMaximumDouble()} methods and should be good for most uses.</p>
*
* @param value the double to test
* @return <code>true</code> if the specified number occurs within this
* range by <code>double</code> comparison
*/
public boolean includesDouble(double value) {
int compareMin = NumberUtils.compare(getMinimumDouble(), value);
int compareMax = NumberUtils.compare(getMaximumDouble(), value);
return (compareMin <= 0 && compareMax >= 0);
}
/**
* <p>Tests whether the specified <code>Number</code> occurs within
* this range using <code>float</code> comparison.</p>
*
* <p><code>null</code> is handled and returns <code>false</code>.</p>
*
* <p>This implementation forwards to the {@link #includesFloat(float)} method.</p>
*
* @param value the float to test, may be <code>null</code>
* @return <code>true</code> if the specified number occurs within this
* range by <code>float</code> comparison
*/
public boolean includesFloat(Number value) {
if (value == null) {
return false;
}
return includesFloat(value.floatValue());
}
/**
* <p>Tests whether the specified <code>float</code> occurs within
* this range using <code>float</code> comparison.</p>
*
* <p>This implementation uses the {@link #getMinimumFloat()} and
* {@link #getMaximumFloat()} methods and should be good for most uses.</p>
*
* @param value the float to test
* @return <code>true</code> if the specified number occurs within this
* range by <code>float</code> comparison
*/
public boolean includesFloat(float value) {
int compareMin = NumberUtils.compare(getMinimumFloat(), value);
int compareMax = NumberUtils.compare(getMaximumFloat(), value);
return (compareMin <= 0 && compareMax >= 0);
}
// Range tests
//--------------------------------------------------------------------
/**
* <p>Tests whether the specified range occurs entirely within this range.</p>
*
* <p>The exact comparison implementation varies by subclass. It is
* intended that an <code>int</code> specific subclass will compare using
* <code>int</code> comparison.</p>
*
* <p><code>null</code> is handled and returns <code>false</code>.</p>
*
* <p>This implementation uses the {@link #includesNumber(Number)} method.
* Subclasses may be able to optimise this.</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; otherwise, <code>false</code>
* @throws IllegalArgumentException if the <code>Range</code> cannot be compared
*/
public boolean includesRange(Range range) {
if (range == null) {
return false;
}
return includesNumber(range.getMinimumNumber()) &&
includesNumber(range.getMaximumNumber());
}
/**
* <p>Tests whether the specified range overlaps with this range.</p>
*
* <p>The exact comparison implementation varies by subclass. It is
* intended that an <code>int</code> specific subclass will compare using
* <code>int</code> comparison.</p>
*
* <p><code>null</code> is handled and returns <code>false</code>.</p>
*
* <p>This implementation uses the {@link #includesNumber(Number)} and
* {@link #includesRange(Range)} methods.
* Subclasses may be able to optimise this.</p>
*
* @param range the range to test, may be <code>null</code>
* @return <code>true</code> if the specified range overlaps with this
* range; otherwise, <code>false</code>
* @throws IllegalArgumentException if the <code>Range</code> cannot be compared
*/
public boolean overlapsRange(Range range) {
if (range == null) {
return false;
}
return range.includesNumber(getMinimumNumber()) ||
range.includesNumber(getMaximumNumber()) ||
includesNumber(range.getMinimumNumber());
}
// 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>
*
* <p>This implementation uses the {@link #getMinimumNumber()} and
* {@link #getMaximumNumber()} methods.
* Subclasses may be able to optimise this.</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;
} else if (obj == null || obj.getClass() != getClass()) {
return false;
} else {
Range range = (Range) obj;
return getMinimumNumber().equals(range.getMinimumNumber()) &&
getMaximumNumber().equals(range.getMaximumNumber());
}
}
/**
* <p>Gets a hashCode for the range.</p>
*
* <p>This implementation uses the {@link #getMinimumNumber()} and
* {@link #getMaximumNumber()} methods.
* Subclasses may be able to optimise this.</p>
*
* @return a hash code value for this object
*/
public int hashCode() {
int result = 17;
result = 37 * result + getClass().hashCode();
result = 37 * result + getMinimumNumber().hashCode();
result = 37 * result + getMaximumNumber().hashCode();
return result;
}
/**
* <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>
*
* <p>This implementation uses the {@link #getMinimumNumber()} and
* {@link #getMaximumNumber()} methods.
* Subclasses may be able to optimise this.</p>
*
* @return the <code>String</code> representation of this range
*/
public String toString() {
StringBuffer buf = new StringBuffer(32);
buf.append("Range[");
buf.append(getMinimumNumber());
buf.append(',');
buf.append(getMaximumNumber());
buf.append(']');
return buf.toString();
}
}

View File

@ -0,0 +1,391 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.lang.math;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Test cases for the {@link Range} classes.
*
* @author Stephen Colebourne
* @version $Id: AbstractRangeTest.java,v 1.1 2002/12/22 16:20:29 scolebourne Exp $
*/
public abstract class AbstractRangeTest extends TestCase {
protected Range tenToTwenty;
protected Range otherRange;
protected Integer five;
protected Integer ten;
protected Integer twelve;
protected Integer fifteen;
protected Integer twenty;
protected Integer twentyFive;
protected Long long8;
protected Long long10;
protected Long long12;
protected Long long20;
protected Long long21;
protected Double double8;
protected Double double10;
protected Double double12;
protected Double double20;
protected Double double21;
protected Float float8;
protected Float float10;
protected Float float12;
protected Float float20;
protected Float float21;
private static class InnerNumber extends Number {
public double doubleValue() {
return 12d;
}
public float floatValue() {
return 12f;
}
public int intValue() {
return 12;
}
public long longValue() {
return 12L;
}
}
protected InnerNumber nonComparable = new InnerNumber();
public AbstractRangeTest(String name) {
super(name);
}
public void setUp() {
five = new Integer(5);
ten = new Integer(10);
twelve = new Integer(12);
fifteen = new Integer(15);
twenty = new Integer(20);
twentyFive = new Integer(25);
long8 = new Long(8);
long10 = new Long(10);
long12 = new Long(12);
long20 = new Long(20);
long21 = new Long(21);
double8 = new Double(8);
double10 = new Double(10);
double12 = new Double(12);
double20 = new Double(20);
double21 = new Double(21);
float8 = new Float(8);
float10 = new Float(10);
float12 = new Float(12);
float20 = new Float(20);
float21 = new Float(21);
}
//--------------------------------------------------------------------------
public void testGetMinimum() {
assertEquals(10L, tenToTwenty.getMinimumLong());
assertEquals(10, tenToTwenty.getMinimumInteger());
assertEquals(10d, tenToTwenty.getMinimumDouble(), 0.00001d);
assertEquals(10f, tenToTwenty.getMinimumFloat(), 0.00001f);
}
public void testGetMaximum() {
assertEquals(20L, tenToTwenty.getMaximumLong());
assertEquals(20, tenToTwenty.getMaximumInteger());
assertEquals(20d, tenToTwenty.getMaximumDouble(), 0.00001d);
assertEquals(20f, tenToTwenty.getMaximumFloat(), 0.00001f);
}
//--------------------------------------------------------------------------
public void testIncludesLong() {
assertEquals(false, tenToTwenty.includesLong(null));
assertEquals(true, tenToTwenty.includesLong(nonComparable));
assertEquals(false, tenToTwenty.includesLong(five));
assertEquals(true, tenToTwenty.includesLong(ten));
assertEquals(true, tenToTwenty.includesLong(fifteen));
assertEquals(true, tenToTwenty.includesLong(twenty));
assertEquals(false, tenToTwenty.includesLong(twentyFive));
assertEquals(false, tenToTwenty.includesLong(long8));
assertEquals(true, tenToTwenty.includesLong(long10));
assertEquals(true, tenToTwenty.includesLong(long12));
assertEquals(true, tenToTwenty.includesLong(long20));
assertEquals(false, tenToTwenty.includesLong(long21));
assertEquals(false, tenToTwenty.includesLong(double8));
assertEquals(true, tenToTwenty.includesLong(double10));
assertEquals(true, tenToTwenty.includesLong(double12));
assertEquals(true, tenToTwenty.includesLong(double20));
assertEquals(false, tenToTwenty.includesLong(double21));
assertEquals(false, tenToTwenty.includesLong(float8));
assertEquals(true, tenToTwenty.includesLong(float10));
assertEquals(true, tenToTwenty.includesLong(float12));
assertEquals(true, tenToTwenty.includesLong(float20));
assertEquals(false, tenToTwenty.includesLong(float21));
assertEquals(false, tenToTwenty.includesLong(9L));
assertEquals(true, tenToTwenty.includesLong(10L));
assertEquals(true, tenToTwenty.includesLong(15L));
assertEquals(true, tenToTwenty.includesLong(20L));
assertEquals(false, tenToTwenty.includesLong(21L));
}
public void testIncludesInteger() {
assertEquals(false, tenToTwenty.includesInteger(null));
assertEquals(true, tenToTwenty.includesInteger(nonComparable));
assertEquals(false, tenToTwenty.includesInteger(five));
assertEquals(true, tenToTwenty.includesInteger(ten));
assertEquals(true, tenToTwenty.includesInteger(fifteen));
assertEquals(true, tenToTwenty.includesInteger(twenty));
assertEquals(false, tenToTwenty.includesInteger(twentyFive));
assertEquals(false, tenToTwenty.includesInteger(long8));
assertEquals(true, tenToTwenty.includesInteger(long10));
assertEquals(true, tenToTwenty.includesInteger(long12));
assertEquals(true, tenToTwenty.includesInteger(long20));
assertEquals(false, tenToTwenty.includesInteger(long21));
assertEquals(false, tenToTwenty.includesInteger(double8));
assertEquals(true, tenToTwenty.includesInteger(double10));
assertEquals(true, tenToTwenty.includesInteger(double12));
assertEquals(true, tenToTwenty.includesInteger(double20));
assertEquals(false, tenToTwenty.includesInteger(double21));
assertEquals(false, tenToTwenty.includesInteger(float8));
assertEquals(true, tenToTwenty.includesInteger(float10));
assertEquals(true, tenToTwenty.includesInteger(float12));
assertEquals(true, tenToTwenty.includesInteger(float20));
assertEquals(false, tenToTwenty.includesInteger(float21));
assertEquals(false, tenToTwenty.includesInteger(9));
assertEquals(true, tenToTwenty.includesInteger(10));
assertEquals(true, tenToTwenty.includesInteger(15));
assertEquals(true, tenToTwenty.includesInteger(20));
assertEquals(false, tenToTwenty.includesInteger(21));
}
public void testIncludesDouble() {
assertEquals(false, tenToTwenty.includesDouble(null));
assertEquals(true, tenToTwenty.includesDouble(nonComparable));
assertEquals(false, tenToTwenty.includesDouble(five));
assertEquals(true, tenToTwenty.includesDouble(ten));
assertEquals(true, tenToTwenty.includesDouble(fifteen));
assertEquals(true, tenToTwenty.includesDouble(twenty));
assertEquals(false, tenToTwenty.includesDouble(twentyFive));
assertEquals(false, tenToTwenty.includesDouble(long8));
assertEquals(true, tenToTwenty.includesDouble(long10));
assertEquals(true, tenToTwenty.includesDouble(long12));
assertEquals(true, tenToTwenty.includesDouble(long20));
assertEquals(false, tenToTwenty.includesDouble(long21));
assertEquals(false, tenToTwenty.includesDouble(double8));
assertEquals(true, tenToTwenty.includesDouble(double10));
assertEquals(true, tenToTwenty.includesDouble(double12));
assertEquals(true, tenToTwenty.includesDouble(double20));
assertEquals(false, tenToTwenty.includesDouble(double21));
assertEquals(false, tenToTwenty.includesDouble(float8));
assertEquals(true, tenToTwenty.includesDouble(float10));
assertEquals(true, tenToTwenty.includesDouble(float12));
assertEquals(true, tenToTwenty.includesDouble(float20));
assertEquals(false, tenToTwenty.includesDouble(float21));
assertEquals(false, tenToTwenty.includesDouble(9d));
assertEquals(true, tenToTwenty.includesDouble(10d));
assertEquals(true, tenToTwenty.includesDouble(15d));
assertEquals(true, tenToTwenty.includesDouble(20d));
assertEquals(false, tenToTwenty.includesDouble(21d));
}
public void testIncludesFloat() {
assertEquals(false, tenToTwenty.includesFloat(null));
assertEquals(true, tenToTwenty.includesFloat(nonComparable));
assertEquals(false, tenToTwenty.includesFloat(five));
assertEquals(true, tenToTwenty.includesFloat(ten));
assertEquals(true, tenToTwenty.includesFloat(fifteen));
assertEquals(true, tenToTwenty.includesFloat(twenty));
assertEquals(false, tenToTwenty.includesFloat(twentyFive));
assertEquals(false, tenToTwenty.includesFloat(long8));
assertEquals(true, tenToTwenty.includesFloat(long10));
assertEquals(true, tenToTwenty.includesFloat(long12));
assertEquals(true, tenToTwenty.includesFloat(long20));
assertEquals(false, tenToTwenty.includesFloat(long21));
assertEquals(false, tenToTwenty.includesFloat(double8));
assertEquals(true, tenToTwenty.includesFloat(double10));
assertEquals(true, tenToTwenty.includesFloat(double12));
assertEquals(true, tenToTwenty.includesFloat(double20));
assertEquals(false, tenToTwenty.includesFloat(double21));
assertEquals(false, tenToTwenty.includesFloat(float8));
assertEquals(true, tenToTwenty.includesFloat(float10));
assertEquals(true, tenToTwenty.includesFloat(float12));
assertEquals(true, tenToTwenty.includesFloat(float20));
assertEquals(false, tenToTwenty.includesFloat(float21));
assertEquals(false, tenToTwenty.includesFloat(9f));
assertEquals(true, tenToTwenty.includesFloat(10f));
assertEquals(true, tenToTwenty.includesFloat(15f));
assertEquals(true, tenToTwenty.includesFloat(20f));
assertEquals(false, tenToTwenty.includesFloat(21f));
}
//--------------------------------------------------------------------------
public void testIncludesRange() {
assertEquals(false, tenToTwenty.includesRange(createRange(five, five)));
assertEquals(false, tenToTwenty.includesRange(createRange(five, ten)));
assertEquals(false, tenToTwenty.includesRange(createRange(five, twelve)));
assertEquals(false, tenToTwenty.includesRange(createRange(five, fifteen)));
assertEquals(false, tenToTwenty.includesRange(createRange(five, twenty)));
assertEquals(false, tenToTwenty.includesRange(createRange(five, twentyFive)));
assertEquals(true, tenToTwenty.includesRange(createRange(ten, ten)));
assertEquals(true, tenToTwenty.includesRange(createRange(ten, twelve)));
assertEquals(true, tenToTwenty.includesRange(createRange(ten, fifteen)));
assertEquals(true, tenToTwenty.includesRange(createRange(ten, twenty)));
assertEquals(false, tenToTwenty.includesRange(createRange(ten, twentyFive)));
assertEquals(true, tenToTwenty.includesRange(createRange(twelve, twelve)));
assertEquals(true, tenToTwenty.includesRange(createRange(twelve, fifteen)));
assertEquals(true, tenToTwenty.includesRange(createRange(twelve, twenty)));
assertEquals(false, tenToTwenty.includesRange(createRange(twelve, twentyFive)));
assertEquals(true, tenToTwenty.includesRange(createRange(fifteen, fifteen)));
assertEquals(true, tenToTwenty.includesRange(createRange(fifteen, twenty)));
assertEquals(false, tenToTwenty.includesRange(createRange(fifteen, twentyFive)));
assertEquals(true, tenToTwenty.includesRange(createRange(twenty, twenty)));
assertEquals(false, tenToTwenty.includesRange(createRange(twenty, twentyFive)));
assertEquals(false, tenToTwenty.includesRange(createRange(twentyFive, twentyFive)));
}
public void testOverlapsRange() {
assertEquals(false, tenToTwenty.overlapsRange(createRange(five, five)));
assertEquals(true, tenToTwenty.overlapsRange(createRange(five, ten)));
assertEquals(true, tenToTwenty.overlapsRange(createRange(five, twelve)));
assertEquals(true, tenToTwenty.overlapsRange(createRange(five, fifteen)));
assertEquals(true, tenToTwenty.overlapsRange(createRange(five, twenty)));
assertEquals(true, tenToTwenty.overlapsRange(createRange(five, twentyFive)));
assertEquals(true, tenToTwenty.overlapsRange(createRange(ten, ten)));
assertEquals(true, tenToTwenty.overlapsRange(createRange(ten, twelve)));
assertEquals(true, tenToTwenty.overlapsRange(createRange(ten, fifteen)));
assertEquals(true, tenToTwenty.overlapsRange(createRange(ten, twenty)));
assertEquals(true, tenToTwenty.overlapsRange(createRange(ten, twentyFive)));
assertEquals(true, tenToTwenty.overlapsRange(createRange(twelve, twelve)));
assertEquals(true, tenToTwenty.overlapsRange(createRange(twelve, fifteen)));
assertEquals(true, tenToTwenty.overlapsRange(createRange(twelve, twenty)));
assertEquals(true, tenToTwenty.overlapsRange(createRange(twelve, twentyFive)));
assertEquals(true, tenToTwenty.overlapsRange(createRange(fifteen, fifteen)));
assertEquals(true, tenToTwenty.overlapsRange(createRange(fifteen, twenty)));
assertEquals(true, tenToTwenty.overlapsRange(createRange(fifteen, twentyFive)));
assertEquals(true, tenToTwenty.overlapsRange(createRange(twenty, twenty)));
assertEquals(true, tenToTwenty.overlapsRange(createRange(twenty, twentyFive)));
assertEquals(false, tenToTwenty.overlapsRange(createRange(twentyFive, twentyFive)));
}
//--------------------------------------------------------------------------
public void testEquals() {
assertEquals(false, tenToTwenty.equals(createRange(ten, fifteen)));
assertEquals(false, tenToTwenty.equals(createRange(ten, twentyFive)));
assertEquals(false, tenToTwenty.equals(createRange(fifteen, twenty)));
assertEquals(false, tenToTwenty.equals(createRange(five, twenty)));
assertEquals(false, tenToTwenty.equals(createRange(five, ten)));
assertEquals(false, tenToTwenty.equals(createRange(ten)));
assertEquals(true, tenToTwenty.equals(createRange(ten, twenty)));
assertEquals(true, tenToTwenty.equals(createRange(twenty, ten)));
assertEquals(false, tenToTwenty.equals(null));
assertEquals(false, tenToTwenty.equals(new Object()));
assertEquals(false, tenToTwenty.equals(otherRange));
}
public void testHashCode() {
assertEquals(tenToTwenty.hashCode(), tenToTwenty.hashCode());
assertTrue(tenToTwenty.hashCode() != 0);
}
public void testToString() {
assertEquals("Range[10,20]", tenToTwenty.toString());
assertEquals("Range[-20,-10]", createRange(new Integer(-20), new Integer(-10)).toString());
}
protected abstract Range createRange(Integer integer);
protected abstract Range createRange(Integer integer1, Integer integer2);
}

View File

@ -0,0 +1,206 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.lang.math;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Test cases for the {@link DoubleRange} class.
*
* @author Stephen Colebourne
* @version $Id: DoubleRangeTest.java,v 1.1 2002/12/22 16:20:29 scolebourne Exp $
*/
public final class DoubleRangeTest extends AbstractRangeTest {
public DoubleRangeTest(String name) {
super(name);
}
public static Test suite() {
TestSuite suite = new TestSuite(DoubleRangeTest.class);
suite.setName("DoubleRange Tests");
return suite;
}
public void setUp() {
super.setUp();
tenToTwenty = new DoubleRange(double10, double20);
otherRange = new NumberRange(ten, twenty);
}
protected Range createRange(Integer integer1, Integer integer2) {
return new DoubleRange(integer1, integer2);
}
protected Range createRange(Integer integer) {
return new NumberRange(integer);
}
//--------------------------------------------------------------------------
public void testConstructor1a() {
DoubleRange nr = new DoubleRange(8d);
assertEquals(double8, nr.getMinimumNumber());
assertEquals(double8, nr.getMaximumNumber());
try {
new DoubleRange(Double.NaN);
fail();
} catch (IllegalArgumentException ex) {}
}
public void testConstructor1b() {
DoubleRange nr = new DoubleRange(double8);
assertSame(double8, nr.getMinimumNumber());
assertSame(double8, nr.getMaximumNumber());
Range r = new DoubleRange(nonComparable);
try {
new DoubleRange(null);
fail();
} catch (IllegalArgumentException ex) {}
try {
new DoubleRange(new Double(Double.NaN));
fail();
} catch (IllegalArgumentException ex) {}
}
public void testConstructor2a() {
DoubleRange nr = new DoubleRange(8d, 10d);
assertEquals(double8, nr.getMinimumNumber());
assertEquals(double10, nr.getMaximumNumber());
nr = new DoubleRange(10d, 8d);
assertEquals(double8, nr.getMinimumNumber());
assertEquals(double10, nr.getMaximumNumber());
try {
new DoubleRange(Double.NaN, 8d);
fail();
} catch (IllegalArgumentException ex) {}
}
public void testConstructor2b() {
DoubleRange nr = new DoubleRange(double8, double10);
assertSame(double8, nr.getMinimumNumber());
assertSame(double10, nr.getMaximumNumber());
nr = new DoubleRange(double10, double8);
assertSame(double8, nr.getMinimumNumber());
assertSame(double10, nr.getMaximumNumber());
nr = new DoubleRange(double8, double10);
assertSame(double8, nr.getMinimumNumber());
assertEquals(double10, nr.getMaximumNumber());
// not null
try {
new DoubleRange(double8, null);
fail();
} catch (IllegalArgumentException ex) {}
try {
new DoubleRange(null, double8);
fail();
} catch (IllegalArgumentException ex) {}
try {
new DoubleRange(null, null);
fail();
} catch (IllegalArgumentException ex) {}
try {
new DoubleRange(new Double(Double.NaN), double10);
fail();
} catch (IllegalArgumentException ex) {}
}
//--------------------------------------------------------------------------
public void testIncludesNumber() {
assertEquals(false, tenToTwenty.includesNumber(null));
assertEquals(true, tenToTwenty.includesNumber(nonComparable));
assertEquals(false, tenToTwenty.includesNumber(five));
assertEquals(true, tenToTwenty.includesNumber(ten));
assertEquals(true, tenToTwenty.includesNumber(fifteen));
assertEquals(true, tenToTwenty.includesNumber(twenty));
assertEquals(false, tenToTwenty.includesNumber(twentyFive));
assertEquals(false, tenToTwenty.includesNumber(long8));
assertEquals(true, tenToTwenty.includesNumber(long10));
assertEquals(true, tenToTwenty.includesNumber(long12));
assertEquals(true, tenToTwenty.includesNumber(long20));
assertEquals(false, tenToTwenty.includesNumber(long21));
assertEquals(false, tenToTwenty.includesNumber(double8));
assertEquals(true, tenToTwenty.includesNumber(double10));
assertEquals(true, tenToTwenty.includesNumber(double12));
assertEquals(true, tenToTwenty.includesNumber(double20));
assertEquals(false, tenToTwenty.includesNumber(double21));
assertEquals(false, tenToTwenty.includesNumber(float8));
assertEquals(true, tenToTwenty.includesNumber(float10));
assertEquals(true, tenToTwenty.includesNumber(float12));
assertEquals(true, tenToTwenty.includesNumber(float20));
assertEquals(false, tenToTwenty.includesNumber(float21));
}
public void testToString() {
assertEquals("Range[10.0,20.0]", tenToTwenty.toString());
assertEquals("Range[-20.0,-10.0]", createRange(new Integer(-20), new Integer(-10)).toString());
}
//--------------------------------------------------------------------------
}

View File

@ -0,0 +1,206 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.lang.math;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Test cases for the {@link FloatRange} class.
*
* @author Stephen Colebourne
* @version $Id: FloatRangeTest.java,v 1.1 2002/12/22 16:20:29 scolebourne Exp $
*/
public final class FloatRangeTest extends AbstractRangeTest {
public FloatRangeTest(String name) {
super(name);
}
public static Test suite() {
TestSuite suite = new TestSuite(FloatRangeTest.class);
suite.setName("FloatRange Tests");
return suite;
}
public void setUp() {
super.setUp();
tenToTwenty = new FloatRange(float10, float20);
otherRange = new NumberRange(ten, twenty);
}
protected Range createRange(Integer integer1, Integer integer2) {
return new FloatRange(integer1, integer2);
}
protected Range createRange(Integer integer) {
return new NumberRange(integer);
}
//--------------------------------------------------------------------------
public void testConstructor1a() {
FloatRange nr = new FloatRange(8f);
assertEquals(float8, nr.getMinimumNumber());
assertEquals(float8, nr.getMaximumNumber());
try {
new FloatRange(Float.NaN);
fail();
} catch (IllegalArgumentException ex) {}
}
public void testConstructor1b() {
FloatRange nr = new FloatRange(float8);
assertSame(float8, nr.getMinimumNumber());
assertSame(float8, nr.getMaximumNumber());
Range r = new FloatRange(nonComparable);
try {
new FloatRange(null);
fail();
} catch (IllegalArgumentException ex) {}
try {
new FloatRange(new Double(Double.NaN));
fail();
} catch (IllegalArgumentException ex) {}
}
public void testConstructor2a() {
FloatRange nr = new FloatRange(8f, 10f);
assertEquals(float8, nr.getMinimumNumber());
assertEquals(float10, nr.getMaximumNumber());
nr = new FloatRange(10f, 8f);
assertEquals(float8, nr.getMinimumNumber());
assertEquals(float10, nr.getMaximumNumber());
try {
new FloatRange(Float.NaN, 8f);
fail();
} catch (IllegalArgumentException ex) {}
}
public void testConstructor2b() {
FloatRange nr = new FloatRange(float8, float10);
assertSame(float8, nr.getMinimumNumber());
assertSame(float10, nr.getMaximumNumber());
nr = new FloatRange(float10, float8);
assertSame(float8, nr.getMinimumNumber());
assertSame(float10, nr.getMaximumNumber());
nr = new FloatRange(float8, float10);
assertSame(float8, nr.getMinimumNumber());
assertEquals(float10, nr.getMaximumNumber());
// not null
try {
new FloatRange(float8, null);
fail();
} catch (IllegalArgumentException ex) {}
try {
new FloatRange(null, float8);
fail();
} catch (IllegalArgumentException ex) {}
try {
new FloatRange(null, null);
fail();
} catch (IllegalArgumentException ex) {}
try {
new FloatRange(new Double(Double.NaN), float10);
fail();
} catch (IllegalArgumentException ex) {}
}
//--------------------------------------------------------------------------
public void testIncludesNumber() {
assertEquals(false, tenToTwenty.includesNumber(null));
assertEquals(true, tenToTwenty.includesNumber(nonComparable));
assertEquals(false, tenToTwenty.includesNumber(five));
assertEquals(true, tenToTwenty.includesNumber(ten));
assertEquals(true, tenToTwenty.includesNumber(fifteen));
assertEquals(true, tenToTwenty.includesNumber(twenty));
assertEquals(false, tenToTwenty.includesNumber(twentyFive));
assertEquals(false, tenToTwenty.includesNumber(long8));
assertEquals(true, tenToTwenty.includesNumber(long10));
assertEquals(true, tenToTwenty.includesNumber(long12));
assertEquals(true, tenToTwenty.includesNumber(long20));
assertEquals(false, tenToTwenty.includesNumber(long21));
assertEquals(false, tenToTwenty.includesNumber(double8));
assertEquals(true, tenToTwenty.includesNumber(double10));
assertEquals(true, tenToTwenty.includesNumber(double12));
assertEquals(true, tenToTwenty.includesNumber(double20));
assertEquals(false, tenToTwenty.includesNumber(double21));
assertEquals(false, tenToTwenty.includesNumber(float8));
assertEquals(true, tenToTwenty.includesNumber(float10));
assertEquals(true, tenToTwenty.includesNumber(float12));
assertEquals(true, tenToTwenty.includesNumber(float20));
assertEquals(false, tenToTwenty.includesNumber(float21));
}
public void testToString() {
assertEquals("Range[10.0,20.0]", tenToTwenty.toString());
assertEquals("Range[-20.0,-10.0]", createRange(new Integer(-20), new Integer(-10)).toString());
}
//--------------------------------------------------------------------------
}

View File

@ -0,0 +1,188 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.lang.math;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Test cases for the {@link IntRange} class.
*
* @author Stephen Colebourne
* @version $Id: IntRangeTest.java,v 1.1 2002/12/22 16:20:29 scolebourne Exp $
*/
public final class IntRangeTest extends AbstractRangeTest {
public IntRangeTest(String name) {
super(name);
}
public static Test suite() {
TestSuite suite = new TestSuite(IntRangeTest.class);
suite.setName("IntRange Tests");
return suite;
}
public void setUp() {
super.setUp();
tenToTwenty = new IntRange(ten, twenty);
otherRange = new NumberRange(ten, twenty);
}
protected Range createRange(Integer integer1, Integer integer2) {
return new IntRange(integer1, integer2);
}
protected Range createRange(Integer integer) {
return new NumberRange(integer);
}
//--------------------------------------------------------------------------
public void testConstructor1a() {
IntRange nr = new IntRange(5);
assertEquals(five, nr.getMinimumNumber());
assertEquals(five, nr.getMaximumNumber());
}
public void testConstructor1b() {
IntRange nr = new IntRange(five);
assertSame(five, nr.getMinimumNumber());
assertSame(five, nr.getMaximumNumber());
Range r = new IntRange(nonComparable);
try {
new IntRange(null);
fail();
} catch (IllegalArgumentException ex) {}
}
public void testConstructor2a() {
IntRange nr = new IntRange(5, 10);
assertEquals(five, nr.getMinimumNumber());
assertEquals(ten, nr.getMaximumNumber());
nr = new IntRange(5, 10);
assertEquals(five, nr.getMinimumNumber());
assertEquals(ten, nr.getMaximumNumber());
}
public void testConstructor2b() {
IntRange nr = new IntRange(five, ten);
assertSame(five, nr.getMinimumNumber());
assertSame(ten, nr.getMaximumNumber());
nr = new IntRange(ten, five);
assertSame(five, nr.getMinimumNumber());
assertSame(ten, nr.getMaximumNumber());
nr = new IntRange(five, long10);
assertSame(five, nr.getMinimumNumber());
assertEquals(ten, nr.getMaximumNumber());
// not null
try {
new IntRange(five, null);
fail();
} catch (IllegalArgumentException ex) {}
try {
new IntRange(null, five);
fail();
} catch (IllegalArgumentException ex) {}
try {
new IntRange(null, null);
fail();
} catch (IllegalArgumentException ex) {}
}
//--------------------------------------------------------------------------
public void testIncludesNumber() {
assertEquals(false, tenToTwenty.includesNumber(null));
assertEquals(true, tenToTwenty.includesNumber(nonComparable));
assertEquals(false, tenToTwenty.includesNumber(five));
assertEquals(true, tenToTwenty.includesNumber(ten));
assertEquals(true, tenToTwenty.includesNumber(fifteen));
assertEquals(true, tenToTwenty.includesNumber(twenty));
assertEquals(false, tenToTwenty.includesNumber(twentyFive));
assertEquals(false, tenToTwenty.includesNumber(long8));
assertEquals(true, tenToTwenty.includesNumber(long10));
assertEquals(true, tenToTwenty.includesNumber(long12));
assertEquals(true, tenToTwenty.includesNumber(long20));
assertEquals(false, tenToTwenty.includesNumber(long21));
assertEquals(false, tenToTwenty.includesNumber(double8));
assertEquals(true, tenToTwenty.includesNumber(double10));
assertEquals(true, tenToTwenty.includesNumber(double12));
assertEquals(true, tenToTwenty.includesNumber(double20));
assertEquals(false, tenToTwenty.includesNumber(double21));
assertEquals(false, tenToTwenty.includesNumber(float8));
assertEquals(true, tenToTwenty.includesNumber(float10));
assertEquals(true, tenToTwenty.includesNumber(float12));
assertEquals(true, tenToTwenty.includesNumber(float20));
assertEquals(false, tenToTwenty.includesNumber(float21));
}
public void testIncludesIntegerBig() {
IntRange big = new IntRange(Integer.MAX_VALUE, Integer.MAX_VALUE- 2);
assertEquals(true, big.includesInteger(Integer.MAX_VALUE - 1));
assertEquals(false, big.includesInteger(Integer.MAX_VALUE - 3));
}
//--------------------------------------------------------------------------
}

View File

@ -0,0 +1,188 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.lang.math;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Test cases for the {@link LongRange} class.
*
* @author Stephen Colebourne
* @version $Id: LongRangeTest.java,v 1.1 2002/12/22 16:20:29 scolebourne Exp $
*/
public final class LongRangeTest extends AbstractRangeTest {
public LongRangeTest(String name) {
super(name);
}
public static Test suite() {
TestSuite suite = new TestSuite(LongRangeTest.class);
suite.setName("LongRange Tests");
return suite;
}
public void setUp() {
super.setUp();
tenToTwenty = new LongRange(long10, long20);
otherRange = new NumberRange(ten, twenty);
}
protected Range createRange(Integer integer1, Integer integer2) {
return new LongRange(integer1, integer2);
}
protected Range createRange(Integer integer) {
return new NumberRange(integer);
}
//--------------------------------------------------------------------------
public void testConstructor1a() {
LongRange nr = new LongRange(8L);
assertEquals(long8, nr.getMinimumNumber());
assertEquals(long8, nr.getMaximumNumber());
}
public void testConstructor1b() {
LongRange nr = new LongRange(long8);
assertSame(long8, nr.getMinimumNumber());
assertSame(long8, nr.getMaximumNumber());
Range r = new LongRange(nonComparable);
try {
new LongRange(null);
fail();
} catch (IllegalArgumentException ex) {}
}
public void testConstructor2a() {
LongRange nr = new LongRange(8L, 10L);
assertEquals(long8, nr.getMinimumNumber());
assertEquals(long10, nr.getMaximumNumber());
nr = new LongRange(10L, 8L);
assertEquals(long8, nr.getMinimumNumber());
assertEquals(long10, nr.getMaximumNumber());
}
public void testConstructor2b() {
LongRange nr = new LongRange(long8, long10);
assertSame(long8, nr.getMinimumNumber());
assertSame(long10, nr.getMaximumNumber());
nr = new LongRange(long10, long8);
assertSame(long8, nr.getMinimumNumber());
assertSame(long10, nr.getMaximumNumber());
nr = new LongRange(long8, long10);
assertSame(long8, nr.getMinimumNumber());
assertEquals(long10, nr.getMaximumNumber());
// not null
try {
new LongRange(long8, null);
fail();
} catch (IllegalArgumentException ex) {}
try {
new LongRange(null, long8);
fail();
} catch (IllegalArgumentException ex) {}
try {
new LongRange(null, null);
fail();
} catch (IllegalArgumentException ex) {}
}
//--------------------------------------------------------------------------
public void testIncludesNumber() {
assertEquals(false, tenToTwenty.includesNumber(null));
assertEquals(true, tenToTwenty.includesNumber(nonComparable));
assertEquals(false, tenToTwenty.includesNumber(five));
assertEquals(true, tenToTwenty.includesNumber(ten));
assertEquals(true, tenToTwenty.includesNumber(fifteen));
assertEquals(true, tenToTwenty.includesNumber(twenty));
assertEquals(false, tenToTwenty.includesNumber(twentyFive));
assertEquals(false, tenToTwenty.includesNumber(long8));
assertEquals(true, tenToTwenty.includesNumber(long10));
assertEquals(true, tenToTwenty.includesNumber(long12));
assertEquals(true, tenToTwenty.includesNumber(long20));
assertEquals(false, tenToTwenty.includesNumber(long21));
assertEquals(false, tenToTwenty.includesNumber(double8));
assertEquals(true, tenToTwenty.includesNumber(double10));
assertEquals(true, tenToTwenty.includesNumber(double12));
assertEquals(true, tenToTwenty.includesNumber(double20));
assertEquals(false, tenToTwenty.includesNumber(double21));
assertEquals(false, tenToTwenty.includesNumber(float8));
assertEquals(true, tenToTwenty.includesNumber(float10));
assertEquals(true, tenToTwenty.includesNumber(float12));
assertEquals(true, tenToTwenty.includesNumber(float20));
assertEquals(false, tenToTwenty.includesNumber(float21));
}
public void testIncludesLongBig() {
LongRange big = new LongRange(Long.MAX_VALUE, Long.MAX_VALUE- 2);
assertEquals(true, big.includesLong(Long.MAX_VALUE - 1));
assertEquals(false, big.includesLong(Long.MAX_VALUE - 3));
}
//--------------------------------------------------------------------------
}

View File

@ -0,0 +1,95 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.lang.math;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
/**
* Test suite for the Math package.
*
* @author Stephen Colebourne
* @version $Id: MathTestSuite.java,v 1.1 2002/12/22 16:20:29 scolebourne Exp $
*/
public class MathTestSuite extends TestCase {
/**
* Construct a new instance.
*/
public MathTestSuite(String name) {
super(name);
}
/**
* Command-line interface.
*/
public static void main(String[] args) {
TestRunner.run(suite());
}
/**
* Get the suite of tests
*/
public static Test suite() {
TestSuite suite = new TestSuite();
suite.setName("Commons-Lang-Math Tests");
suite.addTest(DoubleRangeTest.suite());
suite.addTest(FloatRangeTest.suite());
suite.addTest(IntRangeTest.suite());
suite.addTest(LongRangeTest.suite());
suite.addTest(NumberRangeTest.suite());
return suite;
}
}

View File

@ -0,0 +1,192 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.lang.math;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Test cases for the {@link NumberRange} class.
*
* @author <a href="mailto:chrise@esha.com">Christopher Elkins</a>
* @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
* @author Stephen Colebourne
* @version $Id: NumberRangeTest.java,v 1.1 2002/12/22 16:20:29 scolebourne Exp $
*/
public final class NumberRangeTest extends AbstractRangeTest {
public NumberRangeTest(String name) {
super(name);
}
public static Test suite() {
TestSuite suite = new TestSuite(NumberRangeTest.class);
suite.setName("NumberRange Tests");
return suite;
}
public void setUp() {
super.setUp();
tenToTwenty = new NumberRange(ten, twenty);
otherRange = new IntRange(ten, twenty);
}
protected Range createRange(Integer integer1, Integer integer2) {
return new NumberRange(integer1, integer2);
}
protected Range createRange(Integer integer) {
return new NumberRange(integer);
}
//--------------------------------------------------------------------------
public void testConstructor1() {
NumberRange nr = new NumberRange(five);
assertSame(five, nr.getMinimumNumber());
assertSame(five, nr.getMaximumNumber());
try {
new NumberRange(null);
fail();
} catch (IllegalArgumentException ex) {}
try {
new NumberRange(nonComparable);
fail();
} catch (IllegalArgumentException ex) {}
}
public void testConstructor2() {
NumberRange nr = new NumberRange(five, ten);
assertSame(five, nr.getMinimumNumber());
assertSame(ten, nr.getMaximumNumber());
nr = new NumberRange(ten, five);
assertSame(five, nr.getMinimumNumber());
assertSame(ten, nr.getMaximumNumber());
// not null
try {
new NumberRange(five, null);
fail();
} catch (IllegalArgumentException ex) {}
try {
new NumberRange(null, five);
fail();
} catch (IllegalArgumentException ex) {}
try {
new NumberRange(null, null);
fail();
} catch (IllegalArgumentException ex) {}
// no mixed types
try {
new NumberRange(five, long21);
fail();
} catch (IllegalArgumentException ex) {}
// must be comparable
try {
new NumberRange(nonComparable, nonComparable);
fail();
} catch (IllegalArgumentException ex) {}
// no double NaN
try {
new NumberRange(new Double(0), new Double(Double.NaN));
fail();
} catch (IllegalArgumentException ex) {}
try {
new NumberRange(new Double(Double.NaN), new Double(0));
fail();
} catch (IllegalArgumentException ex) {}
// no float NaN
try {
new NumberRange(new Float(0), new Float(Float.NaN));
fail();
} catch (IllegalArgumentException ex) {}
try {
new NumberRange(new Float(Float.NaN), new Float(0));
fail();
} catch (IllegalArgumentException ex) {}
}
//--------------------------------------------------------------------------
public void testIncludesNumber() {
assertEquals(false, tenToTwenty.includesNumber(null));
assertEquals(false, tenToTwenty.includesNumber(five));
assertEquals(true, tenToTwenty.includesNumber(ten));
assertEquals(true, tenToTwenty.includesNumber(fifteen));
assertEquals(true, tenToTwenty.includesNumber(twenty));
assertEquals(false, tenToTwenty.includesNumber(twentyFive));
try {
tenToTwenty.includesNumber(long21);
fail();
} catch (IllegalArgumentException ex) {}
}
public void testIncludesLongBig() {
// original NumberRange class failed this test
NumberRange big = new NumberRange(new Long(Long.MAX_VALUE), new Long(Long.MAX_VALUE- 2));
assertEquals(true, big.includesLong(Long.MAX_VALUE - 1));
assertEquals(false, big.includesLong(Long.MAX_VALUE - 3));
}
//--------------------------------------------------------------------------
}