mirror of
https://github.com/apache/commons-lang.git
synced 2025-02-10 20:15:00 +00:00
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 IllegalArgumentException if the value is <code>null</code>
|
||||||
* @throws ClassCastException if the value is not Comparable
|
* @throws ClassCastException if the value is not Comparable
|
||||||
*/
|
*/
|
||||||
// TODO: Ideally the ClassCastException would be compile-time via generics
|
public static <T extends Comparable> Range is(T element) {
|
||||||
public Range(T element) {
|
return new Range(element, element, ComparableComparator.INSTANCE);
|
||||||
this( element, element);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -64,9 +63,8 @@ public Range(T element) {
|
|||||||
* @throws IllegalArgumentException if either value is <code>null</code>
|
* @throws IllegalArgumentException if either value is <code>null</code>
|
||||||
* @throws ClassCastException if either value is not Comparable
|
* @throws ClassCastException if either value is not Comparable
|
||||||
*/
|
*/
|
||||||
// TODO: Ideally the ClassCastException would be compile-time via generics
|
public static <T extends Comparable> Range between(T element1, T element2) {
|
||||||
public Range(T element1, T element2) {
|
return new Range( element1, element2, ComparableComparator.INSTANCE);
|
||||||
this( element1, element2, ComparableComparator.INSTANCE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -79,8 +77,8 @@ public Range(T element1, T element2) {
|
|||||||
* @param c comparator to be used
|
* @param c comparator to be used
|
||||||
* @throws IllegalArgumentException if the value is <code>null</code>
|
* @throws IllegalArgumentException if the value is <code>null</code>
|
||||||
*/
|
*/
|
||||||
public Range(T element, Comparator c) {
|
public static <T> Range is(T element, Comparator c) {
|
||||||
this(element, element, c);
|
return new Range(element, element, c);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -97,7 +95,11 @@ public Range(T element, Comparator c) {
|
|||||||
* @param c comparator to be used
|
* @param c comparator to be used
|
||||||
* @throws IllegalArgumentException if either value is <code>null</code>
|
* @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) {
|
if(element1 == null || element2 == null) {
|
||||||
throw new IllegalArgumentException("Elements in a range must not be null: element1=" +
|
throw new IllegalArgumentException("Elements in a range must not be null: element1=" +
|
||||||
element1 + ", element2=" + element2);
|
element1 + ", element2=" + element2);
|
||||||
@ -233,15 +235,8 @@ public int elementCompareTo(T element) {
|
|||||||
/**
|
/**
|
||||||
* <p>Tests whether the specified range occurs entirely within this range.</p>
|
* <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><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>
|
* @param range the range to test, may be <code>null</code>
|
||||||
* @return <code>true</code> if the specified range occurs entirely within
|
* @return <code>true</code> if the specified range occurs entirely within
|
||||||
* this range; otherwise, <code>false</code>
|
* this range; otherwise, <code>false</code>
|
||||||
@ -258,16 +253,8 @@ public boolean containsRange(Range<T> range) {
|
|||||||
/**
|
/**
|
||||||
* <p>Tests whether the specified range overlaps with this range.</p>
|
* <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><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>
|
* @param range the range to test, may be <code>null</code>
|
||||||
* @return <code>true</code> if the specified range overlaps with this
|
* @return <code>true</code> if the specified range overlaps with this
|
||||||
* range; otherwise, <code>false</code>
|
* range; otherwise, <code>false</code>
|
||||||
@ -290,10 +277,6 @@ public boolean overlapsRange(Range<T> range) {
|
|||||||
*
|
*
|
||||||
* <p>To be equal, the class, minimum and maximum must be equal.</p>
|
* <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
|
* @param obj the reference object with which to compare
|
||||||
* @return <code>true</code> if this object is equal
|
* @return <code>true</code> if this object is equal
|
||||||
*/
|
*/
|
||||||
@ -313,10 +296,6 @@ public boolean equals(Object obj) {
|
|||||||
/**
|
/**
|
||||||
* <p>Gets a hashCode for the range.</p>
|
* <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
|
* @return a hash code value for this object
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
@ -333,10 +312,6 @@ public int hashCode() {
|
|||||||
*
|
*
|
||||||
* <p>The format of the String is 'Range[<i>min</i>,<i>max</i>]'.</p>
|
* <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
|
* @return the <code>String</code> representation of this range
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
|
@ -39,33 +39,29 @@ public class RangeTest extends TestCase {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
byteRange = new Range((byte) 0, (byte) 5);
|
byteRange = Range.between((byte) 0, (byte) 5);
|
||||||
byteRange2 = new Range((byte) 0, (byte) 5);
|
byteRange2 = Range.between((byte) 0, (byte) 5);
|
||||||
byteRange3 = new Range((byte) 0, (byte) 10);
|
byteRange3 = Range.between((byte) 0, (byte) 10);
|
||||||
|
|
||||||
intRange = new Range<Integer>((int) 10, (int) 20);
|
intRange = Range.between((int) 10, (int) 20);
|
||||||
longRange = new Range<Long>((long) 10, (long) 20);
|
longRange = Range.between((long) 10, (long) 20);
|
||||||
floatRange = new Range<Float>((float) 10, (float) 20);
|
floatRange = Range.between((float) 10, (float) 20);
|
||||||
doubleRange = new Range<Double>((double) 10, (double) 20);
|
doubleRange = Range.between((double) 10, (double) 20);
|
||||||
}
|
}
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
|
|
||||||
public void testComparableConstructors() {
|
public void testComparableConstructors() {
|
||||||
try {
|
Comparable c =
|
||||||
Range range = new Range(new Object());
|
new Comparable() {
|
||||||
fail("IllegalArgumentException expected");
|
public int compareTo(Object other) {
|
||||||
} catch(ClassCastException cce) {
|
return 1;
|
||||||
// expected
|
}
|
||||||
|
};
|
||||||
|
Range.is(c);
|
||||||
|
Range.between(c, c);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
Range range = new Range(new Object(), new Object());
|
|
||||||
fail("ClassCastException expected");
|
|
||||||
} catch(ClassCastException cce) {
|
|
||||||
// expected
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
|
|
||||||
@ -95,7 +91,7 @@ public void testToString() {
|
|||||||
String str = intRange.toString();
|
String str = intRange.toString();
|
||||||
assertEquals("Range[10,20]", str);
|
assertEquals("Range[10,20]", str);
|
||||||
// assertSame(str, intRange.toString()); // no longer passes - does it matter?
|
// 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 void testContainsRange() {
|
|||||||
assertFalse(intRange.containsRange(null));
|
assertFalse(intRange.containsRange(null));
|
||||||
|
|
||||||
// easy inside range
|
// easy inside range
|
||||||
assertTrue(intRange.containsRange(new Range(12, 18)));
|
assertTrue(intRange.containsRange(Range.between(12, 18)));
|
||||||
|
|
||||||
// outside range on each side
|
// outside range on each side
|
||||||
assertFalse(intRange.containsRange(new Range(32, 45)));
|
assertFalse(intRange.containsRange(Range.between(32, 45)));
|
||||||
assertFalse(intRange.containsRange(new Range(2, 8)));
|
assertFalse(intRange.containsRange(Range.between(2, 8)));
|
||||||
|
|
||||||
// equals range
|
// equals range
|
||||||
assertTrue(intRange.containsRange(new Range(10, 20)));
|
assertTrue(intRange.containsRange(Range.between(10, 20)));
|
||||||
|
|
||||||
// overlaps
|
// overlaps
|
||||||
assertFalse(intRange.containsRange(new Range(9, 14)));
|
assertFalse(intRange.containsRange(Range.between(9, 14)));
|
||||||
assertFalse(intRange.containsRange(new Range(16, 21)));
|
assertFalse(intRange.containsRange(Range.between(16, 21)));
|
||||||
|
|
||||||
// touches lower boundary
|
// touches lower boundary
|
||||||
assertTrue(intRange.containsRange(new Range(10, 19)));
|
assertTrue(intRange.containsRange(Range.between(10, 19)));
|
||||||
assertFalse(intRange.containsRange(new Range(10, 21)));
|
assertFalse(intRange.containsRange(Range.between(10, 21)));
|
||||||
|
|
||||||
// touches upper boundary
|
// touches upper boundary
|
||||||
assertTrue(intRange.containsRange(new Range(11, 20)));
|
assertTrue(intRange.containsRange(Range.between(11, 20)));
|
||||||
assertFalse(intRange.containsRange(new Range(9, 20)));
|
assertFalse(intRange.containsRange(Range.between(9, 20)));
|
||||||
|
|
||||||
// negative
|
// negative
|
||||||
assertFalse(intRange.containsRange(new Range(-11, -18)));
|
assertFalse(intRange.containsRange(Range.between(-11, -18)));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -199,29 +195,29 @@ public void testOverlapsRange() {
|
|||||||
assertFalse(intRange.overlapsRange(null));
|
assertFalse(intRange.overlapsRange(null));
|
||||||
|
|
||||||
// easy inside range
|
// easy inside range
|
||||||
assertTrue(intRange.overlapsRange(new Range(12, 18)));
|
assertTrue(intRange.overlapsRange(Range.between(12, 18)));
|
||||||
|
|
||||||
// outside range on each side
|
// outside range on each side
|
||||||
assertFalse(intRange.overlapsRange(new Range(32, 45)));
|
assertFalse(intRange.overlapsRange(Range.between(32, 45)));
|
||||||
assertFalse(intRange.overlapsRange(new Range(2, 8)));
|
assertFalse(intRange.overlapsRange(Range.between(2, 8)));
|
||||||
|
|
||||||
// equals range
|
// equals range
|
||||||
assertTrue(intRange.overlapsRange(new Range(10, 20)));
|
assertTrue(intRange.overlapsRange(Range.between(10, 20)));
|
||||||
|
|
||||||
// overlaps
|
// overlaps
|
||||||
assertTrue(intRange.overlapsRange(new Range(9, 14)));
|
assertTrue(intRange.overlapsRange(Range.between(9, 14)));
|
||||||
assertTrue(intRange.overlapsRange(new Range(16, 21)));
|
assertTrue(intRange.overlapsRange(Range.between(16, 21)));
|
||||||
|
|
||||||
// touches lower boundary
|
// touches lower boundary
|
||||||
assertTrue(intRange.overlapsRange(new Range(10, 19)));
|
assertTrue(intRange.overlapsRange(Range.between(10, 19)));
|
||||||
assertTrue(intRange.overlapsRange(new Range(10, 21)));
|
assertTrue(intRange.overlapsRange(Range.between(10, 21)));
|
||||||
|
|
||||||
// touches upper boundary
|
// touches upper boundary
|
||||||
assertTrue(intRange.overlapsRange(new Range(11, 20)));
|
assertTrue(intRange.overlapsRange(Range.between(11, 20)));
|
||||||
assertTrue(intRange.overlapsRange(new Range(9, 20)));
|
assertTrue(intRange.overlapsRange(Range.between(9, 20)));
|
||||||
|
|
||||||
// negative
|
// negative
|
||||||
assertFalse(intRange.overlapsRange(new Range(-11, -18)));
|
assertFalse(intRange.overlapsRange(Range.between(-11, -18)));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user