Added test for contains method, along with a null fix for contains and improved javadoc - LANG-551
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@833337 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/java/org/apache/commons/lang/Range.java b/src/java/org/apache/commons/lang/Range.java
index 1a47220..73471db 100644
--- a/src/java/org/apache/commons/lang/Range.java
+++ b/src/java/org/apache/commons/lang/Range.java
@@ -154,22 +154,19 @@
//--------------------------------------------------------------------
/**
- * <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>Tests whether the specified element 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
+ * @param element the element to test, may be <code>null</code>
+ * @return <code>true</code> if the specified element occurs within this range
* @throws IllegalArgumentException if the <code>Number</code> cannot be compared
*/
- public boolean contains(T t) {
-// TODO: Rewrite in terms of !lessThan and !greaterThan?
- return (comparator.compare(t, getMinimum()) > -1) && (comparator.compare(t, getMaximum()) < 1);
+ public boolean contains(T element) {
+ if(element == null) {
+ return false;
+ }
+ return (comparator.compare(element, getMinimum()) > -1) && (comparator.compare(element, getMaximum()) < 1);
}
public boolean lessThan(T element) {
@@ -177,7 +174,7 @@
return false;
}
- return this.comparator.compare(this.getMinimum(), element) < 1;
+ return this.comparator.compare(getMinimum(), element) < 1;
}
public boolean greaterThan(T element) {
diff --git a/src/test/org/apache/commons/lang/RangeTest.java b/src/test/org/apache/commons/lang/RangeTest.java
index cacc435..3e1f569 100644
--- a/src/test/org/apache/commons/lang/RangeTest.java
+++ b/src/test/org/apache/commons/lang/RangeTest.java
@@ -95,4 +95,15 @@
assertEquals(20f, floatRange.getMaximum(), 0.00001f);
assertEquals(20d, doubleRange.getMaximum(), 0.00001d);
}
+
+ public void testContains() {
+ assertFalse(intRange.contains(null));
+
+ assertFalse(intRange.contains(5));
+ assertTrue(intRange.contains(10));
+ assertTrue(intRange.contains(15));
+ assertTrue(intRange.contains(20));
+ assertFalse(intRange.contains(25));
+ }
+
}