Switched to static builder methods, allowing two of the constructors to restrict T to Comparables, and removed javadoc that discussed subclasses optimising methods
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@882854 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
5839d3e011
commit
5adce14980
|
@ -45,9 +45,8 @@ public class Range<T> implements Serializable {
|
|||
* @throws IllegalArgumentException if the value is <code>null</code>
|
||||
* @throws ClassCastException if the value is not Comparable
|
||||
*/
|
||||
// TODO: Ideally the ClassCastException would be compile-time via generics
|
||||
public Range(T element) {
|
||||
this( element, element);
|
||||
public static <T extends Comparable> Range is(T element) {
|
||||
return new Range(element, element, ComparableComparator.INSTANCE);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -64,9 +63,8 @@ public class Range<T> implements Serializable {
|
|||
* @throws IllegalArgumentException if either value is <code>null</code>
|
||||
* @throws ClassCastException if either value is not Comparable
|
||||
*/
|
||||
// TODO: Ideally the ClassCastException would be compile-time via generics
|
||||
public Range(T element1, T element2) {
|
||||
this( element1, element2, ComparableComparator.INSTANCE);
|
||||
public static <T extends Comparable> Range between(T element1, T element2) {
|
||||
return new Range( element1, element2, ComparableComparator.INSTANCE);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -79,8 +77,8 @@ public class Range<T> implements Serializable {
|
|||
* @param c comparator to be used
|
||||
* @throws IllegalArgumentException if the value is <code>null</code>
|
||||
*/
|
||||
public Range(T element, Comparator c) {
|
||||
this(element, element, c);
|
||||
public static <T> Range is(T element, Comparator c) {
|
||||
return new Range(element, element, c);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -97,7 +95,11 @@ public class Range<T> implements Serializable {
|
|||
* @param c comparator to be used
|
||||
* @throws IllegalArgumentException if either value is <code>null</code>
|
||||
*/
|
||||
public Range(T element1, T element2, Comparator c) {
|
||||
public static <T> Range between(T element1, T element2, Comparator c) {
|
||||
return new Range(element1, element2, c);
|
||||
}
|
||||
|
||||
private Range(T element1, T element2, Comparator c) {
|
||||
if(element1 == null || element2 == null) {
|
||||
throw new IllegalArgumentException("Elements in a range must not be null: element1=" +
|
||||
element1 + ", element2=" + element2);
|
||||
|
@ -233,15 +235,8 @@ public class Range<T> implements Serializable {
|
|||
/**
|
||||
* <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 #contains(Object)} 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>
|
||||
|
@ -258,16 +253,8 @@ public class Range<T> implements Serializable {
|
|||
/**
|
||||
* <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 #contains(Object)} 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>
|
||||
|
@ -290,10 +277,6 @@ public class Range<T> implements Serializable {
|
|||
*
|
||||
* <p>To be equal, the class, minimum and maximum must be equal.</p>
|
||||
*
|
||||
* <p>This implementation uses the {@link #getMinimum()} and
|
||||
* {@link #getMaximum()} 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
|
||||
*/
|
||||
|
@ -313,10 +296,6 @@ public class Range<T> implements Serializable {
|
|||
/**
|
||||
* <p>Gets a hashCode for the range.</p>
|
||||
*
|
||||
* <p>This implementation uses the {@link #getMinimum()} and
|
||||
* {@link #getMaximum()} methods.
|
||||
* Subclasses may be able to optimise this.</p>
|
||||
*
|
||||
* @return a hash code value for this object
|
||||
*/
|
||||
@Override
|
||||
|
@ -333,10 +312,6 @@ public class Range<T> implements Serializable {
|
|||
*
|
||||
* <p>The format of the String is 'Range[<i>min</i>,<i>max</i>]'.</p>
|
||||
*
|
||||
* <p>This implementation uses the {@link #getMinimum()} and
|
||||
* {@link #getMaximum()} methods.
|
||||
* Subclasses may be able to optimise this.</p>
|
||||
*
|
||||
* @return the <code>String</code> representation of this range
|
||||
*/
|
||||
@Override
|
||||
|
|
|
@ -39,34 +39,30 @@ public class RangeTest extends TestCase {
|
|||
|
||||
@Override
|
||||
public void setUp() {
|
||||
byteRange = new Range((byte) 0, (byte) 5);
|
||||
byteRange2 = new Range((byte) 0, (byte) 5);
|
||||
byteRange3 = new Range((byte) 0, (byte) 10);
|
||||
byteRange = Range.between((byte) 0, (byte) 5);
|
||||
byteRange2 = Range.between((byte) 0, (byte) 5);
|
||||
byteRange3 = Range.between((byte) 0, (byte) 10);
|
||||
|
||||
intRange = new Range<Integer>((int) 10, (int) 20);
|
||||
longRange = new Range<Long>((long) 10, (long) 20);
|
||||
floatRange = new Range<Float>((float) 10, (float) 20);
|
||||
doubleRange = new Range<Double>((double) 10, (double) 20);
|
||||
intRange = Range.between((int) 10, (int) 20);
|
||||
longRange = Range.between((long) 10, (long) 20);
|
||||
floatRange = Range.between((float) 10, (float) 20);
|
||||
doubleRange = Range.between((double) 10, (double) 20);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
public void testComparableConstructors() {
|
||||
try {
|
||||
Range range = new Range(new Object());
|
||||
fail("IllegalArgumentException expected");
|
||||
} catch(ClassCastException cce) {
|
||||
// expected
|
||||
}
|
||||
|
||||
try {
|
||||
Range range = new Range(new Object(), new Object());
|
||||
fail("ClassCastException expected");
|
||||
} catch(ClassCastException cce) {
|
||||
// expected
|
||||
}
|
||||
Comparable c =
|
||||
new Comparable() {
|
||||
public int compareTo(Object other) {
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
Range.is(c);
|
||||
Range.between(c, c);
|
||||
}
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
public void testEqualsObject() {
|
||||
|
@ -95,7 +91,7 @@ public class RangeTest extends TestCase {
|
|||
String str = intRange.toString();
|
||||
assertEquals("Range[10,20]", str);
|
||||
// assertSame(str, intRange.toString()); // no longer passes - does it matter?
|
||||
assertEquals("Range[-20,-10]", new Range<Integer>(-20, -10).toString());
|
||||
assertEquals("Range[-20,-10]", Range.between(-20, -10).toString());
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
@ -167,29 +163,29 @@ public class RangeTest extends TestCase {
|
|||
assertFalse(intRange.containsRange(null));
|
||||
|
||||
// easy inside range
|
||||
assertTrue(intRange.containsRange(new Range(12, 18)));
|
||||
assertTrue(intRange.containsRange(Range.between(12, 18)));
|
||||
|
||||
// outside range on each side
|
||||
assertFalse(intRange.containsRange(new Range(32, 45)));
|
||||
assertFalse(intRange.containsRange(new Range(2, 8)));
|
||||
assertFalse(intRange.containsRange(Range.between(32, 45)));
|
||||
assertFalse(intRange.containsRange(Range.between(2, 8)));
|
||||
|
||||
// equals range
|
||||
assertTrue(intRange.containsRange(new Range(10, 20)));
|
||||
assertTrue(intRange.containsRange(Range.between(10, 20)));
|
||||
|
||||
// overlaps
|
||||
assertFalse(intRange.containsRange(new Range(9, 14)));
|
||||
assertFalse(intRange.containsRange(new Range(16, 21)));
|
||||
assertFalse(intRange.containsRange(Range.between(9, 14)));
|
||||
assertFalse(intRange.containsRange(Range.between(16, 21)));
|
||||
|
||||
// touches lower boundary
|
||||
assertTrue(intRange.containsRange(new Range(10, 19)));
|
||||
assertFalse(intRange.containsRange(new Range(10, 21)));
|
||||
assertTrue(intRange.containsRange(Range.between(10, 19)));
|
||||
assertFalse(intRange.containsRange(Range.between(10, 21)));
|
||||
|
||||
// touches upper boundary
|
||||
assertTrue(intRange.containsRange(new Range(11, 20)));
|
||||
assertFalse(intRange.containsRange(new Range(9, 20)));
|
||||
assertTrue(intRange.containsRange(Range.between(11, 20)));
|
||||
assertFalse(intRange.containsRange(Range.between(9, 20)));
|
||||
|
||||
// negative
|
||||
assertFalse(intRange.containsRange(new Range(-11, -18)));
|
||||
assertFalse(intRange.containsRange(Range.between(-11, -18)));
|
||||
|
||||
}
|
||||
|
||||
|
@ -199,29 +195,29 @@ public class RangeTest extends TestCase {
|
|||
assertFalse(intRange.overlapsRange(null));
|
||||
|
||||
// easy inside range
|
||||
assertTrue(intRange.overlapsRange(new Range(12, 18)));
|
||||
assertTrue(intRange.overlapsRange(Range.between(12, 18)));
|
||||
|
||||
// outside range on each side
|
||||
assertFalse(intRange.overlapsRange(new Range(32, 45)));
|
||||
assertFalse(intRange.overlapsRange(new Range(2, 8)));
|
||||
assertFalse(intRange.overlapsRange(Range.between(32, 45)));
|
||||
assertFalse(intRange.overlapsRange(Range.between(2, 8)));
|
||||
|
||||
// equals range
|
||||
assertTrue(intRange.overlapsRange(new Range(10, 20)));
|
||||
assertTrue(intRange.overlapsRange(Range.between(10, 20)));
|
||||
|
||||
// overlaps
|
||||
assertTrue(intRange.overlapsRange(new Range(9, 14)));
|
||||
assertTrue(intRange.overlapsRange(new Range(16, 21)));
|
||||
assertTrue(intRange.overlapsRange(Range.between(9, 14)));
|
||||
assertTrue(intRange.overlapsRange(Range.between(16, 21)));
|
||||
|
||||
// touches lower boundary
|
||||
assertTrue(intRange.overlapsRange(new Range(10, 19)));
|
||||
assertTrue(intRange.overlapsRange(new Range(10, 21)));
|
||||
assertTrue(intRange.overlapsRange(Range.between(10, 19)));
|
||||
assertTrue(intRange.overlapsRange(Range.between(10, 21)));
|
||||
|
||||
// touches upper boundary
|
||||
assertTrue(intRange.overlapsRange(new Range(11, 20)));
|
||||
assertTrue(intRange.overlapsRange(new Range(9, 20)));
|
||||
assertTrue(intRange.overlapsRange(Range.between(11, 20)));
|
||||
assertTrue(intRange.overlapsRange(Range.between(9, 20)));
|
||||
|
||||
// negative
|
||||
assertFalse(intRange.overlapsRange(new Range(-11, -18)));
|
||||
assertFalse(intRange.overlapsRange(Range.between(-11, -18)));
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue