commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r882854 - in /commons/proper/lang/trunk/src: java/org/apache/commons/lang/Range.java test/org/apache/commons/lang/RangeTest.java
Date Sat, 21 Nov 2009 08:05:56 GMT
Author: bayard
Date: Sat Nov 21 08:05:55 2009
New Revision: 882854

URL: http://svn.apache.org/viewvc?rev=882854&view=rev
Log:
Switched to static builder methods, allowing two of the constructors to restrict T to Comparables,
and removed javadoc that discussed subclasses optimising methods

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=882854&r1=882853&r2=882854&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 Sat Nov 21 08:05:55
2009
@@ -45,9 +45,8 @@
      * @throws IllegalArgumentException if the value is <code>null</code>
      * @throws ClassCastException if the value is not Comparable
      */
-    // TODO: Ideally the ClassCastException would be compile-time via generics
-    public Range(T element) {
-        this( element, element);
+    public static <T extends Comparable> Range is(T element) {
+        return new Range(element, element, ComparableComparator.INSTANCE);
     }
 
     /**
@@ -64,9 +63,8 @@
      * @throws IllegalArgumentException if either value is <code>null</code>
      * @throws ClassCastException if either value is not Comparable
      */
-    // TODO: Ideally the ClassCastException would be compile-time via generics
-    public Range(T element1, T element2) {
-        this( element1, element2, ComparableComparator.INSTANCE);
+    public static <T extends Comparable> Range between(T element1, T element2) {
+        return new Range( element1, element2, ComparableComparator.INSTANCE);
     }
 
     /**
@@ -79,8 +77,8 @@
      * @param c comparator to be used
      * @throws IllegalArgumentException if the value is <code>null</code>
      */
-    public Range(T element, Comparator c) {
-        this(element, element, c);
+    public static <T> Range is(T element, Comparator c) {
+        return new Range(element, element, c);
     }
 
     /**
@@ -97,7 +95,11 @@
      * @param c comparator to be used
      * @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) {
             throw new IllegalArgumentException("Elements in a range must not be null: element1="
+ 
                                                element1 + ", element2=" + element2);
@@ -233,15 +235,8 @@
     /**
      * <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>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>
      * @return <code>true</code> if the specified range occurs entirely within
      *  this range; otherwise, <code>false</code>
@@ -258,16 +253,8 @@
     /**
      * <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>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>
      * @return <code>true</code> if the specified range overlaps with this
      *  range; otherwise, <code>false</code>
@@ -290,10 +277,6 @@
      * 
      * <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
      * @return <code>true</code> if this object is equal
      */
@@ -313,10 +296,6 @@
     /**
      * <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
      */
     @Override
@@ -333,10 +312,6 @@
      *
      * <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
      */
     @Override

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=882854&r1=882853&r2=882854&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 Sat Nov 21 08:05:55
2009
@@ -39,34 +39,30 @@
 
     @Override
     public void setUp() {
-        byteRange   = new Range((byte) 0, (byte) 5);
-        byteRange2  = new Range((byte) 0, (byte) 5);
-        byteRange3  = new Range((byte) 0, (byte) 10);
-
-        intRange    = new Range<Integer>((int) 10, (int) 20);
-        longRange   = new Range<Long>((long) 10, (long) 20);
-        floatRange  = new Range<Float>((float) 10, (float) 20);
-        doubleRange = new Range<Double>((double) 10, (double) 20);
+        byteRange   = Range.between((byte) 0, (byte) 5);
+        byteRange2  = Range.between((byte) 0, (byte) 5);
+        byteRange3  = Range.between((byte) 0, (byte) 10);
+
+        intRange    = Range.between((int) 10, (int) 20);
+        longRange   = Range.between((long) 10, (long) 20);
+        floatRange  = Range.between((float) 10, (float) 20);
+        doubleRange = Range.between((double) 10, (double) 20);
     }
 
     // --------------------------------------------------------------------------
 
     public void testComparableConstructors() {
-        try {
-            Range range = new Range(new Object());
-            fail("IllegalArgumentException expected");
-        } catch(ClassCastException cce) {
-            // expected
-        }
-
-        try {
-            Range range = new Range(new Object(), new Object());
-            fail("ClassCastException expected");
-        } catch(ClassCastException cce) {
-            // expected
-        }
+        Comparable c = 
+            new Comparable() { 
+                public int compareTo(Object other) {
+                    return 1;
+                }
+            };
+        Range.is(c);
+        Range.between(c, c);
     }
 
+
     // --------------------------------------------------------------------------
 
     public void testEqualsObject() {
@@ -95,7 +91,7 @@
         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<Integer>(-20, -10).toString());
+        assertEquals("Range[-20,-10]", Range.between(-20, -10).toString());
     }
 
     // --------------------------------------------------------------------------
@@ -167,29 +163,29 @@
         assertFalse(intRange.containsRange(null));
 
         // easy inside range
-        assertTrue(intRange.containsRange(new Range(12, 18)));
+        assertTrue(intRange.containsRange(Range.between(12, 18)));
 
         // outside range on each side
-        assertFalse(intRange.containsRange(new Range(32, 45)));
-        assertFalse(intRange.containsRange(new Range(2, 8)));
+        assertFalse(intRange.containsRange(Range.between(32, 45)));
+        assertFalse(intRange.containsRange(Range.between(2, 8)));
 
         // equals range
-        assertTrue(intRange.containsRange(new Range(10, 20)));
+        assertTrue(intRange.containsRange(Range.between(10, 20)));
 
         // overlaps
-        assertFalse(intRange.containsRange(new Range(9, 14)));
-        assertFalse(intRange.containsRange(new Range(16, 21)));
+        assertFalse(intRange.containsRange(Range.between(9, 14)));
+        assertFalse(intRange.containsRange(Range.between(16, 21)));
 
         // touches lower boundary
-        assertTrue(intRange.containsRange(new Range(10, 19)));
-        assertFalse(intRange.containsRange(new Range(10, 21)));
+        assertTrue(intRange.containsRange(Range.between(10, 19)));
+        assertFalse(intRange.containsRange(Range.between(10, 21)));
 
         // touches upper boundary
-        assertTrue(intRange.containsRange(new Range(11, 20)));
-        assertFalse(intRange.containsRange(new Range(9, 20)));
+        assertTrue(intRange.containsRange(Range.between(11, 20)));
+        assertFalse(intRange.containsRange(Range.between(9, 20)));
         
         // negative
-        assertFalse(intRange.containsRange(new Range(-11, -18)));
+        assertFalse(intRange.containsRange(Range.between(-11, -18)));
 
     }
 
@@ -199,29 +195,29 @@
         assertFalse(intRange.overlapsRange(null));
 
         // easy inside range
-        assertTrue(intRange.overlapsRange(new Range(12, 18)));
+        assertTrue(intRange.overlapsRange(Range.between(12, 18)));
 
         // outside range on each side
-        assertFalse(intRange.overlapsRange(new Range(32, 45)));
-        assertFalse(intRange.overlapsRange(new Range(2, 8)));
+        assertFalse(intRange.overlapsRange(Range.between(32, 45)));
+        assertFalse(intRange.overlapsRange(Range.between(2, 8)));
 
         // equals range
-        assertTrue(intRange.overlapsRange(new Range(10, 20)));
+        assertTrue(intRange.overlapsRange(Range.between(10, 20)));
 
         // overlaps
-        assertTrue(intRange.overlapsRange(new Range(9, 14)));
-        assertTrue(intRange.overlapsRange(new Range(16, 21)));
+        assertTrue(intRange.overlapsRange(Range.between(9, 14)));
+        assertTrue(intRange.overlapsRange(Range.between(16, 21)));
 
         // touches lower boundary
-        assertTrue(intRange.overlapsRange(new Range(10, 19)));
-        assertTrue(intRange.overlapsRange(new Range(10, 21)));
+        assertTrue(intRange.overlapsRange(Range.between(10, 19)));
+        assertTrue(intRange.overlapsRange(Range.between(10, 21)));
 
         // touches upper boundary
-        assertTrue(intRange.overlapsRange(new Range(11, 20)));
-        assertTrue(intRange.overlapsRange(new Range(9, 20)));
+        assertTrue(intRange.overlapsRange(Range.between(11, 20)));
+        assertTrue(intRange.overlapsRange(Range.between(9, 20)));
         
         // negative
-        assertFalse(intRange.overlapsRange(new Range(-11, -18)));
+        assertFalse(intRange.overlapsRange(Range.between(-11, -18)));
 
     }
 



Mime
View raw message