sis-commits mailing list archives

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

URL: http://svn.apache.org/r1444548
Log:
Moved the checkMethodArgs(Range<T>) method to ensureCompatible(Range<?>),
using the internationalized resources for error messages if needed.

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=1444548&r1=1444547&r2=1444548&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:06:59 2013
@@ -70,8 +70,6 @@ public class Range<T extends Comparable<
      */
     private final boolean isMinIncluded, isMaxIncluded;
 
-    private static String INVALID_TYPE_ERROR = "Type to be compared does not match the Range
type.";
-
     /**
      * Creates a new range bounded by the given inclusive values.
      *
@@ -112,6 +110,19 @@ public class Range<T extends Comparable<
     }
 
     /**
+     * Ensures that the given range uses the same element class than this range,
+     * then return the casted argument value.
+     *
+     * @param range The range to test for compatibility.
+     */
+    @SuppressWarnings("unchecked")
+    private Range<? extends T> ensureCompatible(final Range<?> range) throws
IllegalArgumentException {
+        ensureNonNull("range", range);
+        ensureCompatibleType(range.elementType);
+        return (Range<? extends T>) range;
+    }
+
+    /**
      * Ensures that the given type is compatible with the type expected by this range.
      */
     private void ensureCompatibleType(final Class<?> type) throws IllegalArgumentException
{
@@ -223,12 +234,6 @@ public class Range<T extends Comparable<
             }
         }
 
-        //first check
-        if (value.getClass() != elementType)
-        {
-            throw new IllegalArgumentException(INVALID_TYPE_ERROR);
-        }
-
         //set unbounded on both ends
         if (unbounded)
         {
@@ -305,30 +310,21 @@ public class Range<T extends Comparable<
 
     public boolean contains(final Range<T> value) throws IllegalArgumentException
     {
-        if (!checkMethodArgs(value))
-        {
-            throw new IllegalArgumentException(INVALID_TYPE_ERROR);
-        }
+        ensureCompatible(value);
         return this.contains(value.getMinValue()) && this.contains(value.getMaxValue());
     }
 
 
     public boolean intersects(final Range<T> value) throws IllegalArgumentException
     {
-        if (!checkMethodArgs(value))
-        {
-            throw new IllegalArgumentException(INVALID_TYPE_ERROR);
-        }
+        ensureCompatible(value);
 
         return this.contains(value.getMinValue()) || this.contains(value.getMaxValue());
     }
 
     public Range<T> union(final Range<T> value) throws IllegalArgumentException
     {
-        if (!checkMethodArgs(value))
-        {
-            throw new IllegalArgumentException(INVALID_TYPE_ERROR);
-        }
+        ensureCompatible(value);
 
         //if they are both the same, return either one
         if (this.equals(value))
@@ -362,10 +358,7 @@ public class Range<T extends Comparable<
 
     public Range<T> intersect(final Range<T> value) throws IllegalArgumentException
     {
-        if (!checkMethodArgs(value))
-        {
-            throw new IllegalArgumentException(INVALID_TYPE_ERROR);
-        }
+        ensureCompatible(value);
 
         //return empty set if the Ranges don't intersect
         if (!this.intersects(value))
@@ -406,10 +399,7 @@ public class Range<T extends Comparable<
     //TODO: implement this
     public Range<T>[] subtract(final Range<T> value) throws IllegalArgumentException
     {
-        if (!checkMethodArgs(value))
-        {
-            throw new IllegalArgumentException(INVALID_TYPE_ERROR);
-        }
+        ensureCompatible(value);
         Range<T>[] ranges = new Range[1];
         ranges[0] = null;
         return ranges;
@@ -456,17 +446,4 @@ public class Range<T extends Comparable<
         hash = 13 * hash + (this.isMaxIncluded ? 1 : 0);
         return hash;
     }
-
-    private boolean checkMethodArgs(final Range<T> value) {
-        if (value == null)
-        {
-            return false;
-        }
-        else if (  value.getElementType() != elementType)
-        {
-            return false;
-        }
-        return true;
-
-    }
 }



Mime
View raw message