Removing old Range classes per LANG-551. New lang.Range class replaces these

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@835780 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2009-11-13 08:54:04 +00:00
parent 452a6c0cd8
commit 56e830a235
13 changed files with 0 additions and 3757 deletions

View File

@ -1,435 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang.math;
import java.io.Serializable;
/**
* <p><code>DoubleRange</code> represents an inclusive range of <code>double</code>s.</p>
*
* @author Apache Software Foundation
* @since 2.0
* @version $Id$
*/
public final class DoubleRange extends Range implements Serializable {
/**
* Required for serialization support.
*
* @see java.io.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 <code>null</code>
* @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
*/
@Override
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
*/
@Override
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
*/
@Override
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
*/
@Override
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
*/
@Override
public float getMinimumFloat() {
return (float) min;
}
/**
* <p>Returns the maximum number in this range.</p>
*
* @return the maximum number in this range
*/
@Override
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
*/
@Override
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
*/
@Override
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
*/
@Override
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
*/
@Override
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
*/
@Override
public boolean containsNumber(Number number) {
if (number == null) {
return false;
}
return containsDouble(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
*/
@Override
public boolean containsDouble(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
*/
@Override
public boolean containsRange(Range range) {
if (range == null) {
return false;
}
return containsDouble(range.getMinimumDouble())
&& containsDouble(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
*/
@Override
public boolean overlapsRange(Range range) {
if (range == null) {
return false;
}
return range.containsDouble(min)
|| range.containsDouble(max)
|| containsDouble(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
*/
@Override
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
*/
@Override
public int hashCode() {
int temp = hashCode;
if (temp == 0) {
temp = 17;
temp = 37 * temp + getClass().hashCode();
long lng = Double.doubleToLongBits(min);
temp = 37 * temp + ((int) (lng ^ (lng >> 32)));
lng = Double.doubleToLongBits(max);
temp = 37 * temp + ((int) (lng ^ (lng >> 32)));
hashCode = temp;
}
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
*/
@Override
public String toString() {
if (toString == null) {
StringBuilder buf = new StringBuilder(32);
buf.append("Range[");
buf.append(min);
buf.append(',');
buf.append(max);
buf.append(']');
toString = buf.toString();
}
return toString;
}
}

View File

@ -1,429 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang.math;
import java.io.Serializable;
/**
* <p><code>FloatRange</code> represents an inclusive range of <code>float</code>s.</p>
*
* @author Apache Software Foundation
* @since 2.0
* @version $Id$
*/
public final class FloatRange extends Range implements Serializable {
/**
* Required for serialization support.
*
* @see java.io.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 <code>null</code>
* @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
*/
@Override
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
*/
@Override
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
*/
@Override
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
*/
@Override
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
*/
@Override
public float getMinimumFloat() {
return min;
}
/**
* <p>Returns the maximum number in this range.</p>
*
* @return the maximum number in this range
*/
@Override
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
*/
@Override
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
*/
@Override
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
*/
@Override
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
*/
@Override
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
*/
@Override
public boolean containsNumber(Number number) {
if (number == null) {
return false;
}
return containsFloat(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
*/
@Override
public boolean containsFloat(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
*/
@Override
public boolean containsRange(Range range) {
if (range == null) {
return false;
}
return containsFloat(range.getMinimumFloat()) &&
containsFloat(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
*/
@Override
public boolean overlapsRange(Range range) {
if (range == null) {
return false;
}
return range.containsFloat(min) ||
range.containsFloat(max) ||
containsFloat(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
*/
@Override
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
*/
@Override
public int hashCode() {
int temp = hashCode;
if (temp == 0) {
temp = 17;
temp = 37 * temp + getClass().hashCode();
temp = 37 * temp + Float.floatToIntBits(min);
temp = 37 * temp + Float.floatToIntBits(max);
hashCode = temp;
}
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
*/
@Override
public String toString() {
if (toString == null) {
StringBuilder buf = new StringBuilder(32);
buf.append("Range[");
buf.append(min);
buf.append(',');
buf.append(max);
buf.append(']');
toString = buf.toString();
}
return toString;
}
}

View File

@ -1,417 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang.math;
import java.io.Serializable;
/**
* <p><code>IntRange</code> represents an inclusive range of <code>int</code>s.</p>
*
* @author Apache Software Foundation
* @since 2.0
* @version $Id$
*/
public final class IntRange extends Range implements Serializable {
/**
* Required for serialization support.
*
* @see java.io.Serializable
*/
private static final long serialVersionUID = 71849363892730L;
/**
* The minimum number in this range (inclusive).
*/
private final int min;
/**
* The maximum number in this range (inclusive).
*/
private final int max;
/**
* Cached output minObject (class is immutable).
*/
private transient Integer minObject = null;
/**
* Cached output maxObject (class is immutable).
*/
private transient Integer maxObject = null;
/**
* Cached output hashCode (class is immutable).
*/
private transient int hashCode = 0;
/**
* Cached output toString (class is immutable).
*/
private transient String toString = null;
/**
* <p>Constructs a new <code>IntRange</code> using the specified
* number as both the minimum and maximum in this range.</p>
*
* @param number the number to use for this range
*/
public IntRange(int number) {
super();
this.min = number;
this.max = number;
}
/**
* <p>Constructs a new <code>IntRange</code> using the specified
* number as both the minimum and maximum in this range.</p>
*
* @param number the number to use for this range, must not be <code>null</code>
* @throws IllegalArgumentException if the number is <code>null</code>
*/
public IntRange(Number number) {
super();
if (number == null) {
throw new IllegalArgumentException("The number must not be null");
}
this.min = number.intValue();
this.max = number.intValue();
if (number instanceof Integer) {
this.minObject = (Integer) number;
this.maxObject = (Integer) number;
}
}
/**
* <p>Constructs a new <code>IntRange</code> with the specified
* minimum and maximum numbers (both inclusive).</p>
*
* <p>The arguments may be passed in the order (min,max) or (max,min). The
* getMinimum and getMaximum methods will return the correct values.</p>
*
* @param number1 first number that defines the edge of the range, inclusive
* @param number2 second number that defines the edge of the range, inclusive
*/
public IntRange(int number1, int number2) {
super();
if (number2 < number1) {
this.min = number2;
this.max = number1;
} else {
this.min = number1;
this.max = number2;
}
}
/**
* <p>Constructs a new <code>IntRange</code> with the specified
* minimum and maximum numbers (both inclusive).</p>
*
* <p>The arguments may be passed in the order (min,max) or (max,min). The
* getMinimum and getMaximum methods will return the correct values.</p>
*
* @param number1 first number that defines the edge of the range, inclusive
* @param number2 second number that defines the edge of the range, inclusive
* @throws IllegalArgumentException if either number is <code>null</code>
*/
public IntRange(Number number1, Number number2) {
super();
if (number1 == null || number2 == null) {
throw new IllegalArgumentException("The numbers must not be null");
}
int number1val = number1.intValue();
int number2val = number2.intValue();
if (number2val < number1val) {
this.min = number2val;
this.max = number1val;
if (number2 instanceof Integer) {
this.minObject = (Integer) number2;
}
if (number1 instanceof Integer) {
this.maxObject = (Integer) number1;
}
} else {
this.min = number1val;
this.max = number2val;
if (number1 instanceof Integer) {
this.minObject = (Integer) number1;
}
if (number2 instanceof Integer) {
this.maxObject = (Integer) number2;
}
}
}
// Accessors
//--------------------------------------------------------------------
/**
* <p>Returns the minimum number in this range.</p>
*
* @return the minimum number in this range
*/
@Override
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
*/
@Override
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
*/
@Override
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
*/
@Override
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
*/
@Override
public float getMinimumFloat() {
return min;
}
/**
* <p>Returns the maximum number in this range.</p>
*
* @return the maximum number in this range
*/
@Override
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
*/
@Override
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
*/
@Override
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
*/
@Override
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
*/
@Override
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
*/
@Override
public boolean containsNumber(Number number) {
if (number == null) {
return false;
}
return containsInteger(number.intValue());
}
/**
* <p>Tests whether the specified <code>int</code> occurs within
* this range using <code>int</code> comparison.</p>
*
* <p>This implementation overrides the superclass for performance as it is
* the most common case.</p>
*
* @param value the int to test
* @return <code>true</code> if the specified number occurs within this
* range by <code>int</code> comparison
*/
@Override
public boolean containsInteger(int value) {
return value >= min && value <= max;
}
// Range tests
//--------------------------------------------------------------------
/**
* <p>Tests whether the specified range occurs entirely within this range
* using <code>int</code> comparison.</p>
*
* <p><code>null</code> is handled and returns <code>false</code>.</p>
*
* @param range the range to test, may be <code>null</code>
* @return <code>true</code> if the specified range occurs entirely within this range
* @throws IllegalArgumentException if the range is not of this type
*/
@Override
public boolean containsRange(Range range) {
if (range == null) {
return false;
}
return containsInteger(range.getMinimumInteger()) &&
containsInteger(range.getMaximumInteger());
}
/**
* <p>Tests whether the specified range overlaps with this range
* using <code>int</code> comparison.</p>
*
* <p><code>null</code> is handled and returns <code>false</code>.</p>
*
* @param range the range to test, may be <code>null</code>
* @return <code>true</code> if the specified range overlaps with this range
*/
@Override
public boolean overlapsRange(Range range) {
if (range == null) {
return false;
}
return range.containsInteger(min) ||
range.containsInteger(max) ||
containsInteger(range.getMinimumInteger());
}
// Basics
//--------------------------------------------------------------------
/**
* <p>Compares this range to another object to test if they are equal.</p>.
*
* <p>To be equal, the class, minimum and maximum must be equal.</p>
*
* @param obj the reference object with which to compare
* @return <code>true</code> if this object is equal
*/
@Override
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
*/
@Override
public int hashCode() {
int temp = hashCode;
if (temp == 0) {
temp = 17;
temp = 37 * temp + getClass().hashCode();
temp = 37 * temp + min;
temp = 37 * temp + max;
hashCode = temp;
}
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
*/
@Override
public String toString() {
if (toString == null) {
StringBuilder buf = new StringBuilder(32);
buf.append("Range[");
buf.append(min);
buf.append(',');
buf.append(max);
buf.append(']');
toString = buf.toString();
}
return toString;
}
/**
* <p>Returns an array containing all the integer values in the range.</p>
*
* @return the <code>int[]</code> representation of this range
* @since 2.4
*/
public int[] toArray() {
int[] array = new int[max - min + 1];
for (int i = 0; i < array.length; i++) {
array[i] = min + i;
}
return array;
}
}

View File

@ -1,430 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang.math;
import java.io.Serializable;
/**
* <p><code>LongRange</code> represents an inclusive range of <code>long</code>s.</p>
*
* @author Apache Software Foundation
* @since 2.0
* @version $Id$
*/
public final class LongRange extends Range implements Serializable {
/**
* Required for serialization support.
*
* @see java.io.Serializable
*/
private static final long serialVersionUID = 71849363892720L;
/**
* The minimum number in this range (inclusive).
*/
private final long min;
/**
* The maximum number in this range (inclusive).
*/
private final long max;
/**
* Cached output minObject (class is immutable).
*/
private transient Long minObject = null;
/**
* Cached output maxObject (class is immutable).
*/
private transient Long maxObject = null;
/**
* Cached output hashCode (class is immutable).
*/
private transient int hashCode = 0;
/**
* Cached output toString (class is immutable).
*/
private transient String toString = null;
/**
* <p>Constructs a new <code>LongRange</code> using the specified
* number as both the minimum and maximum in this range.</p>
*
* @param number the number to use for this range
*/
public LongRange(long number) {
super();
this.min = number;
this.max = number;
}
/**
* <p>Constructs a new <code>LongRange</code> using the specified
* number as both the minimum and maximum in this range.</p>
*
* @param number the number to use for this range, must not
* be <code>null</code>
* @throws IllegalArgumentException if the number is <code>null</code>
*/
public LongRange(Number number) {
super();
if (number == null) {
throw new IllegalArgumentException("The number must not be null");
}
this.min = number.longValue();
this.max = number.longValue();
if (number instanceof Long) {
this.minObject = (Long) number;
this.maxObject = (Long) number;
}
}
/**
* <p>Constructs a new <code>LongRange</code> with the specified
* minimum and maximum numbers (both inclusive).</p>
*
* <p>The arguments may be passed in the order (min,max) or (max,min). The
* getMinimum and getMaximum methods will return the correct values.</p>
*
* @param number1 first number that defines the edge of the range, inclusive
* @param number2 second number that defines the edge of the range, inclusive
*/
public LongRange(long number1, long number2) {
super();
if (number2 < number1) {
this.min = number2;
this.max = number1;
} else {
this.min = number1;
this.max = number2;
}
}
/**
* <p>Constructs a new <code>LongRange</code> with the specified
* minimum and maximum numbers (both inclusive).</p>
*
* <p>The arguments may be passed in the order (min,max) or (max,min). The
* getMinimum and getMaximum methods will return the correct values.</p>
*
* @param number1 first number that defines the edge of the range, inclusive
* @param number2 second number that defines the edge of the range, inclusive
* @throws IllegalArgumentException if either number is <code>null</code>
*/
public LongRange(Number number1, Number number2) {
super();
if (number1 == null || number2 == null) {
throw new IllegalArgumentException("The numbers must not be null");
}
long number1val = number1.longValue();
long number2val = number2.longValue();
if (number2val < number1val) {
this.min = number2val;
this.max = number1val;
if (number2 instanceof Long) {
this.minObject = (Long) number2;
}
if (number1 instanceof Long) {
this.maxObject = (Long) number1;
}
} else {
this.min = number1val;
this.max = number2val;
if (number1 instanceof Long) {
this.minObject = (Long) number1;
}
if (number2 instanceof Long) {
this.maxObject = (Long) number2;
}
}
}
// Accessors
//--------------------------------------------------------------------
/**
* <p>Returns the minimum number in this range.</p>
*
* @return the minimum number in this range
*/
@Override
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
*/
@Override
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
*/
@Override
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
*/
@Override
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
*/
@Override
public float getMinimumFloat() {
return min;
}
/**
* <p>Returns the maximum number in this range.</p>
*
* @return the maximum number in this range
*/
@Override
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
*/
@Override
public long getMaximumLong() {
return max;
}
/**
* <p>Gets the maximum number in this range cast to an <code>int</code>.</p>
*
* <p>This conversion can lose information for large values.</p>
*
* @return the maximum number in this range cast to an <code>int</code>.
*/
@Override
public int getMaximumInteger() {
return (int) max;
}
/**
* <p>Gets the maximum number in this range as a <code>double</code>.</p>
*
* <p>This conversion can lose information for large values.</p>
*
* @return The maximum number in this range as a <code>double</code>.
*/
@Override
public double getMaximumDouble() {
return max;
}
/**
* <p>Gets the maximum number in this range as a <code>float</code>.</p>
*
* <p>This conversion can lose information for large values.</p>
*
* @return The maximum number in this range as a <code>float</code>.
*/
@Override
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
*/
@Override
public boolean containsNumber(Number number) {
if (number == null) {
return false;
}
return containsLong(number.longValue());
}
/**
* <p>Tests whether the specified <code>long</code> occurs within
* this range using <code>long</code> comparison.</p>
*
* <p>This implementation overrides the superclass for performance as it is
* the most common case.</p>
*
* @param value the long to test
* @return <code>true</code> if the specified number occurs within this
* range by <code>long</code> comparison
*/
@Override
public boolean containsLong(long value) {
return value >= min && value <= max;
}
// Range tests
//--------------------------------------------------------------------
/**
* <p>Tests whether the specified range occurs entirely within this range
* using <code>long</code> comparison.</p>
*
* <p><code>null</code> is handled and returns <code>false</code>.</p>
*
* @param range the range to test, may be <code>null</code>
* @return <code>true</code> if the specified range occurs entirely within this range
* @throws IllegalArgumentException if the range is not of this type
*/
@Override
public boolean containsRange(Range range) {
if (range == null) {
return false;
}
return containsLong(range.getMinimumLong()) &&
containsLong(range.getMaximumLong());
}
/**
* <p>Tests whether the specified range overlaps with this range
* using <code>long</code> comparison.</p>
*
* <p><code>null</code> is handled and returns <code>false</code>.</p>
*
* @param range the range to test, may be <code>null</code>
* @return <code>true</code> if the specified range overlaps with this range
*/
@Override
public boolean overlapsRange(Range range) {
if (range == null) {
return false;
}
return range.containsLong(min) ||
range.containsLong(max) ||
containsLong(range.getMinimumLong());
}
// Basics
//--------------------------------------------------------------------
/**
* <p>Compares this range to another object to test if they are equal.</p>.
*
* <p>To be equal, the class, minimum and maximum must be equal.</p>
*
* @param obj the reference object with which to compare
* @return <code>true</code> if this object is equal
*/
@Override
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
*/
@Override
public int hashCode() {
int temp = hashCode;
if (temp == 0) {
temp = 17;
temp = 37 * temp + getClass().hashCode();
temp = 37 * temp + ((int) (min ^ (min >> 32)));
temp = 37 * temp + ((int) (max ^ (max >> 32)));
hashCode = temp;
}
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
*/
@Override
public String toString() {
if (toString == null) {
StringBuilder buf = new StringBuilder(32);
buf.append("Range[");
buf.append(min);
buf.append(',');
buf.append(max);
buf.append(']');
toString = buf.toString();
}
return toString;
}
/**
* <p>Returns an array containing all the long values in the range.</p>
*
* @return the <code>long[]</code> representation of this range
* @since 2.4
*/
public long[] toArray() {
long[] array = new long[(int)(max - min + 1L)];
for(int i = 0; i < array.length; i++) {
array[i] = min + i;
}
return array;
}
}

View File

@ -1,253 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang.math;
import java.io.Serializable;
/**
* <p><code>NumberRange</code> represents an inclusive range of
* {@link java.lang.Number} objects of the same type.</p>
*
* @author Apache Software Foundation
* @author <a href="mailto:chrise@esha.com">Christopher Elkins</a>
* @since 2.0 (previously in org.apache.commons.lang)
* @version $Id$
*/
public final class NumberRange extends Range implements Serializable {
/**
* Required for serialization support.
*
* @see java.io.Serializable
*/
private static final long serialVersionUID = 71849363892710L;
/**
* The minimum number in this range.
*/
private final Number min;
/**
* The maximum number in this range.
*/
private final Number max;
/**
* Cached output hashCode (class is immutable).
*/
private transient int hashCode = 0;
/**
* Cached output toString (class is immutable).
*/
private transient String toString = null;
/**
* <p>Constructs a new <code>NumberRange</code> using the specified
* number as both the minimum and maximum in this range.</p>
*
* @param num the number to use for this range
* @throws IllegalArgumentException if the number is <code>null</code>
* @throws IllegalArgumentException if the number doesn't implement <code>Comparable</code>
* @throws IllegalArgumentException if the number is <code>Double.NaN</code> or <code>Float.NaN</code>
*/
public NumberRange(Number num) {
if (num == null) {
throw new IllegalArgumentException("The number must not be null");
}
if (num instanceof Comparable<?> == false) {
throw new IllegalArgumentException("The number must implement Comparable");
}
if (num instanceof Double && ((Double) num).isNaN()) {
throw new IllegalArgumentException("The number must not be NaN");
}
if (num instanceof Float && ((Float) num).isNaN()) {
throw new IllegalArgumentException("The number must not be NaN");
}
this.min = num;
this.max = num;
}
/**
* <p>Constructs a new <code>NumberRange</code> with the specified
* minimum and maximum numbers (both inclusive).</p>
*
* <p>The arguments may be passed in the order (min,max) or (max,min). The
* {@link #getMinimumNumber()} and {@link #getMaximumNumber()} methods will return the
* correct value.</p>
*
* <p>This constructor is designed to be used with two <code>Number</code>
* objects of the same type. If two objects of different types are passed in,
* an exception is thrown.</p>
*
* @param num1 first number that defines the edge of the range, inclusive
* @param num2 second number that defines the edge of the range, inclusive
* @throws IllegalArgumentException if either number is <code>null</code>
* @throws IllegalArgumentException if the numbers are of different types
* @throws IllegalArgumentException if the numbers don't implement <code>Comparable</code>
*/
public NumberRange(Number num1, Number num2) {
if (num1 == null || num2 == null) {
throw new IllegalArgumentException("The numbers must not be null");
}
if (num1.getClass() != num2.getClass()) {
throw new IllegalArgumentException("The numbers must be of the same type");
}
if (num1 instanceof Comparable<?> == false) {
throw new IllegalArgumentException("The numbers must implement Comparable");
}
if (num1 instanceof Double) {
if (((Double) num1).isNaN() || ((Double) num2).isNaN()) {
throw new IllegalArgumentException("The number must not be NaN");
}
} else if (num1 instanceof Float) {
if (((Float) num1).isNaN() || ((Float) num2).isNaN()) {
throw new IllegalArgumentException("The number must not be NaN");
}
}
@SuppressWarnings("unchecked") // this is checked above
int compare = ((Comparable<Number>) 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
*/
@Override
public Number getMinimumNumber() {
return min;
}
/**
* <p>Returns the maximum number in this range.</p>
*
* @return the maximum number in this range
*/
@Override
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
*/
@Override
public boolean containsNumber(Number number) {
if (number == null) {
return false;
}
if (number.getClass() != min.getClass()) {
throw new IllegalArgumentException("The number must be of the same type as the range numbers");
}
@SuppressWarnings("unchecked") // this was checked in the ctor
int compareMin = ((Comparable<Number>) min).compareTo(number);
@SuppressWarnings("unchecked") // this was checked in the ctor
int compareMax = ((Comparable<Number>) 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
*/
@Override
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
*/
@Override
public int hashCode() {
int temp = hashCode;
if (temp == 0) {
temp = 17;
temp = 37 * temp + getClass().hashCode();
temp = 37 * temp + min.hashCode();
temp = 37 * temp + max.hashCode();
hashCode = temp;
}
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
*/
@Override
public String toString() {
if (toString == null) {
StringBuilder buf = new StringBuilder(32);
buf.append("Range[");
buf.append(min);
buf.append(',');
buf.append(max);
buf.append(']');
toString = buf.toString();
}
return toString;
}
}

View File

@ -1,434 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang.math;
/**
* <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 Apache Software Foundation
* @since 2.0
* @version $Id$
*/
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 containsNumber(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 #containsLong(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 containsLong(Number value) {
if (value == null) {
return false;
}
return containsLong(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 containsLong(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 #containsInteger(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 containsInteger(Number value) {
if (value == null) {
return false;
}
return containsInteger(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 containsInteger(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 #containsDouble(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 containsDouble(Number value) {
if (value == null) {
return false;
}
return containsDouble(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 containsDouble(double value) {
int compareMin = Double.compare(getMinimumDouble(), value);
int compareMax = Double.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 #containsFloat(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 containsFloat(Number value) {
if (value == null) {
return false;
}
return containsFloat(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 containsFloat(float value) {
int compareMin = Float.compare(getMinimumFloat(), value);
int compareMax = Float.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 #containsNumber(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 containsRange(Range range) {
if (range == null) {
return false;
}
return containsNumber(range.getMinimumNumber())
&& containsNumber(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 #containsNumber(Number)} and
* {@link #containsRange(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.containsNumber(getMinimumNumber())
|| range.containsNumber(getMaximumNumber())
|| containsNumber(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
*/
@Override
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
*/
@Override
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
*/
@Override
public String toString() {
StringBuilder buf = new StringBuilder(32);
buf.append("Range[");
buf.append(getMinimumNumber());
buf.append(',');
buf.append(getMaximumNumber());
buf.append(']');
return buf.toString();
}
}

View File

@ -1,364 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.lang.math;
import junit.framework.TestCase;
/**
* Test cases for the {@link Range} classes.
*
* @author Apache Software Foundation
* @version $Id$
*/
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 {
@Override
public double doubleValue() {
return 12d;
}
@Override
public float floatValue() {
return 12f;
}
@Override
public int intValue() {
return 12;
}
@Override
public long longValue() {
return 12L;
}
}
protected InnerNumber nonComparableNumber = new InnerNumber();
public AbstractRangeTest(String name) {
super(name);
}
@Override
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 testContainsLong() {
assertEquals(false, tenToTwenty.containsLong(null));
assertEquals(true, tenToTwenty.containsLong(nonComparableNumber));
assertEquals(false, tenToTwenty.containsLong(five));
assertEquals(true, tenToTwenty.containsLong(ten));
assertEquals(true, tenToTwenty.containsLong(fifteen));
assertEquals(true, tenToTwenty.containsLong(twenty));
assertEquals(false, tenToTwenty.containsLong(twentyFive));
assertEquals(false, tenToTwenty.containsLong(long8));
assertEquals(true, tenToTwenty.containsLong(long10));
assertEquals(true, tenToTwenty.containsLong(long12));
assertEquals(true, tenToTwenty.containsLong(long20));
assertEquals(false, tenToTwenty.containsLong(long21));
assertEquals(false, tenToTwenty.containsLong(double8));
assertEquals(true, tenToTwenty.containsLong(double10));
assertEquals(true, tenToTwenty.containsLong(double12));
assertEquals(true, tenToTwenty.containsLong(double20));
assertEquals(false, tenToTwenty.containsLong(double21));
assertEquals(false, tenToTwenty.containsLong(float8));
assertEquals(true, tenToTwenty.containsLong(float10));
assertEquals(true, tenToTwenty.containsLong(float12));
assertEquals(true, tenToTwenty.containsLong(float20));
assertEquals(false, tenToTwenty.containsLong(float21));
assertEquals(false, tenToTwenty.containsLong(9L));
assertEquals(true, tenToTwenty.containsLong(10L));
assertEquals(true, tenToTwenty.containsLong(15L));
assertEquals(true, tenToTwenty.containsLong(20L));
assertEquals(false, tenToTwenty.containsLong(21L));
}
public void testContainsInteger() {
assertEquals(false, tenToTwenty.containsInteger(null));
assertEquals(true, tenToTwenty.containsInteger(nonComparableNumber));
assertEquals(false, tenToTwenty.containsInteger(five));
assertEquals(true, tenToTwenty.containsInteger(ten));
assertEquals(true, tenToTwenty.containsInteger(fifteen));
assertEquals(true, tenToTwenty.containsInteger(twenty));
assertEquals(false, tenToTwenty.containsInteger(twentyFive));
assertEquals(false, tenToTwenty.containsInteger(long8));
assertEquals(true, tenToTwenty.containsInteger(long10));
assertEquals(true, tenToTwenty.containsInteger(long12));
assertEquals(true, tenToTwenty.containsInteger(long20));
assertEquals(false, tenToTwenty.containsInteger(long21));
assertEquals(false, tenToTwenty.containsInteger(double8));
assertEquals(true, tenToTwenty.containsInteger(double10));
assertEquals(true, tenToTwenty.containsInteger(double12));
assertEquals(true, tenToTwenty.containsInteger(double20));
assertEquals(false, tenToTwenty.containsInteger(double21));
assertEquals(false, tenToTwenty.containsInteger(float8));
assertEquals(true, tenToTwenty.containsInteger(float10));
assertEquals(true, tenToTwenty.containsInteger(float12));
assertEquals(true, tenToTwenty.containsInteger(float20));
assertEquals(false, tenToTwenty.containsInteger(float21));
assertEquals(false, tenToTwenty.containsInteger(9));
assertEquals(true, tenToTwenty.containsInteger(10));
assertEquals(true, tenToTwenty.containsInteger(15));
assertEquals(true, tenToTwenty.containsInteger(20));
assertEquals(false, tenToTwenty.containsInteger(21));
}
public void testContainsDouble() {
assertEquals(false, tenToTwenty.containsDouble(null));
assertEquals(true, tenToTwenty.containsDouble(nonComparableNumber));
assertEquals(false, tenToTwenty.containsDouble(five));
assertEquals(true, tenToTwenty.containsDouble(ten));
assertEquals(true, tenToTwenty.containsDouble(fifteen));
assertEquals(true, tenToTwenty.containsDouble(twenty));
assertEquals(false, tenToTwenty.containsDouble(twentyFive));
assertEquals(false, tenToTwenty.containsDouble(long8));
assertEquals(true, tenToTwenty.containsDouble(long10));
assertEquals(true, tenToTwenty.containsDouble(long12));
assertEquals(true, tenToTwenty.containsDouble(long20));
assertEquals(false, tenToTwenty.containsDouble(long21));
assertEquals(false, tenToTwenty.containsDouble(double8));
assertEquals(true, tenToTwenty.containsDouble(double10));
assertEquals(true, tenToTwenty.containsDouble(double12));
assertEquals(true, tenToTwenty.containsDouble(double20));
assertEquals(false, tenToTwenty.containsDouble(double21));
assertEquals(false, tenToTwenty.containsDouble(float8));
assertEquals(true, tenToTwenty.containsDouble(float10));
assertEquals(true, tenToTwenty.containsDouble(float12));
assertEquals(true, tenToTwenty.containsDouble(float20));
assertEquals(false, tenToTwenty.containsDouble(float21));
assertEquals(false, tenToTwenty.containsDouble(9d));
assertEquals(true, tenToTwenty.containsDouble(10d));
assertEquals(true, tenToTwenty.containsDouble(15d));
assertEquals(true, tenToTwenty.containsDouble(20d));
assertEquals(false, tenToTwenty.containsDouble(21d));
}
public void testContainsFloat() {
assertEquals(false, tenToTwenty.containsFloat(null));
assertEquals(true, tenToTwenty.containsFloat(nonComparableNumber));
assertEquals(false, tenToTwenty.containsFloat(five));
assertEquals(true, tenToTwenty.containsFloat(ten));
assertEquals(true, tenToTwenty.containsFloat(fifteen));
assertEquals(true, tenToTwenty.containsFloat(twenty));
assertEquals(false, tenToTwenty.containsFloat(twentyFive));
assertEquals(false, tenToTwenty.containsFloat(long8));
assertEquals(true, tenToTwenty.containsFloat(long10));
assertEquals(true, tenToTwenty.containsFloat(long12));
assertEquals(true, tenToTwenty.containsFloat(long20));
assertEquals(false, tenToTwenty.containsFloat(long21));
assertEquals(false, tenToTwenty.containsFloat(double8));
assertEquals(true, tenToTwenty.containsFloat(double10));
assertEquals(true, tenToTwenty.containsFloat(double12));
assertEquals(true, tenToTwenty.containsFloat(double20));
assertEquals(false, tenToTwenty.containsFloat(double21));
assertEquals(false, tenToTwenty.containsFloat(float8));
assertEquals(true, tenToTwenty.containsFloat(float10));
assertEquals(true, tenToTwenty.containsFloat(float12));
assertEquals(true, tenToTwenty.containsFloat(float20));
assertEquals(false, tenToTwenty.containsFloat(float21));
assertEquals(false, tenToTwenty.containsFloat(9f));
assertEquals(true, tenToTwenty.containsFloat(10f));
assertEquals(true, tenToTwenty.containsFloat(15f));
assertEquals(true, tenToTwenty.containsFloat(20f));
assertEquals(false, tenToTwenty.containsFloat(21f));
}
//--------------------------------------------------------------------------
public void testContainsRange() {
assertEquals(false, tenToTwenty.containsRange(null));
assertEquals(false, tenToTwenty.containsRange(createRange(five, five)));
assertEquals(false, tenToTwenty.containsRange(createRange(five, ten)));
assertEquals(false, tenToTwenty.containsRange(createRange(five, twelve)));
assertEquals(false, tenToTwenty.containsRange(createRange(five, fifteen)));
assertEquals(false, tenToTwenty.containsRange(createRange(five, twenty)));
assertEquals(false, tenToTwenty.containsRange(createRange(five, twentyFive)));
assertEquals(true, tenToTwenty.containsRange(createRange(ten, ten)));
assertEquals(true, tenToTwenty.containsRange(createRange(ten, twelve)));
assertEquals(true, tenToTwenty.containsRange(createRange(ten, fifteen)));
assertEquals(true, tenToTwenty.containsRange(createRange(ten, twenty)));
assertEquals(false, tenToTwenty.containsRange(createRange(ten, twentyFive)));
assertEquals(true, tenToTwenty.containsRange(createRange(twelve, twelve)));
assertEquals(true, tenToTwenty.containsRange(createRange(twelve, fifteen)));
assertEquals(true, tenToTwenty.containsRange(createRange(twelve, twenty)));
assertEquals(false, tenToTwenty.containsRange(createRange(twelve, twentyFive)));
assertEquals(true, tenToTwenty.containsRange(createRange(fifteen, fifteen)));
assertEquals(true, tenToTwenty.containsRange(createRange(fifteen, twenty)));
assertEquals(false, tenToTwenty.containsRange(createRange(fifteen, twentyFive)));
assertEquals(true, tenToTwenty.containsRange(createRange(twenty, twenty)));
assertEquals(false, tenToTwenty.containsRange(createRange(twenty, twentyFive)));
assertEquals(false, tenToTwenty.containsRange(createRange(twentyFive, twentyFive)));
}
public void testOverlapsRange() {
assertEquals(false, tenToTwenty.overlapsRange(null));
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(tenToTwenty));
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() {
String str = tenToTwenty.toString();
assertEquals("Range[10,20]", str);
assertSame(str, 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

@ -1,177 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.lang.math;
import junit.framework.Test;
import junit.framework.TestSuite;
/**
* Test cases for the {@link DoubleRange} class.
*
* @author Apache Software Foundation
* @version $Id$
*/
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;
}
@Override
public void setUp() {
super.setUp();
tenToTwenty = new DoubleRange(double10, double20);
otherRange = new NumberRange(ten, twenty);
}
@Override
protected Range createRange(Integer integer1, Integer integer2) {
return new DoubleRange(integer1, integer2);
}
@Override
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());
new DoubleRange(nonComparableNumber);
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 testContainsNumber() {
assertEquals(false, tenToTwenty.containsNumber(null));
assertEquals(true, tenToTwenty.containsNumber(nonComparableNumber));
assertEquals(false, tenToTwenty.containsNumber(five));
assertEquals(true, tenToTwenty.containsNumber(ten));
assertEquals(true, tenToTwenty.containsNumber(fifteen));
assertEquals(true, tenToTwenty.containsNumber(twenty));
assertEquals(false, tenToTwenty.containsNumber(twentyFive));
assertEquals(false, tenToTwenty.containsNumber(long8));
assertEquals(true, tenToTwenty.containsNumber(long10));
assertEquals(true, tenToTwenty.containsNumber(long12));
assertEquals(true, tenToTwenty.containsNumber(long20));
assertEquals(false, tenToTwenty.containsNumber(long21));
assertEquals(false, tenToTwenty.containsNumber(double8));
assertEquals(true, tenToTwenty.containsNumber(double10));
assertEquals(true, tenToTwenty.containsNumber(double12));
assertEquals(true, tenToTwenty.containsNumber(double20));
assertEquals(false, tenToTwenty.containsNumber(double21));
assertEquals(false, tenToTwenty.containsNumber(float8));
assertEquals(true, tenToTwenty.containsNumber(float10));
assertEquals(true, tenToTwenty.containsNumber(float12));
assertEquals(true, tenToTwenty.containsNumber(float20));
assertEquals(false, tenToTwenty.containsNumber(float21));
}
@Override
public void testToString() {
String str = tenToTwenty.toString();
assertEquals("Range[10.0,20.0]", str);
assertSame(str, tenToTwenty.toString());
assertEquals("Range[-20.0,-10.0]", createRange(new Integer(-20), new Integer(-10)).toString());
}
//--------------------------------------------------------------------------
}

View File

@ -1,177 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.lang.math;
import junit.framework.Test;
import junit.framework.TestSuite;
/**
* Test cases for the {@link FloatRange} class.
*
* @author Apache Software Foundation
* @version $Id$
*/
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;
}
@Override
public void setUp() {
super.setUp();
tenToTwenty = new FloatRange(float10, float20);
otherRange = new NumberRange(ten, twenty);
}
@Override
protected Range createRange(Integer integer1, Integer integer2) {
return new FloatRange(integer1, integer2);
}
@Override
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());
new FloatRange(nonComparableNumber);
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 testContainsNumber() {
assertEquals(false, tenToTwenty.containsNumber(null));
assertEquals(true, tenToTwenty.containsNumber(nonComparableNumber));
assertEquals(false, tenToTwenty.containsNumber(five));
assertEquals(true, tenToTwenty.containsNumber(ten));
assertEquals(true, tenToTwenty.containsNumber(fifteen));
assertEquals(true, tenToTwenty.containsNumber(twenty));
assertEquals(false, tenToTwenty.containsNumber(twentyFive));
assertEquals(false, tenToTwenty.containsNumber(long8));
assertEquals(true, tenToTwenty.containsNumber(long10));
assertEquals(true, tenToTwenty.containsNumber(long12));
assertEquals(true, tenToTwenty.containsNumber(long20));
assertEquals(false, tenToTwenty.containsNumber(long21));
assertEquals(false, tenToTwenty.containsNumber(double8));
assertEquals(true, tenToTwenty.containsNumber(double10));
assertEquals(true, tenToTwenty.containsNumber(double12));
assertEquals(true, tenToTwenty.containsNumber(double20));
assertEquals(false, tenToTwenty.containsNumber(double21));
assertEquals(false, tenToTwenty.containsNumber(float8));
assertEquals(true, tenToTwenty.containsNumber(float10));
assertEquals(true, tenToTwenty.containsNumber(float12));
assertEquals(true, tenToTwenty.containsNumber(float20));
assertEquals(false, tenToTwenty.containsNumber(float21));
}
@Override
public void testToString() {
String str = tenToTwenty.toString();
assertEquals("Range[10.0,20.0]", str);
assertSame(str, tenToTwenty.toString());
assertEquals("Range[-20.0,-10.0]", createRange(new Integer(-20), new Integer(-10)).toString());
}
//--------------------------------------------------------------------------
}

View File

@ -1,177 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.lang.math;
import junit.framework.Test;
import junit.framework.TestSuite;
import java.util.Arrays;
/**
* Test cases for the {@link IntRange} class.
*
* @author Apache Software Foundation
* @author Janek Bogucki
* @author Phil Steitz
* @version $Id$
*/
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;
}
@Override
public void setUp() {
super.setUp();
tenToTwenty = new IntRange(ten, twenty);
otherRange = new NumberRange(ten, twenty);
}
@Override
protected Range createRange(Integer integer1, Integer integer2) {
return new IntRange(integer1, integer2);
}
@Override
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());
new IntRange(nonComparableNumber);
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());
// test non Integer, for full coverage
Long fiveL = new Long(5L);
Long tenL = new Long(10L);
nr = new IntRange(fiveL, tenL);
assertEquals(five, nr.getMinimumNumber());
assertEquals(ten, nr.getMaximumNumber());
nr = new IntRange(tenL, fiveL);
assertEquals(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 testContainsNumber() {
assertEquals(false, tenToTwenty.containsNumber(null));
assertEquals(true, tenToTwenty.containsNumber(nonComparableNumber));
assertEquals(false, tenToTwenty.containsNumber(five));
assertEquals(true, tenToTwenty.containsNumber(ten));
assertEquals(true, tenToTwenty.containsNumber(fifteen));
assertEquals(true, tenToTwenty.containsNumber(twenty));
assertEquals(false, tenToTwenty.containsNumber(twentyFive));
assertEquals(false, tenToTwenty.containsNumber(long8));
assertEquals(true, tenToTwenty.containsNumber(long10));
assertEquals(true, tenToTwenty.containsNumber(long12));
assertEquals(true, tenToTwenty.containsNumber(long20));
assertEquals(false, tenToTwenty.containsNumber(long21));
assertEquals(false, tenToTwenty.containsNumber(double8));
assertEquals(true, tenToTwenty.containsNumber(double10));
assertEquals(true, tenToTwenty.containsNumber(double12));
assertEquals(true, tenToTwenty.containsNumber(double20));
assertEquals(false, tenToTwenty.containsNumber(double21));
assertEquals(false, tenToTwenty.containsNumber(float8));
assertEquals(true, tenToTwenty.containsNumber(float10));
assertEquals(true, tenToTwenty.containsNumber(float12));
assertEquals(true, tenToTwenty.containsNumber(float20));
assertEquals(false, tenToTwenty.containsNumber(float21));
}
public void testContainsIntegerBig() {
IntRange big = new IntRange(Integer.MAX_VALUE, Integer.MAX_VALUE- 2);
assertEquals(true, big.containsInteger(Integer.MAX_VALUE - 1));
assertEquals(false, big.containsInteger(Integer.MAX_VALUE - 3));
}
public void testToArray() {
int[] threeItems = new IntRange(3, 5).toArray();
assertTrue(Arrays.equals(new int[]{3, 4, 5}, threeItems));
int[] oneItem = new IntRange(4).toArray();
assertTrue(Arrays.equals(new int[]{4}, oneItem));
}
//--------------------------------------------------------------------------
}

View File

@ -1,165 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.lang.math;
import junit.framework.Test;
import junit.framework.TestSuite;
import java.util.Arrays;
/**
* Test cases for the {@link LongRange} class.
*
* @author Apache Software Foundation
* @version $Id$
*/
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;
}
@Override
public void setUp() {
super.setUp();
tenToTwenty = new LongRange(long10, long20);
otherRange = new NumberRange(ten, twenty);
}
@Override
protected Range createRange(Integer integer1, Integer integer2) {
return new LongRange(integer1, integer2);
}
@Override
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());
new LongRange(nonComparableNumber);
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 testContainsNumber() {
assertEquals(false, tenToTwenty.containsNumber(null));
assertEquals(true, tenToTwenty.containsNumber(nonComparableNumber));
assertEquals(false, tenToTwenty.containsNumber(five));
assertEquals(true, tenToTwenty.containsNumber(ten));
assertEquals(true, tenToTwenty.containsNumber(fifteen));
assertEquals(true, tenToTwenty.containsNumber(twenty));
assertEquals(false, tenToTwenty.containsNumber(twentyFive));
assertEquals(false, tenToTwenty.containsNumber(long8));
assertEquals(true, tenToTwenty.containsNumber(long10));
assertEquals(true, tenToTwenty.containsNumber(long12));
assertEquals(true, tenToTwenty.containsNumber(long20));
assertEquals(false, tenToTwenty.containsNumber(long21));
assertEquals(false, tenToTwenty.containsNumber(double8));
assertEquals(true, tenToTwenty.containsNumber(double10));
assertEquals(true, tenToTwenty.containsNumber(double12));
assertEquals(true, tenToTwenty.containsNumber(double20));
assertEquals(false, tenToTwenty.containsNumber(double21));
assertEquals(false, tenToTwenty.containsNumber(float8));
assertEquals(true, tenToTwenty.containsNumber(float10));
assertEquals(true, tenToTwenty.containsNumber(float12));
assertEquals(true, tenToTwenty.containsNumber(float20));
assertEquals(false, tenToTwenty.containsNumber(float21));
}
public void testContainsLongBig() {
LongRange big = new LongRange(Long.MAX_VALUE, Long.MAX_VALUE- 2);
assertEquals(true, big.containsLong(Long.MAX_VALUE - 1));
assertEquals(false, big.containsLong(Long.MAX_VALUE - 3));
}
public void testToArray() {
long[] threeItems = new LongRange(3, 5).toArray();
assertTrue(Arrays.equals(new long[]{3, 4, 5}, threeItems));
long[] oneItem = new LongRange(4).toArray();
assertTrue(Arrays.equals(new long[]{4}, oneItem));
}
//--------------------------------------------------------------------------
}

View File

@ -1,169 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.lang.math;
import junit.framework.Test;
import junit.framework.TestSuite;
/**
* Test cases for the {@link NumberRange} class.
*
* @author Apache Software Foundation
* @author <a href="mailto:chrise@esha.com">Christopher Elkins</a>
* @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
* @version $Id$
*/
public final class NumberRangeTest extends AbstractRangeTest {
public static Test suite() {
TestSuite suite = new TestSuite(NumberRangeTest.class);
suite.setName("NumberRange Tests");
return suite;
}
public NumberRangeTest(String name) {
super(name);
}
void checkConstructorException(Number num) {
try {
new NumberRange(num);
fail("Expected an IllegalArgumentException");
} catch (IllegalArgumentException e) {
// Expected.
}
}
void checkConstructorException(Number num1, Number num2) {
try {
new NumberRange(num1, num2);
fail("Expected an IllegalArgumentException");
} catch (IllegalArgumentException e) {
// Expected.
}
}
@Override
protected Range createRange(Integer integer) {
return new NumberRange(integer);
}
// --------------------------------------------------------------------------
@Override
protected Range createRange(Integer integer1, Integer integer2) {
return new NumberRange(integer1, integer2);
}
@Override
public void setUp() {
super.setUp();
tenToTwenty = new NumberRange(ten, twenty);
otherRange = new IntRange(ten, twenty);
}
/**
* Tests non-exceptional conditions for the one argument constructor.
*/
public void testConstructor1() {
NumberRange nr = new NumberRange(five);
assertSame(five, nr.getMinimumNumber());
assertSame(five, nr.getMaximumNumber());
}
/**
* Tests exceptional conditions for the one argument constructor.
*/
public void testConstructor1Exceptions() {
this.checkConstructorException(null);
this.checkConstructorException(nonComparableNumber);
this.checkConstructorException(new Float(Float.NaN));
this.checkConstructorException(new Double(Double.NaN));
}
/**
* Tests non-exceptional conditions for the two argument constructor.
*/
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());
}
/**
* Tests exceptional conditions for the two argument constructor.
*/
public void testConstructor2Exceptions() {
this.checkConstructorException(null, null);
this.checkConstructorException(new Float(12.2f), new Double(12.2));
this.checkConstructorException(new Float(Float.NaN), new Double(12.2));
this.checkConstructorException(new Double(Double.NaN), new Double(12.2));
this.checkConstructorException(new Double(12.2), new Double(Double.NaN));
this.checkConstructorException(new Double(Double.NaN), new Double(Double.NaN));
this.checkConstructorException(null, new Double(12.2));
this.checkConstructorException(new Double(12.2), null);
this.checkConstructorException(new Double(12.2f), new Float(12.2));
this.checkConstructorException(new Double(Double.NaN), new Float(12.2));
this.checkConstructorException(new Float(Float.NaN), new Float(12.2));
this.checkConstructorException(new Float(12.2), new Float(Float.NaN));
this.checkConstructorException(new Float(Float.NaN), new Float(Float.NaN));
this.checkConstructorException(null, new Float(12.2));
this.checkConstructorException(new Float(12.2), null);
this.checkConstructorException(nonComparableNumber, nonComparableNumber);
this.checkConstructorException(null, nonComparableNumber);
this.checkConstructorException(nonComparableNumber, null);
this.checkConstructorException(new Float(12.2), nonComparableNumber);
this.checkConstructorException(nonComparableNumber, new Float(12.2));
}
// --------------------------------------------------------------------------
public void testContainsLongBig() {
// original NumberRange class failed this test
NumberRange big = new NumberRange(new Long(Long.MAX_VALUE), new Long(Long.MAX_VALUE - 2));
assertEquals(true, big.containsLong(Long.MAX_VALUE - 1));
assertEquals(false, big.containsLong(Long.MAX_VALUE - 3));
}
public void testContainsNumber() {
assertEquals(false, tenToTwenty.containsNumber(null));
assertEquals(false, tenToTwenty.containsNumber(five));
assertEquals(true, tenToTwenty.containsNumber(ten));
assertEquals(true, tenToTwenty.containsNumber(fifteen));
assertEquals(true, tenToTwenty.containsNumber(twenty));
assertEquals(false, tenToTwenty.containsNumber(twentyFive));
try {
tenToTwenty.containsNumber(long21);
fail();
} catch (IllegalArgumentException ex) {
}
}
// --------------------------------------------------------------------------
}

View File

@ -1,130 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang.math;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* <p>
* Tests the base methods in the {@link org.apache.commons.lang.math.Range} class.
* </p>
*
* @author Nathan Beyer
* @version $Id$
*/
public class RangeTest extends TestCase {
private static class RangeTestFixture extends Range {
private byte max;
private byte min;
RangeTestFixture(byte min, byte max) {
super();
this.min = min;
this.max = max;
}
@Override
public boolean containsNumber(Number number) {
if (number.byteValue() >= min && number.byteValue() <= max) {
return true;
}
return false;
}
@Override
public Number getMaximumNumber() {
return Byte.valueOf(max);
}
@Override
public Number getMinimumNumber() {
return Byte.valueOf(min);
}
}
public static Test suite() {
TestSuite suite = new TestSuite(RangeTest.class);
suite.setName("Range Tests");
return suite;
}
public RangeTest(String name) {
super(name);
}
@Override
protected void setUp() throws Exception {
super.setUp();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
/**
* Test method for 'org.apache.commons.lang.math.Range.equals(Object)'
*/
public void testEqualsObject() {
RangeTestFixture r1 = new RangeTestFixture((byte) 0, (byte) 5);
RangeTestFixture r2 = new RangeTestFixture((byte) 0, (byte) 5);
RangeTestFixture r3 = new RangeTestFixture((byte) 0, (byte) 10);
assertEquals(r1, r1);
assertEquals(r1, r2);
assertEquals(r2, r2);
assertTrue(r1.equals(r1));
assertTrue(r2.equals(r2));
assertTrue(r3.equals(r3));
assertFalse(r2.equals(r3));
assertFalse(r2.equals(null));
assertFalse(r2.equals("Ni!"));
}
/**
* Test method for 'org.apache.commons.lang.math.Range.hashCode()'
*/
public void testHashCode() {
RangeTestFixture r1 = new RangeTestFixture((byte) 0, (byte) 5);
RangeTestFixture r2 = new RangeTestFixture((byte) 0, (byte) 5);
RangeTestFixture r3 = new RangeTestFixture((byte) 0, (byte) 10);
assertEquals(r1.hashCode(), r2.hashCode());
assertFalse(r1.hashCode() == r3.hashCode());
}
/**
* Test method for 'org.apache.commons.lang.math.Range.toString()'
*/
public void testToString() {
RangeTestFixture r1 = new RangeTestFixture((byte) 0, (byte) 5);
assertNotNull(r1.toString());
assertNotNull(r1.toString());
RangeTestFixture r2 = new RangeTestFixture((byte) 0, (byte) 5);
assertNotNull(r2.toString());
assertNotNull(r2.toString());
RangeTestFixture r3 = new RangeTestFixture((byte) 0, (byte) 10);
assertNotNull(r3.toString());
assertNotNull(r3.toString());
}
}