sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1445888 - in /sis/branches/JDK7/sis-utility/src: main/java/org/apache/sis/measure/ test/java/org/apache/sis/measure/
Date Wed, 13 Feb 2013 19:52:28 GMT
Author: desruisseaux
Date: Wed Feb 13 19:52:28 2013
New Revision: 1445888

URL: http://svn.apache.org/r1445888
Log:
Change of strategy regarding parameterized type in Range and subclasses:
Define all Range methods in a stricter way. Then instead than trying to
make the methods more lenient in subclasses, provide the lenient methods
as separated methods with the "Any" suffix ("contains" vs "containsAny",
"intersect" vs "intersectAny", "union" vs "unionAny", etc.)

Modified:
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/DateRange.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/MeasurementRange.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/NumberRange.java
    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/NumberRangeTest.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/DateRange.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/DateRange.java?rev=1445888&r1=1445887&r2=1445888&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/DateRange.java (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/DateRange.java Wed Feb 13 19:52:28 2013
@@ -111,19 +111,6 @@ public class DateRange extends Range<Dat
     }
 
     /**
-     * Ensures that {@link #elementType} is compatible with the type expected by this range class.
-     * Invoked for argument checking by the super-class constructor.
-     */
-    @Override
-    final void ensureValidType() throws IllegalArgumentException {
-        // No need to call super.checkElementClass() because Date implements Comparable.
-        if (!Date.class.isAssignableFrom(elementType)) {
-            throw new IllegalArgumentException(Errors.format(
-                    Errors.Keys.IllegalClass_2, Date.class, elementType));
-        }
-    }
-
-    /**
      * Casts the given {@code Range} object to a {@code DateRange}. This method shall be invoked
      * only in context where we have verified that the range element type is compatible.
      * This verification is performed by {@link Range#ensureCompatible(Range)} method.
@@ -174,7 +161,7 @@ public class DateRange extends Range<Dat
      * {@inheritDoc}
      */
     @Override
