commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r835777 - in /commons/proper/lang/trunk/src: java/org/apache/commons/lang/Range.java test/org/apache/commons/lang/RangeTest.java
Date Fri, 13 Nov 2009 08:51:09 GMT
Author: bayard
Date: Fri Nov 13 08:51:08 2009
New Revision: 835777

URL: http://svn.apache.org/viewvc?rev=835777&view=rev
Log:
Adding elementCompareTo method as requested in LANG-386 (which called the method positionOfXxx)

Modified:
    commons/proper/lang/trunk/src/java/org/apache/commons/lang/Range.java
    commons/proper/lang/trunk/src/test/org/apache/commons/lang/RangeTest.java

Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/Range.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/Range.java?rev=835777&r1=835776&r2=835777&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/Range.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/Range.java Fri Nov 13 08:51:08
2009
@@ -199,6 +199,30 @@
         return this.comparator.compare(element, getMaximum()) > 0;
     }
 
+    /**
+     * <p>Tests where the specified element occurs relative to this range.</p>
+     * <p>The API is reminiscent of the Comparable interface returning <code>-1</code>
if 
+     * the element is before the range, <code>0</code> if contained within the
range and 
+     * <code>1</code> if the element is after the range. </p>
+     *
+     * @param element  the element to test
+     * @return -1, 0 or +1 depending on the element's location relative to the range
+     */
+    public int elementCompareTo(T element) {
+        if(element == null) {
+            // Comparable API says throw NPE on null
+            throw new NullPointerException("Element is null");
+        }
+        if(elementBefore(element)) {
+            return -1;
+        } else
+        if(elementAfter(element)) {
+            return 1;
+        } else {
+            return 0;
+        }
+    }
+
     // Range tests
     //--------------------------------------------------------------------
 

Modified: commons/proper/lang/trunk/src/test/org/apache/commons/lang/RangeTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang/RangeTest.java?rev=835777&r1=835776&r2=835777&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/org/apache/commons/lang/RangeTest.java (original)
+++ commons/proper/lang/trunk/src/test/org/apache/commons/lang/RangeTest.java Fri Nov 13 08:51:08
2009
@@ -146,6 +146,21 @@
         assertTrue(intRange.elementAfter(25));
     }
 
+    public void testElementCompareTo() {
+        try {
+            intRange.elementCompareTo(null);
+            fail("NullPointerException should have been thrown");
+        } catch(NullPointerException npe) {
+            // expected
+        }
+        
+        assertEquals(-1, intRange.elementCompareTo(5));
+        assertEquals(0, intRange.elementCompareTo(10));
+        assertEquals(0, intRange.elementCompareTo(15));
+        assertEquals(0, intRange.elementCompareTo(20));
+        assertEquals(1, intRange.elementCompareTo(25));
+    }
+
     // --------------------------------------------------------------------------
 
     public void testContainsRange() {



Mime
View raw message