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
This commit is contained in:
Henri Yandell 2009-11-06 10:47:15 +00:00
parent ff1102d68d
commit bd900f6be7
2 changed files with 20 additions and 12 deletions

View File

@ -154,22 +154,19 @@ public boolean isDefaultNaturalOrdering() {
//--------------------------------------------------------------------
/**
* <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 @@ public boolean lessThan(T element) {
return false;
}
return this.comparator.compare(this.getMinimum(), element) < 1;
return this.comparator.compare(getMinimum(), element) < 1;
}
public boolean greaterThan(T element) {

View File

@ -95,4 +95,15 @@ public void testGetMaximum() {
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));
}
}