-    public DateRange union(final Range<?> range) throws IllegalArgumentException {
+    public DateRange union(final Range<Date> range) {
         return cast(super.union(range));
     }
 
@@ -182,7 +169,7 @@ public class DateRange extends Range<Dat
      * {@inheritDoc}
      */
     @Override
-    public DateRange intersect(final Range<?> range) throws IllegalArgumentException {
+    public DateRange intersect(final Range<Date> range) {
         return cast(super.intersect(range));
     }
 
@@ -190,7 +177,7 @@ public class DateRange extends Range<Dat
      * {@inheritDoc}
      */
     @Override
-    public DateRange[] subtract(final Range<?> range) throws IllegalArgumentException {
+    public DateRange[] subtract(final Range<Date> range) {
         return (DateRange[]) super.subtract(range);
         // Should never throw ClassCastException because super.subtract(Range) invokes newArray(int)
         // and create(...), which are overridden in this class with DateRange return type.

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/MeasurementRange.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/MeasurementRange.java?rev=1445888&r1=1445887&r2=1445888&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/MeasurementRange.java (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/MeasurementRange.java Wed Feb 13 19:52:28 2013
@@ -31,6 +31,12 @@ import java.util.Objects;
  * A range of numbers associated with a unit of measurement. Unit conversions are applied as
  * needed by {@linkplain #union union} and {@linkplain #intersect intersection} operations.
  *
+ * {@section Construction}
+ * This class provides convenience {@code create(…)} static methods for every floating point
+ * primitive types. Usage of {@code MeasurementRange} with integer types is possible, but no
+ * convenience methods is provided for integers because they are usually not representative
+ * of the nature of physical measurements.
+ *
  * @param <T> The type of range elements as a subclass of {@link Number}.
  *
  * @author  Martin Desruisseaux (IRD)
@@ -195,17 +201,17 @@ public class MeasurementRange<T extends 
      * @param range The range to copy. The elements must be {@link Number} instances.
      * @param units The units of measurement, or {@code null} if unknown.
      */
-    private MeasurementRange(Class<T> type, Range<? extends Number> range, final Unit<?> units) {
+    private MeasurementRange(final Class<T> type, final Range<? extends Number> range, final Unit<?> units) {
         super(type, range);
         this.units = units;
     }
 
     /**
-     * Creates a new range using the same element type than this range.
+     * Creates a new range using the same element type and the same units than this range.
      */
     @Override
-    MeasurementRange<T> create(final T minValue, final boolean isMinIncluded,
-                               final T maxValue, final boolean isMaxIncluded)
+    Range<T> create(final T minValue, final boolean isMinIncluded,
+                    final T maxValue, final boolean isMaxIncluded)
     {
         return new MeasurementRange<>(elementType, minValue, isMinIncluded, maxValue, isMaxIncluded, units);
     }
@@ -228,7 +234,7 @@ public class MeasurementRange<T extends 
      * @param  targetUnits the target units, or {@code null} for keeping the units unchanged.
      * @return The converted range, or {@code this} if no conversion is needed.
      * @throws ConversionException if the target units are not compatible with
-     *         this {@linkplain #getUnits range units}.
+     *         this {@linkplain #getUnits() range units}.
      */
     public MeasurementRange<T> convertTo(final Unit<?> targetUnits) throws ConversionException {
         return convertAndCast(elementType, targetUnits);
@@ -238,8 +244,32 @@ public class MeasurementRange<T extends 
      * {@inheritDoc}
      */
     @Override
-    public <N extends Number & Comparable<? super N>> MeasurementRange<N> castTo(Class<N> type) {
-        return convertAndCast(this, type);
+    @SuppressWarnings("unchecked")
+    public <N extends Number & Comparable<? super N>> MeasurementRange<N> castTo(final Class<N> type) {
+        if (elementType == type) {
+            return (MeasurementRange<N>) this;
+        } else {
+            return new MeasurementRange<>(type, this, units);
+        }
+    }
+
+    /**
+     * If the given range is an instance of {@code MeasurementRange}, converts that
+     * range to the units of this range. Otherwise returns the given range unchanged.
+     *
+     * @param  range The range to convert.
+     * @return The converted range.
+     * @throws IllegalArgumentException if the given target unit is not compatible with
+     *         the unit of this range.
+     */
+    private Range<T> convert(final Range<T> range) throws IllegalArgumentException {
+        if (range instanceof MeasurementRange<?>) try {
+            return ((MeasurementRange<T>) range).convertAndCast(elementType, units);
+        } catch (ConversionException e) {
+            throw new IllegalArgumentException(Errors.format(Errors.Keys.IncompatibleUnits_2,
+                    ((MeasurementRange<?>) range).units, units), e);
+        }
+        return range;
     }
 
     /**
@@ -253,17 +283,14 @@ public class MeasurementRange<T extends 
      */
     @Override
     <N extends Number & Comparable<? super N>>
-    MeasurementRange<N> convertAndCast(final Range<? extends Number> range, final Class<N> type)
+    NumberRange<N> convertAndCast(final NumberRange<?> range, final Class<N> type)
             throws IllegalArgumentException
     {
-        if (range instanceof MeasurementRange<?>) {
-            final MeasurementRange<?> casted = (MeasurementRange<?>) range;
-            try {
-                return casted.convertAndCast(type, units);
-            } catch (ConversionException e) {
-                throw new IllegalArgumentException(Errors.format(
-                        Errors.Keys.IncompatibleUnits_2, casted.units, units), e);
-            }
+        if (range instanceof MeasurementRange<?>) try {
+            return ((MeasurementRange<?>) range).convertAndCast(type, units);
+        } catch (ConversionException e) {
+            throw new IllegalArgumentException(Errors.format(Errors.Keys.IncompatibleUnits_2,
+                    ((MeasurementRange<?>) range).units, units), e);
         }
         return new MeasurementRange<>(type, range, units);
     }
@@ -275,42 +302,39 @@ public class MeasurementRange<T extends 
      *             {@link Integer}, {@link Long}, {@link Float} or {@link Double}.
      * @param  targetUnit the target units, or {@code null} for no change.
      * @return The casted range, or {@code this}.
-     * @throws ConversionException if the target units are not compatible with
-     *         this {@linkplain #getUnits range units}.
+     * @throws ConversionException if the given target unit is not compatible with
+     *         the unit of this range.
      */
     @SuppressWarnings("unchecked")
     private <N extends Number & Comparable<? super N>> MeasurementRange<N>
-            convertAndCast(final Class<N> type, final Unit<?> targetUnits) throws ConversionException
+            convertAndCast(final Class<N> type, Unit<?> targetUnits) throws ConversionException
     {
         if (targetUnits == null || targetUnits.equals(units)) {
-            if (type.equals(elementType)) {
+            if (elementType == type) {
                 return (MeasurementRange<N>) this;
-            } else {
-                return new MeasurementRange<>(type, this, units);
+            }
+            targetUnits = units;
+        } else if (units != null) {
+            final UnitConverter converter = units.getConverterToAny(targetUnits);
+            if (!converter.equals(UnitConverter.IDENTITY)) {
+                boolean minInc = isMinIncluded;
+                boolean maxInc = isMaxIncluded;
+                double minimum = converter.convert(getMinimum());
+                double maximum = converter.convert(getMaximum());
+                if (minimum > maximum) {
+                    final double  td = minimum; minimum = maximum; maximum = td;
+                    final boolean tb = minInc;  minInc  = maxInc;  maxInc  = tb;
+                }
+                if (Numbers.isInteger(type)) {
+                    minInc &= (minimum == (minimum = Math.floor(minimum)));
+                    maxInc &= (maximum == (maximum = Math.ceil (maximum)));
+                }
+                return new MeasurementRange<>(type,
+                        Numbers.cast(minimum, type), minInc,
+                        Numbers.cast(maximum, type), maxInc, targetUnits);
             }
         }
-        if (units == null) {
-            return new MeasurementRange<>(type, this, targetUnits);
-        }
-        final UnitConverter converter = units.getConverterToAny(targetUnits);
-        if (converter.equals(UnitConverter.IDENTITY)) {
-            return new MeasurementRange<>(type, this, targetUnits);
-        }
-        boolean isMinIncluded = isMinIncluded();
-        boolean isMaxIncluded = isMaxIncluded();
-        Double minimum = converter.convert(getMinimum());
-        Double maximum = converter.convert(getMaximum());
-        if (minimum.compareTo(maximum) > 0) {
-            final Double td = minimum;
-            minimum = maximum;
-            maximum = td;
-            final boolean tb = isMinIncluded;
-            isMinIncluded = isMaxIncluded;
-            isMaxIncluded = tb;
-        }
-        return new MeasurementRange<>(type,
-                Numbers.cast(minimum, type), isMinIncluded,
-                Numbers.cast(maximum, type), isMaxIncluded, targetUnits);
+        return new MeasurementRange<>(type, this, targetUnits);
     }
 
     /**
@@ -318,39 +342,56 @@ public class MeasurementRange<T extends 
      */
     @Override
     @SuppressWarnings({"unchecked","rawtypes"}) // Generic array creation.
-    MeasurementRange<T>[] newArray(final int length) {
+    final Range<T>[] newArray(final int length) {
         return new MeasurementRange[length];
     }
 
     /**
      * {@inheritDoc}
+     * If the given range is an instance of {@code MeasurementRange}, then this method converts
+     * the value of the other range to the unit of measurement of this range before to perform
+     * the operation.
+     *
+     * @throws IllegalArgumentException is the given range is an instance of
+     *         {@code MeasurementRange} using incommensurable units of measurement.
      */
     @Override
-    public MeasurementRange<?> union(final Range<?> range) throws IllegalArgumentException {
-        return (MeasurementRange<?>) super.union(range);
+    public MeasurementRange<T> union(final Range<T> range) throws IllegalArgumentException {
+        return (MeasurementRange<T>) super.union(convert(range));
         // Should never throw ClassCastException because super.union(Range) invokes create(...),
-        // which is overridden in this class with MeasurementRange return type.
+        // convertAndCast(…) which is overridden in this class to create MeasurementRange.
     }
 
     /**
      * {@inheritDoc}
+     * If the given range is an instance of {@code MeasurementRange}, then this method converts
+     * the value of the other range to the unit of measurement of this range before to perform
+     * the operation.
+     *
+     * @throws IllegalArgumentException is the given range is an instance of
+     *         {@code MeasurementRange} using incommensurable units of measurement.
      */
     @Override
-    public MeasurementRange<?> intersect(final Range<?> range) throws IllegalArgumentException {
-        return (MeasurementRange<?>) super.intersect(range);
+    public MeasurementRange<T> intersect(final Range<T> range) throws IllegalArgumentException {
+        return (MeasurementRange<T>) super.intersect(convert(range));
         // Should never throw ClassCastException because super.intersect(Range) invokes
-        // convertAndCast(...),  which is overridden in this class with MeasurementRange
-        // return type.
+        // convertAndCast(…) which is overridden in this class to create MeasurementRange.
     }
 
     /**
      * {@inheritDoc}
+     * If the given range is an instance of {@code MeasurementRange}, then this method converts
+     * the value of the other range to the unit of measurement of this range before to perform
+     * the operation.
+     *
+     * @throws IllegalArgumentException is the given range is an instance of
+     *         {@code MeasurementRange} using incommensurable units of measurement.
      */
     @Override
-    public MeasurementRange<?>[] subtract(final Range<?> range) throws IllegalArgumentException {
-        return (MeasurementRange<?>[]) super.subtract(range);
+    public MeasurementRange<T>[] subtract(final Range<T> range) throws IllegalArgumentException {
+        return (MeasurementRange<T>[]) super.subtract(convert(range));
         // Should never throw ClassCastException because super.subtract(Range) invokes newArray(int)
-        // and create(...), which are overridden in this class with MeasurementRange return type.
+        // convertAndCast(…) which is overridden in this class to create MeasurementRange.
     }
 
     /**

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/NumberRange.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/NumberRange.java?rev=1445888&r1=1445887&r2=1445888&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/NumberRange.java (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/NumberRange.java Wed Feb 13 19:52:28 2013
@@ -18,16 +18,32 @@ package org.apache.sis.measure;
 
 import net.jcip.annotations.Immutable;
 import org.apache.sis.util.Numbers;
-import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.util.resources.Errors;
 
 
 /**
- * A range of numbers. {@linkplain #union Union} and {@linkplain #intersect intersection}
- * are computed as usual, except that widening conversions will be applied as needed.
+ * A range of numbers capable of widening conversions when performing range operations.
+ * {@code NumberRange} has no units. For a range of physical measurements with units of
+ * measure, see {@link MeasurementRange}.
  *
- * <p>{@code NumberRange} has no units. For a range of physical measurements with units of
- * measure, see {@link MeasurementRange}.</p>
+ * <p>Most operations in this class are defined in two versions:</p>
+ * <ul>
+ *   <li><p>Methods inherited from the {@code Range} parent class
+ *      ({@link #contains(Range) contains}, {@link #intersect(Range) intersect},
+ *       {@link #intersects(Range) intersects}, {@link #union(Range) union} and
+ *       {@link #subtract(Range) subtract}) requires argument or range elements
+ *       of type {@code <T>}. No type conversion is performed.</p></li>
+ *
+ *   <li><p>Methods defined in this class with the {@code Any} suffix
+ *      ({@link #containsAny(NumberRange) containsAny}, {@link #intersectAny(NumberRange) intersectAny},
+ *       {@link #intersectsAny(NumberRange) intersectsAny}, {@link #unionAny(NumberRange) unionAny} and
+ *       {@link #subtractAny(NumberRange) subtractAny}) are more lenient on the argument or range element
+ *       type {@code <T>}. Widening conversions are performed as needed.</p></li>
+ * </ul>
+ *
+ * {@section Construction}
+ * This class provides convenience {@code create(…)} static methods for every
+ * numeric primitive types.
  *
  * @param <T> The type of range elements as a subclass of {@link Number}.
  *
@@ -41,12 +57,6 @@ import org.apache.sis.util.resources.Err
  */
 @Immutable
 public class NumberRange<T extends Number & Comparable<? super T>> extends Range<T> {
-    //
-    // IMPLEMENTATION NOTE: This class is full of @SuppressWarnings("unchecked") annotations.
-    // Nevertheless we should never get ClassCastException - if we get some, this would be a
-    // bug in this implementation. Users may get IllegalArgumentException however.
-    //
-
     /**
      * Serial number for inter-operability with different versions.
      */
@@ -363,49 +373,13 @@ public class NumberRange<T extends Numbe
      * be overridden by subclasses in order to create a range of a more specific type.
      */
     @Override
-    NumberRange<T> create(final T minValue, final boolean isMinIncluded,
-                          final T maxValue, final boolean isMaxIncluded)
+    Range<T> create(final T minValue, final boolean isMinIncluded,
+                    final T maxValue, final boolean isMaxIncluded)
     {
         return new NumberRange<>(elementType, minValue, isMinIncluded, maxValue, isMaxIncluded);
     }
 
     /**
-     * Ensures that {@link #elementType} is compatible with the type expected by this range class.
-     * Invoked for argument checking by the super-class constructor.
-     */
-    @Override
-    final void ensureValidType() throws IllegalArgumentException {
-        ensureNumberClass(elementType);
-        super.ensureValidType(); // Check that the type implements also Comparable.
-    }
-
-    /**
-     * Ensures that the given class is {@link Number} or a subclass.
-     */
-    private static void ensureNumberClass(final Class<?> type) throws IllegalArgumentException {
-        if (!Number.class.isAssignableFrom(type)) {
-            throw new IllegalArgumentException(Errors.format(
-                    Errors.Keys.IllegalClass_2, Number.class, type));
-        }
-    }
-
-    /**
-     * Returns the type of minimum and maximum values.
-     */
-    @SuppressWarnings("unchecked")
-    private static Class<? extends Number> getElementType(final Range<?> range) {
-        ArgumentChecks.ensureNonNull("range", range);
-        final Class<?> type = range.elementType;
-        ensureNumberClass(type);
-        /*
-         * Safe because we checked in the above line. We could have used Class.asSubclass(Class)
-         * instead but we want an IllegalArgumentException in case of failure rather than a
-         * ClassCastException.
-         */
-        return (Class<? extends Number>) type;
-    }
-
-    /**
      * Casts the specified range to the specified type.  If this class is associated to a unit of
      * measurement, then this method converts the {@code range} units to the same units than this
      * instance.  This method is overridden by {@link MeasurementRange} only in the way described
@@ -417,14 +391,13 @@ public class NumberRange<T extends Numbe
      * @throws IllegalArgumentException If the given type is not one of the primitive
      *         wrappers for numeric types.
      */
-    @SuppressWarnings({"unchecked","rawtypes"})
+    @SuppressWarnings("unchecked")
     <N extends Number & Comparable<? super N>>
-    NumberRange<N> convertAndCast(final Range<? extends Number> range, final Class<N> type)
+    NumberRange<N> convertAndCast(final NumberRange<?> range, final Class<N> type)
             throws IllegalArgumentException
     {
-        if (type.equals(range.getElementType())) {
-            // Safe because we checked in the line just above.
-            return (NumberRange<N>) wrap((Range) range);
+        if (range.elementType == type) {
+            return (NumberRange<N>) range;
         }
         return new NumberRange<>(type, range);
     }
@@ -441,10 +414,14 @@ public class NumberRange<T extends Numbe
      * @throws IllegalArgumentException If the given type is not one of the primitive
      *         wrappers for numeric types.
      */
+    @SuppressWarnings("unchecked")
     public <N extends Number & Comparable<? super N>> NumberRange<N> castTo(final Class<N> type)
             throws IllegalArgumentException
     {
-        return convertAndCast(this, type);
+        if (elementType == type) {
+            return (NumberRange<N>) this;
+        }
+        return new NumberRange<>(type, this);
     }
 
     /**
@@ -452,129 +429,11 @@ public class NumberRange<T extends Numbe
      */
     @Override
     @SuppressWarnings({"unchecked","rawtypes"}) // Generic array creation.
-    NumberRange<T>[] newArray(final int length) {
+    Range<T>[] newArray(final int length) {
         return new NumberRange[length];
     }
 
     /**
-     * Returns {@code true} if the specified value is within this range.
-     * This method delegates to {@link #contains(Comparable)}.
-     *
-     * @param  value The value to check for inclusion.
-     * @return {@code true} if the given value is within this range.
-     * @throws IllegalArgumentException if the given value is not comparable.
-     */
-    public final boolean contains(final Number value) throws IllegalArgumentException {
-        if (value != null && !(value instanceof Comparable<?>)) {
-            throw new IllegalArgumentException(Errors.format(
-                    Errors.Keys.NotComparableClass_1, value.getClass()));
-        }
-        return contains((Comparable<?>) value);
-    }
-
-    /**
-     * Returns {@code true} if the specified value is within this range.
-     * The given value must be a subclass of {@link Number}.
-     *
-     * @throws IllegalArgumentException If the given type is not one of the primitive
-     *         wrappers for numeric types.
-     */
-    @Override
-    @SuppressWarnings({"unchecked","rawtypes"})
-    public boolean contains(Comparable<?> value) throws IllegalArgumentException {
-        if (value == null) {
-            return false;
-        }
-        ArgumentChecks.ensureCanCast("value", Number.class, value);
-        /*
-         * Suppress warning because we checked the class in the line just above, so we are safe.
-         * We could have used Class.cast(Object) but we want an IllegalArgumentException with a
-         * localized message.
-         */
-        Number number = (Number) value;
-        final Class<? extends Number> type = Numbers.widestClass(elementType, number.getClass());
-        number = Numbers.cast(number, type);
-        /*
-         * The 'type' bounds should actually be <? extends Number & Comparable> since the method
-         * signature expect a Comparable and we have additionally casted to a Number.  However I
-         * have not found a way to express that safely in a local variable with Java 6.
-         */
-        return castTo((Class) type).containsNC((Comparable<?>) number);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    @SuppressWarnings({"unchecked","rawtypes"})
-    public boolean contains(Range<?> range) throws IllegalArgumentException {
-        final Class<? extends Number> type = Numbers.widestClass(elementType, getElementType(range));
-        /*
-         * The type bounds is actually <? extends Number & Comparable> but I'm unable to express
-         * it as local variable as of Java 6. So we have to bypass the compiler check, but those
-         * casts are actually safes - including the (Range) cast - because getElementType(range)
-         * would have throw an exception otherwise.
-         */
-        range = convertAndCast((Range) range, (Class) type);
-        return castTo((Class) type).containsNC(range);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    @SuppressWarnings({"unchecked","rawtypes"})
-    public boolean intersects(Range<?> range) throws IllegalArgumentException {
-        final Class<? extends Number> type = Numbers.widestClass(elementType, getElementType(range));
-        range = convertAndCast((Range) range, (Class) type); // Same comment than contains(Range).
-        return castTo((Class) type).intersectsNC(range);
-    }
-
-    /**
-     * {@inheritDoc}
-     * Widening conversions will be applied as needed.
-     */
-    @Override
-    @SuppressWarnings({"unchecked","rawtypes"})
-    public NumberRange<?> union(Range<?> range) throws IllegalArgumentException {
-        final Class<? extends Number> type = Numbers.widestClass(elementType, getElementType(range));
-        range = convertAndCast((Range) range, (Class) type); // Same comment than contains(Range).
-        return (NumberRange) castTo((Class) type).unionNC(range);
-    }
-
-    /**
-     * {@inheritDoc}
-     * Widening conversions will be applied as needed.
-     */
-    @Override
-    @SuppressWarnings({"unchecked","rawtypes"})
-    public NumberRange<?> intersect(Range<?> range) throws IllegalArgumentException {
-        final Class<? extends Number> rangeType = getElementType(range);
-        Class<? extends Number> type = Numbers.widestClass(elementType, rangeType);
-        range = castTo((Class) type).intersectNC(convertAndCast((Range) range, (Class) type));
-        /*
-         * Use a finer type capable to holds the result (since the intersection
-         * may have reduced the range), but not finer than the finest type of
-         * the ranges used in the intersection calculation.
-         */
-        type = Numbers.narrowestClass(elementType, rangeType);
-        type = Numbers.widestClass(type, Numbers.narrowestClass((Number) range.minValue));
-        type = Numbers.widestClass(type, Numbers.narrowestClass((Number) range.maxValue));
-        return convertAndCast((Range) range, (Class) type);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    @SuppressWarnings({"unchecked","rawtypes"})
-    public NumberRange<?>[] subtract(Range<?> range) throws IllegalArgumentException {
-        Class<? extends Number> type = Numbers.widestClass(elementType, getElementType(range));
-        return (NumberRange[]) castTo((Class) type)
-                .subtractNC(convertAndCast((Range) range, (Class) type));
-    }
-
-    /**
      * Returns the {@linkplain #getMinValue() minimum value} as a {@code double}.
      * If this range is unbounded, then {@link Double#NEGATIVE_INFINITY} is returned.
      *
@@ -660,4 +519,161 @@ public class NumberRange<T extends Numbe
         }
         return value;
     }
+
+    /**
+     * Returns {@code true} if this range contains the given value.
+     * This method converts {@code this} or the given argument to the widest numeric type,
+     * then performs the same work than {@link #contains(Comparable)}.
+     *
+     * @param  value The value to check for inclusion in this range.
+     * @return {@code true} if the given value is included in this range.
+     * @throws IllegalArgumentException if the given range can not be converted to a valid type
+     *         through widening conversion.
+     */
+    public boolean containsAny(Number value) throws IllegalArgumentException {
+        if (value == null) {
+            return false;
+        }
+        final Class<? extends Number> type = Numbers.widestClass(elementType, value.getClass());
+        value = Numbers.cast(value, type);
+        if (minValue != null) {
+            @SuppressWarnings("unchecked")
+            final int c = ((Comparable) Numbers.cast(minValue, type)).compareTo(value);
+            if (isMinIncluded ? (c > 0) : (c >= 0)) {
+                return false;
+            }
+        }
+        if (maxValue != null) {
+            @SuppressWarnings("unchecked")
+            final int c = ((Comparable) Numbers.cast(maxValue, type)).compareTo(value);
+            if (isMaxIncluded ? (c < 0) : (c <= 0)) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    /**
+     * Returns {@code true} if the supplied range is fully contained within this range.
+     * This method converts {@code this} or the given argument to the widest numeric type,
+     * then delegates to {@link #contains(Range)}.
+     *
+     * @param  range The range to check for inclusion in this range.
+     * @return {@code true} if the given range is included in this range.
+     * @throws IllegalArgumentException if the given range can not be converted to a valid type
+     *         through widening conversion, or if the units of measurement are not convertible.
+     */
+    @SuppressWarnings({"unchecked","rawtypes"})
+    public boolean containsAny(final NumberRange<?> range) throws IllegalArgumentException {
+        /*
+         * The type bounds is actually <? extends Number & Comparable> but I'm unable to express
+         * it as local variable as of Java 7. So we have to bypass the compiler check, but those
+         * casts are actually safes.
+         */
+        final Class type = Numbers.widestClass(elementType, range.elementType);
+        return castTo(type).contains(convertAndCast(range, type));
+    }
+
+    /**
+     * Returns {@code true} if the supplied range is fully contained within this range.
+     * This method converts {@code this} or the given argument to the widest numeric type,
+     * then delegates to {@link #intersects(Range)}.
+     *
+     * @param  range The range to check for inclusion in this range.
+     * @return {@code true} if the given range is included in this range.
+     * @throws IllegalArgumentException if the given range can not be converted to a valid type
+     *         through widening conversion, or if the units of measurement are not convertible.
+     */
+    @SuppressWarnings({"unchecked","rawtypes"})
+    public boolean intersectsAny(final NumberRange<?> range) throws IllegalArgumentException {
+        final Class type = Numbers.widestClass(elementType, range.elementType);
+        return castTo(type).intersects(convertAndCast(range, type));
+    }
+
+    /**
+     * Returns the union of this range with the given range.
+     * This method converts {@code this} or the given argument to the widest numeric type,
+     * then delegates to {@link #intersect(Range)}.
+     *
+     * @param  range The range to add to this range.
+     * @return The union of this range with the given range.
+     * @throws IllegalArgumentException if the given range can not be converted to a valid type
+     *         through widening conversion, or if the units of measurement are not convertible.
+     */
+    @SuppressWarnings({"unchecked","rawtypes"})
+    public NumberRange<?> intersectAny(final NumberRange<?> range) throws IllegalArgumentException {
+        Class type = Numbers.widestClass(elementType, range.elementType);
+        final NumberRange<?> intersect = castTo(type).intersect(convertAndCast(range, type));
+        /*
+         * Use a finer type capable to holds the result (since the intersection
+         * may have reduced the range), but not finer than the finest type of
+         * the ranges used in the intersection calculation.
+         */
+        type = Numbers.narrowestClass(elementType, range.elementType);
+        type = Numbers.widestClass(type, Numbers.narrowestClass((Number) intersect.minValue));
+        type = Numbers.widestClass(type, Numbers.narrowestClass((Number) intersect.maxValue));
+        return intersect.castTo(type);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public NumberRange<T> intersect(final Range<T> range) {
+        return (NumberRange<T>) super.intersect(range);
+        // Should never throw ClassCastException because super.intersect(Range) invokes
+        // convertAndCast(…),  which is overridden in this class to create NumberRange.
+    }
+
+    /**
+     * Returns the union of this range with the given range.
+     * This method converts {@code this} or the given argument to the widest numeric type,
+     * then delegates to {@link #union(Range)}.
+     *
+     * @param  range The range to add to this range.
+     * @return The union of this range with the given range.
+     * @throws IllegalArgumentException if the given range can not be converted to a valid type
+     *         through widening conversion, or if the units of measurement are not convertible.
+     */
+    @SuppressWarnings({"unchecked","rawtypes"})
+    public NumberRange<?> unionAny(final NumberRange<?> range) throws IllegalArgumentException {
+        final Class type = Numbers.widestClass(elementType, range.elementType);
+        return castTo(type).union(convertAndCast(range, type));
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public NumberRange<T> union(final Range<T> range) {
+        return (NumberRange<T>) super.union(range);
+        // Should never throw ClassCastException because super.union(Range) invokes
+        // convertAndCast(…),  which is overridden in this class to create NumberRange.
+    }
+
+    /**
+     * Returns the range of values that are in this range but not in the given range.
+     * This method converts {@code this} or the given argument to the widest numeric type,
+     * then delegates to {@link #subtract(Range)}.
+     *
+     * @param  range The range to subtract.
+     * @return This range without the given range, as an array of length 0, 1 or 2.
+     * @throws IllegalArgumentException if the given range can not be converted to a valid type
+     *         through widening conversion, or if the units of measurement are not convertible.
+     */
+    @SuppressWarnings({"unchecked","rawtypes"})
+    public NumberRange<?>[] subtractAny(final NumberRange<?> range) throws IllegalArgumentException {
+        final Class type = Numbers.widestClass(elementType, range.elementType);
+        return castTo(type).subtract(convertAndCast(range, type));
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public NumberRange<T>[] subtract(final Range<T> range) {
+        return (NumberRange<T>[]) super.subtract(range);
+        // Should never throw ClassCastException because super.subtract(Range) invokes
+        // convertAndCast(…),  which is overridden in this class to create NumberRange.
+    }
 }

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=1445888&r1=1445887&r2=1445888&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 13 19:52:28 2013
@@ -20,11 +20,9 @@ import java.io.Serializable;
 import javax.measure.unit.Unit;
 import net.jcip.annotations.Immutable;
 import org.apache.sis.util.collection.CheckedContainer;
-import org.apache.sis.util.resources.Errors;
+import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.util.Numbers;
 
-import static org.apache.sis.util.ArgumentChecks.ensureNonNull;
-
 // Related to JDK7
 import java.util.Objects;
 
@@ -39,19 +37,18 @@ import java.util.Objects;
  * within the range. Null values are always considered <em>exclusive</em>,
  * since iterations over the values will never reach the infinite bound.
  *
- * {@section Type of range elements}
+ * {@section Type and value of range elements}
  * To be a member of a {@code Range}, the {@code <T>} type defining the range must implement the
- * {@link Comparable} interface. Some methods like {@link #contains(Comparable)}, which would
- * normally expect an argument of type {@code T}, accept the base type {@code Comparable<?>} in
- * order to allow widening conversions by the {@link NumberRange} subclass. Passing an argument of
- * non-convertible type to any method will cause an {@link IllegalArgumentException} to be thrown.
- *
- * {@note This class should never throw <code>ClassCastException</code>, unless there is a bug
- *        in the <code>Range</code> class or subclasses implementation.}
+ * {@link Comparable} interface. All argument values given to the methods of this class shall be
+ * or contain instances of the same {@code <T>} type. The type is enforced by parameterized type,
+ * but some subclasses may put additional constraints. For example {@link MeasurementRange} will
+ * additionally checks the units of measurement. Every methods defined in this class may throw
+ * an {@link IllegalArgumentException} if a given argument does not meet a constraint other than
+ * the type.
  *
  * {@section String representation}
  * The {@linkplain #toString() string representation} of a {@code Range} is defined
- * in a locale-insensitive way. In order to format a range using the current locale,
+ * in a locale-insensitive way. In order to format a range using a different locale,
  * or for parsing a range, use {@link RangeFormat}.
  *
  * @param <T> The type of range elements, typically a {@link Number} subclass or {@link java.util.Date}.
@@ -101,7 +98,7 @@ public class Range<T extends Comparable<
         isMinIncluded = range.isMinIncluded;
         maxValue      = range.maxValue;
         isMaxIncluded = range.isMaxIncluded;
-        validate();
+        assert validate();
     }
 
     /**
@@ -112,12 +109,13 @@ public class Range<T extends Comparable<
      * @param maxValue     The maximal value (inclusive), or {@code null} if none.
      */
     public Range(final Class<T> elementType, final T minValue, final T maxValue) {
+        ArgumentChecks.ensureNonNull("elementType", elementType);
         this.elementType   = elementType;
         this.minValue      = minValue;
         this.isMinIncluded = (minValue != null);
         this.maxValue      = maxValue;
         this.isMaxIncluded = (maxValue != null);
-        validate();
+        assert validate();
     }
 
     /**
@@ -133,6 +131,7 @@ public class Range<T extends Comparable<
             final T minValue, final boolean isMinIncluded,
             final T maxValue, final boolean isMaxIncluded)
     {
+        ArgumentChecks.ensureNonNull("elementType", elementType);
         /*
          * The 'isMin/Maxincluded' flags must be forced to 'false' if 'minValue' or 'maxValue'
          * are null. This is required for proper working of algorithms implemented in this class.
@@ -142,7 +141,7 @@ public class Range<T extends Comparable<
         this.isMinIncluded = isMinIncluded && (minValue != null);
         this.maxValue      = maxValue;
         this.isMaxIncluded = isMaxIncluded && (maxValue != null);
-        validate();
+        assert validate();
     }
 
     /**
@@ -172,48 +171,14 @@ public class Range<T extends Comparable<
     }
 
     /**
-     * Ensures that the given range uses the same element type 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 {
-        if (!elementType.isAssignableFrom(type)) {
-            throw new IllegalArgumentException(Errors.format(
-                    Errors.Keys.IllegalClass_2, elementType, type));
-        }
-    }
-
-    /**
-     * Ensures that {@link #elementType} is compatible with the type expected by this range class.
-     * This method is invoked at construction time for validating the type argument. This method
-     * is overridden by {@link NumberRange} and {@link DateRange} for more specific check.
-     */
-    void ensureValidType() throws IllegalArgumentException {
-        if (!Comparable.class.isAssignableFrom(elementType)) {
-            throw new IllegalArgumentException(Errors.format(
-                    Errors.Keys.IllegalClass_2, Comparable.class, elementType));
-        }
-    }
-
-    /**
      * Invoked by the constructors in order to ensure that the argument are of valid types.
+     * This check is performed only when assertions are enabled. This test is not needed in
+     * normal execution if the users do not bypass the checks performed by generic types.
      */
-    private void validate() {
-        ensureNonNull("elementType", elementType);
-        ensureValidType();
-        if (minValue != null) ensureCompatibleType(minValue.getClass());
-        if (maxValue != null) ensureCompatibleType(maxValue.getClass());
+    private boolean validate() {
+        ArgumentChecks.ensureCanCast("minValue", elementType, minValue);
+        ArgumentChecks.ensureCanCast("maxValue", elementType, maxValue);
+        return Comparable.class.isAssignableFrom(elementType);
     }
 
     /**
@@ -275,9 +240,12 @@ public class Range<T extends Comparable<
      * {@linkplain #getMaxValue() maximum value}, or if they are equal while
      * at least one of them is exclusive.
      *
+     * {@note This method is final because often used by the internal implementation.
+     *        Making the method final ensures that the other methods behave consistently.}
+     *
      * @return {@code true} if this range is empty.
      */
-    public boolean isEmpty() {
+    public final boolean isEmpty() {
         if (minValue == null || maxValue == null) {
             return false; // Unbounded: can't be empty.
         }
@@ -297,26 +265,11 @@ public class Range<T extends Comparable<
      *
      * @param  value The value to check for inclusion in this range.
      * @return {@code true} if the given value is included in this range.
-     * @throws IllegalArgumentException is the given value can not be converted to a valid type
-     *         through widening conversion.
      */
-    @SuppressWarnings("unchecked")
-    public boolean contains(final Comparable<?> value) throws IllegalArgumentException {
+    public boolean contains(final T value) {
         if (value == null) {
             return false;
         }
-        ensureCompatibleType(value.getClass());
-        return containsNC((T) value);
-    }
-
-    /**
-     * Implementation of {@link #contains(Comparable)} to be invoked directly by subclasses.
-     * "NC" stands for "No Conversion" - this method does not try to convert the value to a
-     * compatible type.
-     *
-     * @param value The value to test for inclusion. Can not be null.
-     */
-    final boolean containsNC(final T value) {
         /*
          * 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
@@ -350,20 +303,10 @@ public class Range<T extends Comparable<
      *
      * @param  range The range to check for inclusion in this range.
      * @return {@code true} if the given range is included in this range.
-     * @throws IllegalArgumentException is the bounds of the given range can not be converted to
-     *         a valid type through widening conversion, or if the units of measurement are not
-     *         convertible.
-     */
-    public boolean contains(final Range<?> range) throws IllegalArgumentException {
-        return containsNC(ensureCompatible(range));
-    }
-
-    /**
-     * Implementation of {@link #contains(Range)} to be invoked directly by subclasses.
-     * "NC" stands for "No Conversion" - this method does not try to convert the bounds
-     * to a compatible type.
+     * @throws IllegalArgumentException is the given range is incompatible,
+     *         for example because of incommensurable units of measurement.
      */
-    final boolean containsNC(final Range<? extends T> range) {
+    public boolean contains(final Range<? extends T> range) {
         /*
          * We could implement this method as below:
          *
@@ -394,19 +337,10 @@ public class Range<T extends Comparable<
      *
      * @param  range The range to check for intersection with this range.
      * @return {@code true} if the given range intersects this 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.
+     * @throws IllegalArgumentException is the given range is incompatible,
+     *         for example because of incommensurable units of measurement.
      */
-    public boolean intersects(final Range<?> range) throws IllegalArgumentException {
-        return intersectsNC(ensureCompatible(range));
-    }
-
-    /**
-     * Implementation of {@link #intersects(Range)} to be invoked directly by subclasses.
-     * "NC" stands for "No Conversion" - this method does not try to convert the bounds
-     * to a compatible type.
-     */
-    final boolean intersectsNC(final Range<? extends T> range) {
+    public boolean intersects(final Range<? extends T> range) {
         return (compareMinTo(range.maxValue, range.isMaxIncluded ? 0 : +1) <= 0) &&
                (compareMaxTo(range.minValue, range.isMinIncluded ? 0 : -1) >= 0);
     }
@@ -416,21 +350,10 @@ public class Range<T extends Comparable<
      *
      * @param  range The range to intersect.
      * @return The intersection 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<?> intersect(final Range<?> range) throws IllegalArgumentException {
-        return intersectNC(ensureCompatible(range));
-    }
-
-    /**
-     * Implementation of {@link #intersect(Range)} to be invoked directly by subclasses.
-     * "NC" stands for "No Conversion" - this method does not try to convert the bounds
-     * to a compatible type.
+     * @throws IllegalArgumentException is the given range is incompatible,
+     *         for example because of incommensurable units of measurement.
      */
-    final Range<? extends T> intersectNC(final Range<? extends T> range)
-            throws IllegalArgumentException
-    {
+    public Range<T> intersect(final Range<T> range) {
         /*
          * For two ranges [L₁ … H₁] and [L₂ … H₂], the intersection is given by
          * ([max(L₁, L₂) … min(H₁, H₂)]). Only two comparisons is needed.
@@ -442,7 +365,7 @@ public class Range<T extends Comparable<
          * be either 'this' or 'range), return that range. Otherwise we need to create a
          * new one.
          */
-        final Range<? extends T> intersect, min, max;
+        final Range<T> intersect, 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) {
@@ -459,19 +382,11 @@ public class Range<T extends Comparable<
      *
      * @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.
+     * @throws IllegalArgumentException is the given range is incompatible,
+     *         for example because of incommensurable units of measurement.
      */
-    public Range<?> union(final Range<?> range) throws IllegalArgumentException {
-        return unionNC(ensureCompatible(range));
-    }
-
-    /**
-     * 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;
+    public Range<T> union(final Range<T> range) {
+        final Range<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) {
@@ -496,19 +411,11 @@ public class Range<T extends Comparable<
      * </ul>
      *
      * @param  range The range to subtract.
-     * @return This range without 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<?>[] subtract(final Range<?> range) throws IllegalArgumentException {
-        return subtractNC(ensureCompatible(range));
-    }
-
-    /**
-     * Implementation of {@link #subtract(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.
+     * @return This range without the given range, as an array of length 0, 1 or 2.
+     * @throws IllegalArgumentException is the given range is incompatible,
+     *         for example because of incommensurable units of measurement.
      */
-    final Range<T>[] subtractNC(final Range<? extends T> range) throws IllegalArgumentException {
+    public Range<T>[] subtract(final Range<T> range) {
         final Range<T> subtract;
         if (!intersects(range)) {
             subtract = this;

Modified: sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/measure/NumberRangeTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/measure/NumberRangeTest.java?rev=1445888&r1=1445887&r2=1445888&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/measure/NumberRangeTest.java (original)
+++ sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/measure/NumberRangeTest.java Wed Feb 13 19:52:28 2013
@@ -48,10 +48,10 @@ public final strictfp class NumberRangeT
     }
 
     /**
-     * Tests union and intersection without units and type change.
+     * Tests union and intersection with {@link Integer} values.
      */
     @Test
-    public void testIntegerIntersect() {
+    public void testWithIntegers() {
         NumberRange<Integer> r1 = NumberRange.create(10, 20);
         NumberRange<Integer> r2 = NumberRange.create(15, 30);
         assertTrue (r1.equals(r1));
@@ -64,10 +64,10 @@ public final strictfp class NumberRangeT
     }
 
     /**
-     * Tests union and intersection with type change.
+     * Tests union and intersection with {@link Double} values.
      */
     @Test
-    public void testDoubleIntersect() {
+    public void testWithDoubles() {
         NumberRange<Double> r1 = NumberRange.create(10.0, 20.0);
         NumberRange<Double> r2 = NumberRange.create(15.0, 30.0);
         assertEquals(Double.class, r1.getElementType());
@@ -77,39 +77,39 @@ public final strictfp class NumberRangeT
     }
 
     /**
-     * Tests union and intersection with type change.
+     * Tests union and intersection involving a cast from integer to double values.
      */
     @Test
-    public void testIntegerDoubleIntersect() {
+    public void testIntegerWithDoubleArguments() {
         NumberRange<Integer> r1 = NumberRange.create(10, 20);
         NumberRange<Double>  r2 = NumberRange.create(15.0, 30.0);
         assertEquals(Integer.class, r1.getElementType());
         assertEquals(Double .class, r2.getElementType());
-        assertEquals(NumberRange.create(10.0, 30.0), r1.union(r2));
-        assertEquals(NumberRange.create(15, 20), r1.intersect(r2));
+        assertEquals(NumberRange.create(10.0, 30.0), r1.unionAny(r2));
+        assertEquals(NumberRange.create(15, 20), r1.intersectAny(r2));
 
         r2 = NumberRange.create(15.5, 30.0);
-        assertEquals(NumberRange.create(15.5f, 20.0f), r1.intersect(r2));
+        assertEquals(NumberRange.create(15.5f, 20.0f), r1.intersectAny(r2));
     }
 
     /**
-     * Tests union and intersection with type change.
+     * Tests union and intersection involving a cast from integer to double values.
      */
     @Test
-    public void testDoubleIntegerIntersect() {
+    public void testDoubleWithIntegerArguments() {
         NumberRange<Double>  r1 = NumberRange.create(10.0, 20.0);
         NumberRange<Integer> r2 = NumberRange.create(15, 30);
         assertEquals(Double .class, r1.getElementType());
         assertEquals(Integer.class, r2.getElementType());
-        assertEquals(NumberRange.create(10.0, 30.0), r1.union(r2));
-        assertEquals(NumberRange.create(15, 20), r1.intersect(r2));
+        assertEquals(NumberRange.create(10.0, 30.0), r1.unionAny(r2));
+        assertEquals(NumberRange.create(15, 20), r1.intersectAny(r2));
 
         r1 = NumberRange.create(10.0, 20.5);
-        assertEquals(NumberRange.create(15.0f, 20.5f), r1.intersect(r2));
+        assertEquals(NumberRange.create(15.0f, 20.5f), r1.intersectAny(r2));
     }
 
     /**
-     * Tests the {@link NumberRange#createBestFit} method.
+     * Tests the {@link NumberRange#createBestFit(Number, boolean, Number, boolean)} method.
      */
     @Test
     public void testCreateBestFit() {

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=1445888&r1=1445887&r2=1445888&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 13 19:52:28 2013
@@ -19,6 +19,7 @@ package org.apache.sis.measure;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
 
+import static org.junit.Assume.*;
 import static org.apache.sis.test.Assert.*;
 
 
@@ -75,10 +76,13 @@ public final strictfp class RangeTest ex
      * 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.
+     *
+     * <p>This test requires assertions to be enabled.</p>
      */
     @Test(expected = IllegalArgumentException.class)
     @SuppressWarnings({"unchecked", "rawtypes"})
     public void testConstructorErrors00() {
+        assumeTrue(Range.class.desiredAssertionStatus());
         new Range(Double.class, "error", "blast");
     }
 
@@ -87,10 +91,13 @@ public final strictfp class RangeTest ex
      * 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.
+     *
+     * <p>This test requires assertions to be enabled.</p>
      */
     @Test(expected = IllegalArgumentException.class)
     @SuppressWarnings({"unchecked", "rawtypes"})
     public void testConstructorErrors01() {
+        assumeTrue(Range.class.desiredAssertionStatus());
         new Range(String.class, 123.233, 8740.09);
     }
 
@@ -203,28 +210,6 @@ public final strictfp class RangeTest ex
     }
 
     /**
-     * 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<Double> doubleRange = new Range<>(Double.class, 2.0, 5.0);
-
-        intRange.contains(doubleRange);
-    }
-
-    /**
-     * 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<Double> doubleRange = new Range<>(Double.class, 2.0, 5.0);
-
-        intRange.contains(doubleRange);
-    }
-
-    /**
      * Tests the {@link Range#intersects(Range)} method.
      */
     @Test
@@ -240,17 +225,6 @@ public final strictfp class RangeTest ex
     }
 
     /**
-     * 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<Integer>   range2 = new Range<>(Integer.class, 5, 7);
-
-        range1.intersects(range2);
-    }
-
-    /**
      * Tests the {@link Range#intersect(Range)} method.
      */
     @Test



Mime
View raw message