sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1443088 - in /sis/branches/JDK7/sis-utility/src: main/java/org/apache/sis/measure/Range.java test/java/org/apache/sis/measure/RangeTest.java
Date Wed, 06 Feb 2013 17:45:46 GMT
Author: desruisseaux
Date: Wed Feb  6 17:45:45 2013
New Revision: 1443088

URL: http://svn.apache.org/viewvc?rev=1443088&view=rev
Log:
Parameterized the range type, and added some javadoc.

Modified:
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/Range.java
    sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/measure/RangeTest.java

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/Range.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/Range.java?rev=1443088&r1=1443087&r2=1443088&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/Range.java (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/Range.java Wed Feb
 6 17:45:45 2013
@@ -16,32 +16,41 @@
  */
 package org.apache.sis.measure;
 
+
 /**
- * A range is a set of minimum and maximum values of a certain class, allowing
+ * A set of minimum and maximum values of a certain class, allowing
  * a user to determine if a value of the same class is contained inside the range.
  * The minimum and maximum values do not have to be included in the range, and
  * can be null.  If the minimum or maximum values are null, the range is said to
  * be unbounded on that extreme. If both the minimum and maximum are null,
  * the range is completely unbounded and all values of that class are contained
- * within the range.
+ * within the range. Null values are always considered <em>exclusive</em>,
+ * since iterations over the values will never reach the infinite bound.
+ *
+ * <p>To be a member of a {@code Range}, the class type defining the range must implement
+ * the {@link Comparable} interface.</p>
  *
- * To be a member of a Range, the class type defining the Range must implement
- * the Comparable interface.
+ * @param <T> The type of range elements, typically a {@link Number} subclass or {@link
java.util.Date}.
  *
- * @author Joe White
+ * @author  Joe White
+ * @since   0.3
+ * @version 0.3
+ * @module
+ *
+ * @see RangeFormat
  */
-public class Range
-{
+public class Range<T extends Comparable<? super T>> {
 
-    private Comparable minimumValue;
-    private Comparable maximumValue;
-    private Class rangeType;
-    private boolean isMinimumIncluded;
-    private boolean isMaximumIncluded;
+    private final T minimumValue;
+    private final T maximumValue;
+    private final Class<T> rangeType;
+    private final boolean isMinimumIncluded;
+    private final boolean isMaximumIncluded;
     private static String INVALID_TYPE_ERROR = "Type to be compared does not match the Range
type.";
 
-    public Range(Class elementClass, Comparable minValue, boolean isMinIncluded,
-          Comparable maxValue, boolean isMaxIncluded) throws IllegalArgumentException
+    public Range(final Class<T> elementClass,
+            final T minValue, final boolean isMinIncluded,
+            final T maxValue, final boolean isMaxIncluded) throws IllegalArgumentException
     {
         if(!checkConstructorArgs(elementClass, minValue, maxValue))
         {
@@ -54,8 +63,8 @@ public class Range
         isMaximumIncluded = isMaxIncluded;
     }
 
-    public Range(Class elementClass, Comparable minValue,
-            Comparable maxValue) throws IllegalArgumentException
+    public Range(final Class<T> elementClass, final T minValue,
+            final T maxValue) throws IllegalArgumentException
     {
         if(!checkConstructorArgs(elementClass, minValue, maxValue))
         {
@@ -69,7 +78,7 @@ public class Range
         isMaximumIncluded = true;
     }
 
-    public boolean contains(Comparable value) throws IllegalArgumentException
+    public boolean contains(final T value) throws IllegalArgumentException
     {
 
         boolean unbounded = (minimumValue == null && maximumValue == null);
@@ -166,7 +175,7 @@ public class Range
     }
 
 
-    public boolean contains(Range value) throws IllegalArgumentException
+    public boolean contains(final Range<T> value) throws IllegalArgumentException
     {
         if (!checkMethodArgs(value))
         {
@@ -176,7 +185,7 @@ public class Range
     }
 
 
-    public boolean intersects(Range value) throws IllegalArgumentException
+    public boolean intersects(final Range<T> value) throws IllegalArgumentException
     {
         if (!checkMethodArgs(value))
         {
@@ -186,7 +195,7 @@ public class Range
         return this.contains(value.getMinValue()) || this.contains(value.getMaxValue());
     }
 
-    public Range union(Range value) throws IllegalArgumentException
+    public Range<T> union(final Range<T> value) throws IllegalArgumentException
     {
         if (!checkMethodArgs(value))
         {
@@ -202,7 +211,7 @@ public class Range
         //get the min and max value of both sets, compare them, then take
         //the smallest of either and the largest of either and create
         //a new Range with them.
-        Comparable rangeMin, rangeMax;
+        T rangeMin, rangeMax;
         if (value.getMinValue().compareTo(minimumValue) <= 0)
         {
             rangeMin = value.getMinValue();
@@ -220,10 +229,10 @@ public class Range
         {
             rangeMax = maximumValue;
         }
-        return new Range(this.rangeType, rangeMin, rangeMax );
+        return new Range<>(this.rangeType, rangeMin, rangeMax );
     }
 
-    public Range intersect(Range value) throws IllegalArgumentException
+    public Range<T> intersect(final Range<T> value) throws IllegalArgumentException
     {
         if (!checkMethodArgs(value))
         {
@@ -233,7 +242,7 @@ public class Range
         //return empty set if the Ranges don't intersect
         if (!this.intersects(value))
         {
-            return new Range(rangeType, maximumValue, minimumValue);
+            return new Range<>(rangeType, maximumValue, minimumValue);
         }
 
         //if they are equal, return the passed in value
@@ -243,7 +252,7 @@ public class Range
         }
 
         //we knkow they intersect, the question is where.
-        Comparable rangeMin, rangeMax;
+        T rangeMin, rangeMax;
         if (this.contains(value.getMinValue()))
         {
             rangeMin = value.getMinValue();
@@ -262,18 +271,18 @@ public class Range
             rangeMax = maximumValue;
         }
 
-        return new Range(this.rangeType, rangeMin, rangeMax );
+        return new Range<>(this.rangeType, rangeMin, rangeMax );
 
     }
 
     //TODO: implement this
-    public Range[] subtract(Range value) throws IllegalArgumentException
+    public Range<T>[] subtract(final Range<T> value) throws IllegalArgumentException
     {
         if (!checkMethodArgs(value))
         {
             throw new IllegalArgumentException(INVALID_TYPE_ERROR);
         }
-        Range[] ranges = new Range[1];
+        Range<T>[] ranges = new Range[1];
         ranges[0] = null;
         return ranges;
     }
@@ -308,7 +317,7 @@ public class Range
         }
 
 
-        Range value = (Range)object;
+        Range<?> value = (Range<?>) object;
         if (value == null)
         {
             return false;
@@ -350,24 +359,24 @@ public class Range
         return isMaximumIncluded;
     }
 
-    public Class getElementClass()
+    public Class<T> getElementClass()
     {
         return rangeType;
     }
 
-    public Comparable getMinValue()
+    public T getMinValue()
     {
         return minimumValue;
     }
 
-    public Comparable getMaxValue()
+    public T getMaxValue()
     {
         return maximumValue;
     }
 
 
-    private boolean checkConstructorArgs(Class elementClass, Comparable minValue,
-            Comparable maxValue)
+    private static <T> boolean checkConstructorArgs(final Class<T> elementClass,
+            final T minValue, final T maxValue)
     {
         boolean retVal = true;
         if (minValue != null)
@@ -386,7 +395,7 @@ public class Range
         {
             Class[] interfaces = elementClass.getInterfaces();
             boolean comparableFound = false;
-            for (Class interf : interfaces)
+            for (Class<?> interf : interfaces)
             {
                 if (interf == Comparable.class)
                 {
@@ -398,8 +407,7 @@ public class Range
         return retVal;
     }
 
-    private boolean checkMethodArgs(Range value)
-    {
+    private boolean checkMethodArgs(final Range<T> value) {
         if (value == null)
         {
             return false;

Modified: sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/measure/RangeTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/measure/RangeTest.java?rev=1443088&r1=1443087&r2=1443088&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/measure/RangeTest.java (original)
+++ sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/measure/RangeTest.java Wed
Feb  6 17:45:45 2013
@@ -17,257 +17,313 @@
 package org.apache.sis.measure;
 
 import org.apache.sis.test.TestCase;
-import static org.junit.Assert.*;
 import org.junit.Test;
 
+import static org.junit.Assert.*;
+
+
 /**
- * Tests the @link Range class
+ * Tests the {@link Range} class.
  *
  * @author Joe White
  */
 public final strictfp class RangeTest extends TestCase {
-
-    @Test
-    public void testCtor()
-    {
-        Range intRange = new Range(Integer.class, new Integer(3), new Integer(5));
-        assertTrue(intRange.isMaxIncluded());
-        assertTrue(intRange.isMinIncluded());
-        assertFalse(intRange.isEmpty());
-
-        Range intRange1 = new Range(Integer.class, 3, false, 5, true);
-        assertTrue(intRange1.isMaxIncluded());
-        assertFalse(intRange1.isMinIncluded());
-        assertFalse(intRange1.isEmpty());
-
-        Range intRange2 = new Range(Integer.class, 2, true, 7, false);
-        assertFalse(intRange2.isMaxIncluded());
-        assertTrue(intRange2.isMinIncluded());
-        assertFalse(intRange2.isEmpty());
-
-        Range intRange3 = new Range(Integer.class, 3, false, 10, false);
-        assertFalse(intRange3.isMaxIncluded());
-        assertFalse(intRange3.isMinIncluded());
-        assertFalse(intRange3.isEmpty());
-
-        Range intRange4 = new Range(Integer.class, 10, 2);
-        assertTrue(intRange4.isEmpty());
-    }
-
-    @Test (expected=IllegalArgumentException.class)
-    public void testCtorErrors00()
-    {
-        Range doubleRange = new Range(Double.class, "error", "blast");
-    }
-
-    @Test (expected=IllegalArgumentException.class)
-    public void testCtorErrors01()
-    {
-        Range stringRange = new Range(String.class, 123.233, 8740.09);
-    }
-
-    @Test
-    public void testContains()
-    {
-        Range intRange = new Range(Integer.class, new Integer(3), new Integer(5));
-        assertTrue(intRange.contains(4));
-        assertFalse(intRange.contains(6));
-        assertFalse(intRange.contains(2));
-        assertTrue(intRange.contains(3));
-        assertTrue(intRange.contains(5));
-    }
-
-
-    @Test
-    public void testContainsNotInclusiveMinimum()
-    {
-        Range intRange = new Range(Integer.class, new Integer(2), false, new Integer(5),
true);
-        assertTrue(intRange.contains(4));
-        assertFalse(intRange.contains(6));
-        assertFalse(intRange.contains(2));
-        assertTrue(intRange.contains(3));
-        assertTrue(intRange.contains(5));
-
-    }
-
-    @Test
-    public void testContainsNotInclusiveMaximum()
-    {
-        Range intRange = new Range(Integer.class, new Integer(3), true, new Integer(6), false);
-        assertTrue(intRange.contains(4));
-        assertFalse(intRange.contains(6));
-        assertFalse(intRange.contains(2));
-        assertTrue(intRange.contains(3));
-        assertTrue(intRange.contains(5));
-    }
-
-    @Test
-    public void testContainsNoLowerBound()
-    {
-        Range intRange = new Range(Integer.class, null, new Integer(5));
-        assertTrue(intRange.contains(-555));
-        assertTrue(intRange.contains(5));
-        assertFalse(intRange.contains(6));
-    }
-
-    @Test
-    public void testContainsNoUpperBound()
-    {
-        Range intRange = new Range(Integer.class, new Integer(3), null);
-        assertFalse(intRange.contains(1));
-        assertTrue(intRange.contains(3));
-        assertTrue(intRange.contains(10000));
-    }
-
-    @Test
-    public void testContainsNoBounds()
-    {
-        Range intRange = new Range(Integer.class, null, null);
-        assertTrue(intRange.contains(-55555));
-        assertTrue(intRange.contains(100000));
-    }
-
-    @Test
-    public void testContainsRange()
-    {
-        Range intRange = new Range(Integer.class, -10, 10);
-        Range testRange = new Range(Integer.class, -5, 5);
-
-        assertTrue(intRange.contains(testRange));
-        assertFalse(testRange.contains(intRange));
-    }
-
-    @Test
-    public void testContainsRangeNoLowerBound()
-    {
-        Range intRange = new Range(Integer.class, null, new Integer(500));
-        Range testRange = new Range(Integer.class, -2500, 305);
-
-        assertTrue(intRange.contains(testRange));
-        assertFalse(testRange.contains(intRange));
-    }
-
-    @Test
-    public void testContainsRangeNoUpperBound()
-    {
-        Range intRange = new Range(Integer.class, new Integer(-2500), null);
-        Range testRange = new Range(Integer.class, 17, 305);
-
-        assertTrue(intRange.contains(testRange));
-        assertFalse(testRange.contains(intRange));
-    }
-
-    @Test (expected = IllegalArgumentException.class)
-    public void testIncompatibleTypeRangeContains()
-    {
-        Range intRange = new Range(Integer.class, new Integer(0), new Integer(10));
-        Range doubleRange = new Range(Double.class, new Double(2.0), new Double(5.0));
+    /**
+     * Tests the creation of {@link Range} objects under normal conditions.
+     */
+    @Test
+    public void testConstructor() {
+        Range<Integer> range = new Range<>(Integer.class, 3, 5);
+        assertEquals(Integer.valueOf(3), range.getMinValue());
+        assertEquals(Integer.valueOf(5), range.getMaxValue());
+        assertTrue  (range.isMaxIncluded());
+        assertTrue  (range.isMinIncluded());
+        assertFalse (range.isEmpty());
+
+        range = new Range<>(Integer.class, 3, false, 5, true);
+        assertEquals(Integer.valueOf(3), range.getMinValue());
+        assertEquals(Integer.valueOf(5), range.getMaxValue());
+        assertTrue  (range.isMaxIncluded());
+        assertFalse (range.isMinIncluded());
+        assertFalse (range.isEmpty());
+
+        range = new Range<>(Integer.class, 2, true, 7, false);
+        assertEquals(Integer.valueOf(2), range.getMinValue());
+        assertEquals(Integer.valueOf(7), range.getMaxValue());
+        assertFalse (range.isMaxIncluded());
+        assertTrue  (range.isMinIncluded());
+        assertFalse (range.isEmpty());
+
+        range = new Range<>(Integer.class, 3, false, 10, false);
+        assertEquals(Integer.valueOf( 3), range.getMinValue());
+        assertEquals(Integer.valueOf(10), range.getMaxValue());
+        assertFalse (range.isMaxIncluded());
+        assertFalse (range.isMinIncluded());
+        assertFalse (range.isEmpty());
+
+        range = new Range<>(Integer.class, 10, 2);
+        assertEquals(Integer.valueOf(10), range.getMinValue());
+        assertEquals(Integer.valueOf( 2), range.getMaxValue());
+        assertTrue (range.isEmpty());
+    }
+
+    /**
+     * Tests the detection of illegal arguments at {@link Range} creation time.
+     * Note that such error should never happen when parameterized types are used.
+     * The check performed by the constructor is a safety in case the user bypass
+     * the parameterized type check by using the raw type instead.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    @SuppressWarnings({"unchecked", "rawtypes"})
+    public void testConstructorErrors00() {
+        new Range(Double.class, "error", "blast");
+    }
+
+    /**
+     * Tests the detection of illegal arguments at {@link Range} creation time.
+     * Note that such error should never happen when parameterized types are used.
+     * The check performed by the constructor is a safety in case the user bypass
+     * the parameterized type check by using the raw type instead.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    @SuppressWarnings({"unchecked", "rawtypes"})
+    public void testConstructorErrors01() {
+        new Range(String.class, 123.233, 8740.09);
+    }
+
+    /**
+     * Tests the {@link Range#contains(Comparable)} method.
+     */
+    @Test
+    public void testContains() {
+        final Range<Integer> range = new Range<>(Integer.class, 3, 5);
+        assertTrue (range.contains(4));
+        assertFalse(range.contains(6));
+        assertFalse(range.contains(2));
+        assertTrue (range.contains(3));
+        assertTrue (range.contains(5));
+    }
+
+    /**
+     * Tests the {@link Range#contains(Comparable)} method with an exclusive minimal value.
+     */
+    @Test
+    public void testContainsNotInclusiveMinimum() {
+        final Range<Integer> range = new Range<>(Integer.class, 2, false, 5,
true);
+        assertTrue (range.contains(4));
+        assertFalse(range.contains(6));
+        assertFalse(range.contains(2));
+        assertTrue (range.contains(3));
+        assertTrue (range.contains(5));
+
+    }
+
+    /**
+     * Tests the {@link Range#contains(Comparable)} method with an exclusive maximal value.
+     */
+    @Test
+    public void testContainsNotInclusiveMaximum() {
+        final Range<Integer> range = new Range<>(Integer.class, 3, true, 6, false);
+        assertTrue (range.contains(4));
+        assertFalse(range.contains(6));
+        assertFalse(range.contains(2));
+        assertTrue (range.contains(3));
+        assertTrue (range.contains(5));
+    }
+
+    /**
+     * Tests the {@link Range#contains(Comparable)} method without lower bound.
+     */
+    @Test
+    public void testContainsNoLowerBound() {
+        final Range<Integer> range = new Range<>(Integer.class, null, 5);
+        assertTrue (range.contains(-555));
+        assertTrue (range.contains(5));
+        assertFalse(range.contains(6));
+    }
+
+    /**
+     * Tests the {@link Range#contains(Comparable)} method without upper bound.
+     */
+    @Test
+    public void testContainsNoUpperBound() {
+        final Range<Integer> range = new Range<>(Integer.class, 3, null);
+        assertFalse(range.contains(1));
+        assertTrue (range.contains(3));
+        assertTrue (range.contains(10000));
+    }
+
+    /**
+     * Tests the {@link Range#contains(Comparable)} method without lower or upper bounds.
+     */
+    @Test
+    public void testContainsNoBounds() {
+        final Range<Integer> range = new Range<>(Integer.class, null, null);
+        assertTrue(range.contains(-55555));
+        assertTrue(range.contains(100000));
+    }
+
+    /**
+     * Tests the {@link Range#contains(Range)} method.
+     */
+    @Test
+    public void testContainsRange() {
+        final Range<Integer> range  = new Range<>(Integer.class, -10, 10);
+        final Range<Integer> inside = new Range<>(Integer.class,  -5,  5);
+
+        assertTrue(range.contains(inside));
+        assertFalse(inside.contains(range));
+    }
+
+    /**
+     * Tests the {@link Range#contains(Range)} method without lower bound.
+     */
+    @Test
+    public void testContainsRangeNoLowerBound() {
+        final Range<Integer> range  = new Range<>(Integer.class,  null, 500);
+        final Range<Integer> inside = new Range<>(Integer.class, -2500, 305);
+
+        assertTrue (range.contains(inside));
+        assertFalse(inside.contains(range));
+    }
+
+    /**
+     * Tests the {@link Range#contains(Range)} method without upper bound.
+     */
+    @Test
+    public void testContainsRangeNoUpperBound() {
+        final Range<Integer> range  = new Range<>(Integer.class, -2500, null);
+        final Range<Integer> inside = new Range<>(Integer.class,    17,  305);
+
+        assertTrue(range.contains(inside));
+        assertFalse(inside.contains(range));
+    }
+
+    /**
+     * Tests the {@link Range#contains(Range)} method with a range of incompatible type.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testIncompatibleTypeRangeContains() {
+        final Range<Integer> intRange = new Range<>(Integer.class, 0, 10);
+        final Range doubleRange = new Range<>(Double.class, 2.0, 5.0);
 
         intRange.contains(doubleRange);
     }
 
-    @Test (expected = IllegalArgumentException.class)
-    public void testIncompatibleTypeContains()
-    {
-        Range intRange = new Range(Integer.class, new Integer(0), new Integer(10));
-        Range doubleRange = new Range(Double.class, new Double(2.0), new Double(5.0));
+    /**
+     * Tests the {@link Range#contains(Range)} method with a range of incompatible type.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testIncompatibleTypeContains() {
+        final Range<Integer> intRange = new Range<>(Integer.class, 0, 10);
+        final Range doubleRange = new Range<>(Double.class, 2.0, 5.0);
 
         intRange.contains(doubleRange);
     }
 
-    @Test
-    public void testIntersects()
-    {
-        Range range1 = new Range(Character.class, 'a', 'g');
-        Range range2 = new Range(Character.class, 'c', 'm');
+    /**
+     * Tests the {@link Range#intersects(Range)} method.
+     */
+    @Test
+    public void testIntersects() {
+        final Range<Character> range1 = new Range<>(Character.class, 'a', 'g');
+        final Range<Character> range2 = new Range<>(Character.class, 'c', 'm');
+        final Range<Character> range3 = new Range<>(Character.class, 'o', 'z');
 
-        assertTrue(range1.intersects(range2));
-        assertTrue(range2.intersects(range1));
-
-        Range range3 = new Range(Character.class, 'o', 'z');
+        assertTrue (range1.intersects(range2));
+        assertTrue (range2.intersects(range1));
         assertFalse(range1.intersects(range3));
         assertFalse(range3.intersects(range1));
     }
 
-    @Test (expected=IllegalArgumentException.class)
-    public void testIntersectsIncompatibleTypes()
-    {
-        Range range1 = new Range(Character.class, 'a', 'g');
-        Range range2 = new Range(Integer.class, 5, 7);
-
-        boolean ok = range1.intersects(range2);
+    /**
+     * Tests the {@link Range#intersects(Range)} method with a range of incompatible type.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testIntersectsIncompatibleTypes() {
+        final Range<Character> range1 = new Range<>(Character.class, 'a', 'g');
+        final Range range2 = new Range<>(Integer.class, 5, 7);
 
+        range1.intersects(range2);
     }
 
+    /**
+     * Tests the {@link Range#intersection(Range)} method.
+     */
     @Test
-    public void testIntersection()
-    {
-        Range range1 = new Range(Integer.class, 1, 5);
-        Range range2 = new Range(Integer.class, 4, 6);
+    public void testIntersection() {
+        final Range<Integer> range1 = new Range<>(Integer.class, 1, 5);
+        final Range<Integer> range2 = new Range<>(Integer.class, 4, 6);
 
-        Range intersection1 = range1.intersect(range2);
-        assertTrue(intersection1.getElementClass() == Integer.class);
-        assertTrue((Integer)intersection1.getMinValue() == 4);
-        assertTrue((Integer)intersection1.getMaxValue() == 5);
+        final Range<Integer> intersection = range1.intersect(range2);
+        assertEquals(Integer.class, intersection.getElementClass());
+        assertEquals(Integer.valueOf(4), intersection.getMinValue());
+        assertEquals(Integer.valueOf(5), intersection.getMaxValue());
     }
 
+    /**
+     * Tests the {@link Range#intersection(Range)} method with arguments resulting in empty
range.
+     */
     @Test
-    public void testIntersectionOfNonIntersectingRanges()
-    {
-        Range range1 = new Range(Integer.class, 1, 5);
-        Range range2 = new Range(Integer.class, 8, 10);
+    public void testIntersectionOfNonIntersectingRanges() {
+        final Range<Integer> range1 = new Range<>(Integer.class, 1,  5);
+        final Range<Integer> range2 = new Range<>(Integer.class, 8, 10);
 
-        Range intersection1 = range1.intersect(range2);
-        assertTrue(intersection1.getElementClass() == Integer.class);
-        assertTrue(intersection1.isEmpty());
+        final Range<Integer>  intersection = range1.intersect(range2);
+        assertEquals(Integer.class, intersection.getElementClass());
+        assertTrue(intersection.isEmpty());
     }
 
+    /**
+     * Tests the {@link Range#union(Range)} method.
+     */
     @Test
-    public void testUnion()
-    {
-        Range range1 = new Range(Character.class, 'a', 'f');
-        Range range2 = new Range(Character.class, 'd', 'h');
+    public void testUnion() {
+        final Range<Character> range1 = new Range<>(Character.class, 'a', 'f');
+        final Range<Character> range2 = new Range<>(Character.class, 'd', 'h');
 
-        Range unionRange = range1.union(range2);
-        assertFalse(unionRange.isEmpty());
-        assertTrue((Character)unionRange.getMinValue() == 'a');
-        assertTrue((Character)unionRange.getMaxValue() == 'h');
+        final Range<Character> union = range1.union(range2);
+        assertFalse(union.isEmpty());
+        assertEquals(Character.valueOf('a'), union.getMinValue());
+        assertEquals(Character.valueOf('h'), union.getMaxValue());
     }
 
+    /**
+     * Tests the {@link Range#union(Range)} method with disjoint ranges.
+     */
     @Test
-    public void testDisjointUnion()
-    {
-        Range range1 = new Range(Character.class, 'a', 'f');
-        Range range2 = new Range(Character.class, 'm', 'v');
+    public void testDisjointUnion() {
+        final Range<Character> range1 = new Range<>(Character.class, 'a', 'f');
+        final Range<Character> range2 = new Range<>(Character.class, 'm', 'v');
 
-        Range unionRange = range1.union(range2);
+        final Range<Character> unionRange = range1.union(range2);
         assertFalse(unionRange.isEmpty());
-        assertTrue((Character)unionRange.getMinValue() == 'a');
-        assertTrue((Character)unionRange.getMaxValue() == 'v');
+        assertEquals(Character.valueOf('a'), unionRange.getMinValue());
+        assertEquals(Character.valueOf('v'), unionRange.getMaxValue());
     }
 
-    @Test
-    public void testEquality()
-    {
-        //positive test - success case
-        Range range1 = new Range(Character.class, 'a', 'f');
-        Range range2 = new Range(Character.class, 'a', 'f');
+    /**
+     * Tests the {@link Range#equals(Object)} method.
+     */
+    @Test
+    public void testEquality() {
+        // Positive test - success case
+        final Range<Character> range1 = new Range<>(Character.class, 'a', 'f');
+        final Range<Character> range2 = new Range<>(Character.class, 'a', 'f');
         assertTrue(range1.equals(range2));
 
-        //positive test - failure case
-        Range range3 = new Range(Character.class, 'a', 'g');
+        // Positive test - failure case
+        final Range<Character> range3 = new Range<>(Character.class, 'a', 'g');
         assertFalse(range1.equals(range3));
 
-        //failure due to type incompatibility
-        Range range4 = new Range(String.class, "a", "g");
+        // Failure due to type incompatibility
+        final Range<String> range4 = new Range<>(String.class, "a", "g");
         assertFalse(range3.equals(range4));
 
-        Range range5 = new Range(Character.class, 'g', 'a');
-        Range range6 = new Range(Character.class, 'g', 'a');
+        final Range<Character> range5 = new Range<>(Character.class, 'g', 'a');
+        final Range<Character> range6 = new Range<>(Character.class, 'g', 'a');
         assertTrue(range5.isEmpty());
         assertTrue(range6.isEmpty());
         assertTrue(range5.equals(range6));
-
-
     }
 }



Mime
View raw message