sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1444591 - in /sis/branches/JDK7/sis-utility/src: main/java/org/apache/sis/measure/Range.java test/java/org/apache/sis/measure/RangeTest.java
Date Sun, 10 Feb 2013 19:45:51 GMT
Author: desruisseaux
Date: Sun Feb 10 19:45:50 2013
New Revision: 1444591

URL: http://svn.apache.org/r1444591
Log:
Applied to 'union(Range<?>)' the same work (reduce the amount of comparison,
take inclusion/exclusion in account) than 'intersect(Range<?>)'.

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=1444591&r1=1444590&r2=1444591&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 Sun Feb
10 19:45:50 2013
@@ -399,38 +399,34 @@ public class Range<T extends Comparable<
         return intersect;
     }
 
-    public Range<T> union(final Range<T> value) throws IllegalArgumentException
-    {
-        ensureCompatible(value);
-
-        //if they are both the same, return either one
-        if (this.equals(value))
-        {
-            return value;
-        }
-
-        //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.
-        T rangeMin, rangeMax;
-        if (value.getMinValue().compareTo(minValue) <= 0)
-        {
-            rangeMin = value.getMinValue();
-        }
-        else
-        {
-            rangeMin = minValue;
-        }
+    /**
+     * Returns the union of this range with the given range.
+     *
+     * @param  range The range to add to this range.
+     * @return The union of this range with the given range.
+     * @throws IllegalArgumentException is the given range can not be converted to a valid
type
+     *         through widening conversion, or if the units of measurement are not convertible.
+     */
+    public Range<?> union(final Range<?> range) throws IllegalArgumentException
{
+        return unionNC(ensureCompatible(range));
+    }
 
-        if (value.getMaxValue().compareTo(maxValue) >= 0)
-        {
-            rangeMax = value.getMaxValue();
-        }
-        else
-        {
-            rangeMax = maxValue;
-        }
-        return new Range<>(this.elementType, rangeMin, rangeMax );
+    /**
+     * Implementation of {@link #union(Range)} to be invoked directly by subclasses.
+     * "NC" stands for "No Cast" - this method do not try to cast the value to a compatible
type.
+     */
+    final Range<?> unionNC(final Range<? extends T> range) throws IllegalArgumentException
{
+        final Range<? extends T> union, min, max;
+        min = compareMinTo(range.minValue, range.isMinIncluded ? 0 : -1) > 0 ? range :
this;
+        max = compareMaxTo(range.maxValue, range.isMaxIncluded ? 0 : +1) < 0 ? range :
this;
+        if (min == max) {
+            union = min;
+        } else {
+            union = create(min.minValue, min.isMinIncluded, max.maxValue, max.isMaxIncluded);
+        }
+        assert union.contains(min) : min;
+        assert union.contains(max) : max;
+        return union;
     }
 
     //TODO: implement this

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=1444591&r1=1444590&r2=1444591&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 Sun
Feb 10 19:45:50 2013
@@ -282,7 +282,7 @@ public final strictfp class RangeTest ex
         final Range<Character> range1 = new Range<>(Character.class, 'a', 'f');
         final Range<Character> range2 = new Range<>(Character.class, 'd', 'h');
 
-        final Range<Character> union = range1.union(range2);
+        final Range<?> union = range1.union(range2);
         assertFalse(union.isEmpty());
         assertEquals(Character.valueOf('a'), union.getMinValue());
         assertEquals(Character.valueOf('h'), union.getMaxValue());
@@ -296,7 +296,7 @@ public final strictfp class RangeTest ex
         final Range<Character> range1 = new Range<>(Character.class, 'a', 'f');
         final Range<Character> range2 = new Range<>(Character.class, 'm', 'v');
 
-        final Range<Character> unionRange = range1.union(range2);
+        final Range<?> unionRange = range1.union(range2);
         assertFalse(unionRange.isEmpty());
         assertEquals(Character.valueOf('a'), unionRange.getMinValue());
         assertEquals(Character.valueOf('v'), unionRange.getMaxValue());



Mime
View raw message