diff --git a/src/java/org/apache/commons/lang/Range.java b/src/java/org/apache/commons/lang/Range.java index 73471dbfe..15b928753 100644 --- a/src/java/org/apache/commons/lang/Range.java +++ b/src/java/org/apache/commons/lang/Range.java @@ -160,7 +160,6 @@ public class Range { * * @param element the element to test, may be null * @return true if the specified element occurs within this range - * @throws IllegalArgumentException if the Number cannot be compared */ public boolean contains(T element) { if(element == null) { @@ -169,20 +168,32 @@ public class Range { return (comparator.compare(element, getMinimum()) > -1) && (comparator.compare(element, getMaximum()) < 1); } - public boolean lessThan(T element) { + /** + *

Tests whether the specified element occurs before this range.

+ * + * @param element the element to test + * @return true if the specified element occurs before this range + */ + public boolean elementBefore(T element) { if (element == null) { - return false; + return false; // ?? } - return this.comparator.compare(getMinimum(), element) < 1; + return this.comparator.compare(element, getMinimum()) < 0; } - public boolean greaterThan(T element) { + /** + *

Tests whether the specified element occurs after this range.

+ * + * @param element the element to test + * @return true if the specified element occurs after this range + */ + public boolean elementAfter(T element) { if (element == null) { - return false; + return false; // ?? } - return this.comparator.compare(getMaximum(), element) > -1; + return this.comparator.compare(element, getMaximum()) > 0; } // Range tests diff --git a/src/test/org/apache/commons/lang/RangeTest.java b/src/test/org/apache/commons/lang/RangeTest.java index 066f89382..a7ab9ec53 100644 --- a/src/test/org/apache/commons/lang/RangeTest.java +++ b/src/test/org/apache/commons/lang/RangeTest.java @@ -51,9 +51,6 @@ public class RangeTest extends TestCase { doubleRange = new Range((double) 10, (double) 20); } - /** - * Test method for 'org.apache.commons.lang.Range.equals(Object)' - */ public void testEqualsObject() { assertEquals(byteRange, byteRange); assertEquals(byteRange, byteRange2); @@ -66,22 +63,25 @@ public class RangeTest extends TestCase { assertFalse(byteRange2.equals("Ni!")); } - /** - * Test method for 'org.apache.commons.lang.Range.hashCode()' - */ public void testHashCode() { assertEquals(byteRange.hashCode(), byteRange2.hashCode()); assertFalse(byteRange.hashCode() == byteRange3.hashCode()); + + assertEquals(intRange.hashCode(), intRange.hashCode()); + assertTrue(intRange.hashCode() != 0); } - /** - * Test method for 'org.apache.commons.lang.Range.toString()' - */ public void testToString() { assertNotNull(byteRange.toString()); + + 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(-20, -10).toString()); } // -------------------------------------------------------------------------- + public void testGetMinimum() { assertEquals(10, (int) intRange.getMinimum()); assertEquals(10L, (long) longRange.getMinimum()); @@ -106,6 +106,28 @@ public class RangeTest extends TestCase { assertFalse(intRange.contains(25)); } + public void testElementBefore() { + assertFalse(intRange.elementBefore(null)); + + assertTrue(intRange.elementBefore(5)); + assertFalse(intRange.elementBefore(10)); + assertFalse(intRange.elementBefore(15)); + assertFalse(intRange.elementBefore(20)); + assertFalse(intRange.elementBefore(25)); + } + + public void testElementAfter() { + assertFalse(intRange.elementAfter(null)); + + assertFalse(intRange.elementAfter(5)); + assertFalse(intRange.elementAfter(10)); + assertFalse(intRange.elementAfter(15)); + assertFalse(intRange.elementAfter(20)); + assertTrue(intRange.elementAfter(25)); + } + + // -------------------------------------------------------------------------- + public void testContainsRange() { // null handling @@ -138,4 +160,36 @@ public class RangeTest extends TestCase { } + public void testOverlapsRange() { + + // null handling + assertFalse(intRange.overlapsRange(null)); + + // easy inside range + assertTrue(intRange.overlapsRange(new Range(12, 18))); + + // outside range on each side + assertFalse(intRange.overlapsRange(new Range(32, 45))); + assertFalse(intRange.overlapsRange(new Range(2, 8))); + + // equals range + assertTrue(intRange.overlapsRange(new Range(10, 20))); + + // overlaps + assertTrue(intRange.overlapsRange(new Range(9, 14))); + assertTrue(intRange.overlapsRange(new Range(16, 21))); + + // touches lower boundary + assertTrue(intRange.overlapsRange(new Range(10, 19))); + assertTrue(intRange.overlapsRange(new Range(10, 21))); + + // touches upper boundary + assertTrue(intRange.overlapsRange(new Range(11, 20))); + assertTrue(intRange.overlapsRange(new Range(9, 20))); + + // negative + assertFalse(intRange.overlapsRange(new Range(-11, -18))); + + } + }