sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1444552 - /sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/Range.java
Date Sun, 10 Feb 2013 14:42:14 GMT
Author: desruisseaux
Date: Sun Feb 10 14:42:14 2013
New Revision: 1444552

URL: http://svn.apache.org/r1444552
Log:
Simplified the contains(T) method by testing for non-inclusion instead than testing for inclusion.

Modified:
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/Range.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=1444552&r1=1444551&r2=1444552&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 14:42:14 2013
@@ -219,92 +219,35 @@ public class Range<T extends Comparable<
 
     public boolean contains(final T value) throws IllegalArgumentException
     {
-
-        boolean unbounded = (minValue == null && maxValue == null);
-        //safety check
-        if (value == null)
-        {
-            if (unbounded)
-            {
-                return true;
-            }
-            else
-            {
-                return false;
-            }
-        }
-
-        //set unbounded on both ends
-        if (unbounded)
-        {
-            return true;
-        }
-
-        int minimumValueCheck = 1;
-        if (minValue != null)
-        {
-            minimumValueCheck = value.compareTo(minValue);
-        }
-        int maximumValueCheck = -1;
-        if (maxValue != null)
-        {
-            maximumValueCheck = value.compareTo(maxValue);
-        }
-
-        //set unbounded on lower end
-        if (minValue == null && maximumValueCheck <= 0)
-        {
-            if (isMaxIncluded && minimumValueCheck > 0)
-            {
-                return true;
-            }
-            else if (!isMaxIncluded)
-            {
-                return true;
-            }
-
+        /*
+         * Implementation note: when testing for inclusion or intersection in a range
+         * (or in a rectangle, cube, etc.), it is often easier to test when we do not
+         * have inclusion than to test for inclusion. So we test when to return false
+         * and if no such test pass, we can return true.
+         *
+         * We consistently use min/maxValue.compareTo(value) in this class rather than
+         * the opposite argument order (namely value.compareTo(min/maxValue)) in the
+         * hope to reduce the risk of inconsistent behavior if usera pass different
+         * sub-classes for the 'value' argument with different implementations of the
+         * 'compareTo' method. Intead than using those user implementations, we always
+         * use the implementations provided by min/maxValue.
+         */
+        if (value == null) {
+            return false;
         }
-
-        //set unbounded on upper end
-        if (maxValue == null && minimumValueCheck >= 0)
-        {
-            if (isMinIncluded && minimumValueCheck > 0)
-            {
-                return true;
-            }
-            else if (!isMinIncluded)
-            {
-                return true;
+        if (minValue != null) {
+            final int c = minValue.compareTo(value);
+            if (isMinIncluded ? (c > 0) : (c >= 0)) {
+                return false;
             }
         }
-
-        //set bounded on both ends
-        if (minimumValueCheck >= 0 && maximumValueCheck <= 0)
-        {
-            //both min and max are included
-            if (isMinIncluded && isMaxIncluded)
-            {
-                return true;
-            }
-            //only min is included
-            else if (!isMinIncluded && minimumValueCheck > 0)
-            {
-                return true;
-            }
-            //only max is included
-            else if (!isMaxIncluded && maximumValueCheck < 0)
-            {
-                return true;
-            }
-            //neither is included
-            else
-            {
-                return (minimumValueCheck > 0 && maximumValueCheck < 0);
+        if (maxValue != null) {
+            final int c = maxValue.compareTo(value);
+            if (isMaxIncluded ? (c < 0) : (c <= 0)) {
+                return false;
             }
-
         }
-        return false;
-
+        return true;
     }
 
 



Mime
View raw message