sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1450981 - in /sis/branches/JDK7/sis-utility/src: main/java/org/apache/sis/io/ main/java/org/apache/sis/measure/ main/java/org/apache/sis/util/ main/java/org/apache/sis/util/resources/ test/java/org/apache/sis/measure/ test/java/org/apache/...
Date Wed, 27 Feb 2013 21:43:12 GMT
Author: desruisseaux
Date: Wed Feb 27 21:43:11 2013
New Revision: 1450981

URL: http://svn.apache.org/r1450981
Log:
Added, completed or tested a few more minor utility methods related to ValueRange annotation,
needed for the ungoing port of metadata classes.

Modified:
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/io/DefaultFormat.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/ValueRange.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/Numbers.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/ObjectConverters.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
    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/util/NumbersTest.java

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/io/DefaultFormat.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/io/DefaultFormat.java?rev=1450981&r1=1450980&r2=1450981&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/io/DefaultFormat.java [UTF-8]
(original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/io/DefaultFormat.java [UTF-8]
Wed Feb 27 21:43:11 2013
@@ -97,7 +97,7 @@ final class DefaultFormat extends Format
      * @throws NumberFormatException If the parsing failed.
      */
     private Object valueOf(final String source) throws NumberFormatException {
-        return (type != Number.class) ? Numbers.valueOf(type, source) : Numbers.narrowestNumber(source);
+        return (type != Number.class) ? Numbers.valueOf(source, type) : Numbers.narrowestNumber(source);
     }
 
     /**

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=1450981&r1=1450980&r2=1450981&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/MeasurementRange.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/MeasurementRange.java
[UTF-8] Wed Feb 27 21:43:11 2013
@@ -141,6 +141,25 @@ public class MeasurementRange<E extends 
     }
 
     /**
+     * Constructs a range of the given type with values from the given annotation.
+     * This constructor does not verify if the given type is wide enough for the values of
+     * the given annotation, because those information are usually static. If nevertheless
+     * the given type is not wide enough, then the values are truncated in the same way
+     * than the Java language casts primitive types.
+     *
+     * @param  type  The element type, restricted to one of {@link Byte}, {@link Short},
+     *               {@link Integer}, {@link Long}, {@link Float} or {@link Double}.
+     * @param  range The range of values.
+     * @param  unit  The unit of measurement, or {@code null} if unknown.
+     * @throws IllegalArgumentException If the given type is not one of the primitive
+     *         wrappers for numeric types.
+     */
+    public MeasurementRange(final Class<E> type, final ValueRange range, final Unit<?>
unit) throws IllegalArgumentException {
+        super(type, range);
+        this.unit = unit;
+    }
+
+    /**
      * Constructs a range of {@link Number} objects.
      *
      * @param type          The element type, usually one of {@link Float} or {@link Double}.

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=1450981&r1=1450980&r2=1450981&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/NumberRange.java [UTF-8]
(original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/NumberRange.java [UTF-8]
Wed Feb 27 21:43:11 2013
@@ -271,12 +271,18 @@ public class NumberRange<E extends Numbe
 
     /**
      * Constructs a range of the given type with values from the given annotation.
+     * This constructor does not verify if the given type is wide enough for the values of
+     * the given annotation, because those information are usually static. If nevertheless
+     * the given type is not wide enough, then the values are truncated in the same way
+     * than the Java language casts primitive types.
      *
-     * @param type  The element type, usually one of {@link Byte}, {@link Short},
-     *              {@link Integer}, {@link Long}, {@link Float} or {@link Double}.
-     * @param range The range of values.
+     * @param  type  The element type, restricted to one of {@link Byte}, {@link Short},
+     *               {@link Integer}, {@link Long}, {@link Float} or {@link Double}.
+     * @param  range The range of values.
+     * @throws IllegalArgumentException If the given type is not one of the primitive
+     *         wrappers for numeric types.
      */
-    public NumberRange(final Class<E> type, final ValueRange range) {
+    public NumberRange(final Class<E> type, final ValueRange range) throws IllegalArgumentException
{
         super(type, Numbers.cast(valueOf("minimum", range.minimum(), Double.NEGATIVE_INFINITY),
type), range.isMinIncluded(),
                     Numbers.cast(valueOf("maximum", range.maximum(), Double.POSITIVE_INFINITY),
type), range.isMaxIncluded());
     }

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/ValueRange.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/ValueRange.java?rev=1450981&r1=1450980&r2=1450981&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/ValueRange.java [UTF-8]
(original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/measure/ValueRange.java [UTF-8]
Wed Feb 27 21:43:11 2013
@@ -18,6 +18,7 @@ package org.apache.sis.measure;
 
 import java.lang.annotation.Target;
 import java.lang.annotation.Retention;
+import java.lang.annotation.Documented;
 import java.lang.annotation.ElementType;
 import java.lang.annotation.RetentionPolicy;
 
@@ -73,6 +74,7 @@ import java.lang.annotation.RetentionPol
  *
  * @see NumberRange#NumberRange(Class, ValueRange)
  */
+@Documented
 @Target({ElementType.FIELD, ElementType.METHOD})
 @Retention(RetentionPolicy.RUNTIME)
 public @interface ValueRange {

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/Numbers.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/Numbers.java?rev=1450981&r1=1450980&r2=1450981&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/Numbers.java [UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/Numbers.java [UTF-8] Wed
Feb 27 21:43:11 2013
@@ -275,6 +275,7 @@ public final class Numbers extends Stati
         }
         if (c1 == null) return c2;
         if (c2 == null) return c1;
+        // At this point, m1 and m2 can not be null.
         return (m1.ordinal >= m2.ordinal) ? c1 : c2;
     }
 
@@ -336,6 +337,7 @@ public final class Numbers extends Stati
         }
         if (c1 == null) return c2;
         if (c2 == null) return c1;
+        // At this point, m1 and m2 can not be null.
         return (m1.ordinal < m2.ordinal) ? c1 : c2;
     }
 
@@ -511,9 +513,9 @@ public final class Numbers extends Stati
      *
      * <ul>
      *   <li>If the given type is {@code Double.class}, then this method returns
-     *       <code>{@linkplain Double#valueOf(double) Double.valueOf}(n.doubleValue())</code>;</li>
+     *       <code>{@linkplain Double#valueOf(double) Double.valueOf}(number.doubleValue())</code>;</li>
      *   <li>If the given type is {@code Float.class}, then this method returns
-     *       <code>{@linkplain Float#valueOf(float) Float.valueOf}(n.floatValue())</code>;</li>
+     *       <code>{@linkplain Float#valueOf(float) Float.valueOf}(number.floatValue())</code>;</li>
      *   <li>And likewise for all remaining known types.</li>
      * </ul>
      *
@@ -527,27 +529,60 @@ public final class Numbers extends Stati
      * then the behavior depends on the implementation of the corresponding
      * {@code Number.fooValue()} method - typically, the value is just rounded or truncated.
      *
-     * @param  <N> The class to cast to.
-     * @param  n The number to cast.
-     * @param  c The destination type.
-     * @return The number casted to the given type.
+     * @param  <N>    The class to cast to.
+     * @param  number The number to cast, or {@code null}.
+     * @param  type   The destination type.
+     * @return The number casted to the given type, or {@code null} if the given value was
null.
      * @throws IllegalArgumentException If the given type is not one of the primitive
      *         wrappers for numeric types.
      */
     @SuppressWarnings("unchecked")
-    public static <N extends Number> N cast(final Number n, final Class<N> c)
+    public static <N extends Number> N cast(final Number number, final Class<N>
type)
             throws IllegalArgumentException
     {
-        if (n == null || n.getClass() == c) {
-            return (N) n;
+        if (number == null || number.getClass() == type) {
+            return (N) number;
         }
-        if (c == Byte   .class) return (N) Byte   .valueOf(n.  byteValue());
-        if (c == Short  .class) return (N) Short  .valueOf(n. shortValue());
-        if (c == Integer.class) return (N) Integer.valueOf(n.   intValue());
-        if (c == Long   .class) return (N) Long   .valueOf(n.  longValue());
-        if (c == Float  .class) return (N) Float  .valueOf(n. floatValue());
-        if (c == Double .class) return (N) Double .valueOf(n.doubleValue());
-        throw unknownType(c);
+        if (type == Double .class) return (N) Double .valueOf(number.doubleValue());
+        if (type == Float  .class) return (N) Float  .valueOf(number. floatValue());
+        if (type == Long   .class) return (N) Long   .valueOf(number.  longValue());
+        if (type == Integer.class) return (N) Integer.valueOf(number.   intValue());
+        if (type == Short  .class) return (N) Short  .valueOf(number. shortValue());
+        if (type == Byte   .class) return (N) Byte   .valueOf(number.  byteValue());
+        throw unknownType(type);
+    }
+
+    /**
+     * Wraps the given value in a {@code Number} of the specified class.
+     * The given type shall be one of {@link Byte}, {@link Short}, {@link Integer},
+     * {@link Long}, {@link Float} or {@link Double} classes. Furthermore, the given
+     * value shall be convertible to the given class without precision lost,
+     * otherwise an {@link IllegalArgumentException} will be thrown.
+     *
+     * @param  <N> The wrapper class.
+     * @param  value The value to wrap.
+     * @param  type The desired wrapper class.
+     * @return The value wrapped in an object of the given class.
+     * @throws IllegalArgumentException If the given type is not one of the primitive
+     *         wrappers for numeric types, or if the given value can not be wrapped in
+     *         an instance of the given class without precision lost.
+     */
+    @SuppressWarnings("unchecked")
+    public static <N extends Number> N wrap(final double value, final Class<N>
type)
+            throws IllegalArgumentException
+    {
+        final N number;
+             if (type == Double .class) return   (N) Double .valueOf(value); // No need to
verify.
+        else if (type == Float  .class) number = (N) Float  .valueOf((float) value);
+        else if (type == Long   .class) number = (N) Long   .valueOf((long)  value);
+        else if (type == Integer.class) number = (N) Integer.valueOf((int)   value);
+        else if (type == Short  .class) number = (N) Short  .valueOf((short) value);
+        else if (type == Byte   .class) number = (N) Byte   .valueOf((byte)  value);
+        else throw unknownType(type);
+        if (Double.doubleToLongBits(number.doubleValue()) != Double.doubleToLongBits(value))
{
+            throw new IllegalArgumentException(Errors.format(Errors.Keys.CanNotConvertValue_2,
value, type));
+        }
+        return number;
     }
 
     /**
@@ -570,15 +605,15 @@ public final class Numbers extends Stati
      *        <code>ConverterRegistry</code> class for a more generic method.}
      *
      * @param  <T> The requested type.
-     * @param  type The requested type.
      * @param  value the value to parse.
+     * @param  type The requested type.
      * @return The value object, or {@code null} if {@code value} was null.
      * @throws IllegalArgumentException if {@code type} is not a recognized type.
      * @throws NumberFormatException if {@code type} is a subclass of {@link Number} and
the
      *         string value is not parseable as a number of the specified type.
      */
     @SuppressWarnings("unchecked")
-    public static <T> T valueOf(final Class<T> type, String value)
+    public static <T> T valueOf(String value, final Class<T> type)
             throws IllegalArgumentException, NumberFormatException
     {
         if (value == null || type == String.class) {

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/ObjectConverters.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/ObjectConverters.java?rev=1450981&r1=1450980&r2=1450981&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/ObjectConverters.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/ObjectConverters.java
[UTF-8] Wed Feb 27 21:43:11 2013
@@ -19,6 +19,7 @@ package org.apache.sis.util;
 import java.util.Map;
 import java.util.Set;
 import org.apache.sis.util.collection.CollectionsExt;
+import org.apache.sis.util.resources.Errors;
 
 
 /**
@@ -49,6 +50,23 @@ public final class ObjectConverters exte
     }
 
     /**
+     * Returns a converter for the specified source and target classes.
+     *
+     * @param  <S> The source class.
+     * @param  <T> The target class.
+     * @param  source The source class.
+     * @param  target The target class, or {@code Object.class} for any.
+     * @return The converter from the specified source class to the target class.
+     * @throws UnconvertibleObjectException if no converter is found.
+     */
+    public static <S,T> ObjectConverter<S,T> find(final Class<S> source,
final Class<T> target)
+            throws UnconvertibleObjectException
+    {
+        // TODO: port the implementation from Geotk
+        throw new UnconvertibleObjectException(Errors.format(Errors.Keys.CanNotConvertFromType_2,
source, target));
+    }
+
+    /**
      * Returns a set whose elements are derived <cite>on-the-fly</cite> from
the given set.
      * Conversions from the original elements to the derived elements are performed when
needed
      * by invoking the {@link ObjectConverter#convert(Object)} method on the given converter.

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java?rev=1450981&r1=1450980&r2=1450981&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
[UTF-8] Wed Feb 27 21:43:11 2013
@@ -68,6 +68,16 @@ public final class Errors extends Indexe
         public static final int CanNotComputeDerivative = 44;
 
         /**
+         * Can not convert from type ‘{0}’ to type ‘{1}’.
+         */
+        public static final int CanNotConvertFromType_2 = 72;
+
+        /**
+         * Can not convert value “{0}” to type ‘{1}’.
+         */
+        public static final int CanNotConvertValue_2 = 74;
+
+        /**
          * Class ‘{0}’ is not final.
          */
         public static final int ClassNotFinal_1 = 71;
@@ -245,6 +255,11 @@ public final class Errors extends Indexe
         public static final int NegativeArgument_2 = 8;
 
         /**
+         * No property named “{0}” has been found in “{1}”.
+         */
+        public static final int NoSuchProperty_2 = 73;
+
+        /**
          * No unit of measurement has been specified.
          */
         public static final int NoUnit = 68;

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties?rev=1450981&r1=1450980&r2=1450981&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
[ISO-8859-1] (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
[ISO-8859-1] Wed Feb 27 21:43:11 2013
@@ -24,6 +24,8 @@
 # programmatic parameters do not have to be last in the formatted text, since each localized
message
 # can reorder the parameters as they want.
 #
+CanNotConvertFromType_2         = Can not convert from type \u2018{0}\u2019 to type \u2018{1}\u2019.
+CanNotConvertValue_2            = Can not convert value \u201c{0}\u201d to type \u2018{1}\u2019.
 CanNotComputeDerivative         = Can not compute the derivative.
 ClassNotFinal_1                 = Class \u2018{0}\u2019 is not final.
 CloneNotSupported_1             = Can not clone an object of type \u2018{0}\u2019.
@@ -72,6 +74,7 @@ NonTemporalUnit_1               = \u201c
 NotANumber_1                    = Argument \u2018{0}\u2019 shall not be NaN (Not-a-Number).
 NotAPrimitiveWrapper_1          = Class \u2018{0}\u2019 is not a primitive type wrapper.
 NotComparableClass_1            = Class \u2018{0}\u2019 is not a comparable.
+NoSuchProperty_2                = No property named \u201c{0}\u201d has been found in \u201c{1}\u201d.
 NoUnit                          = No unit of measurement has been specified.
 NullArgument_1                  = Argument \u2018{0}\u2019 shall not be null.
 # Use the OGC/ISO "Dictionary" word instead of "Map" for avoiding confusion with geographic
map.

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties?rev=1450981&r1=1450980&r2=1450981&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
[ISO-8859-1] (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
[ISO-8859-1] Wed Feb 27 21:43:11 2013
@@ -14,6 +14,8 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 #
+CanNotConvertFromType_2         = Ne peut pas convertir du type \u2018{0}\u2019 vers le type
\u2018{1}\u2019.
+CanNotConvertValue_2            = La valeur \u201c{0}\u201d ne peut pas \u00eatre convertie
vers le type \u2018{1}\u2019.
 CanNotComputeDerivative         = La d\u00e9riv\u00e9 ne peut pas \u00eatre calcul\u00e9e.
 ClassNotFinal_1                 = La classe \u2018{0}\u2019 n\u2019est pas finale.
 CloneNotSupported_1             = Un objet de type \u2018{0}\u2019 ne peut pas \u00eatre
clon\u00e9.
@@ -62,6 +64,7 @@ NonTemporalUnit_1               = \u201c
 NotANumber_1                    = L\u2019argument \u2018{0}\u2019 ne doit pas \u00eatre NaN
(Not-a-Number).
 NotAPrimitiveWrapper_1          = La classe \u2018{0}\u2019 n\u2019est pas un adaptateur
d\u2019un type primitif.
 NotComparableClass_1            = La classe \u2018{0}\u2019 n\u2019est pas comparable.
+NoSuchProperty_2                = Aucune propri\u00e9t\u00e9 nomm\u00e9e \u201c{0}\u201d
n\u2019a \u00e9t\u00e9 trouv\u00e9e dans \u201c{1}\u201d.
 NoUnit                          = Aucune unit\u00e9 de mesure n\u2019a \u00e9t\u00e9 sp\u00e9cifi\u00e9e.
 NullArgument_1                  = L\u2019argument \u2018{0}\u2019 ne doit pas \u00eatre nul.
 NullMapKey                      = La cl\u00e9 nulle n\u2019est pas autoris\u00e9e dans ce
dictionnaire.

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=1450981&r1=1450980&r2=1450981&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/measure/NumberRangeTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/measure/NumberRangeTest.java
[UTF-8] Wed Feb 27 21:43:11 2013
@@ -116,4 +116,20 @@ public final strictfp class NumberRangeT
         assertEquals(NumberRange.create((short) 2, true, (short) 200, true),
                 NumberRange.createBestFit(2, true, 200.0, true));
     }
+
+    /**
+     * Tests the construction using the {@link ValueRange} annotation.
+     * The annotation used for this test is declared on this test method.
+     *
+     * @throws NoSuchMethodException Should never happen.
+     */
+    @Test
+    @ValueRange(minimum=4, maximum=8, isMaxIncluded=false)
+    public void testValueRangeAnnotation() throws NoSuchMethodException {
+        final ValueRange values = NumberRangeTest.class
+                .getMethod("testValueRangeAnnotation").getAnnotation(ValueRange.class);
+        assertNotNull("Annotation not found.", values);
+        final NumberRange<Short> range = new NumberRange<>(Short.class, values);
+        assertEquals(NumberRange.create((short) 4, true, (short) 8, false), range);
+    }
 }

Modified: sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/NumbersTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/NumbersTest.java?rev=1450981&r1=1450980&r2=1450981&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/NumbersTest.java [UTF-8]
(original)
+++ sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/NumbersTest.java [UTF-8]
Wed Feb 27 21:43:11 2013
@@ -174,7 +174,33 @@ public final class NumbersTest extends T
      */
     @Test
     public void testCast() {
-        assertEquals(Byte .valueOf((byte)  10), cast(Integer.valueOf(10), Byte .class));
-        assertEquals(Float.valueOf((float) 10), cast(Integer.valueOf(10), Float.class));
+        final Integer value = new Integer(10); // Intentionally a new instance.
+        assertEquals(Byte   .valueOf((byte)   10), cast(value, Byte   .class));
+        assertEquals(Short  .valueOf((short)  10), cast(value, Short  .class));
+        assertSame  (value,                        cast(value, Integer.class));
+        assertEquals(Long   .valueOf((long)   10), cast(value, Long   .class));
+        assertEquals(Float  .valueOf((float)  10), cast(value, Float  .class));
+        assertEquals(Double .valueOf((double) 10), cast(value, Double .class));
+    }
+
+    /**
+     * Tests {@link Numbers#wrap(double, Class)}.
+     */
+    @Test
+    public void testWrap() {
+        final double value = 10;
+        assertEquals(Byte   .valueOf((byte)   10), wrap(value, Byte   .class));
+        assertEquals(Short  .valueOf((short)  10), wrap(value, Short  .class));
+        assertEquals(Integer.valueOf(         10), wrap(value, Integer.class));
+        assertEquals(Long   .valueOf((long)   10), wrap(value, Long   .class));
+        assertEquals(Float  .valueOf((float)  10), wrap(value, Float  .class));
+        assertEquals(Double .valueOf((double) 10), wrap(value, Double .class));
+        try {
+            final Integer n = wrap(4.5, Integer.class);
+            fail("Expected an exception but got " + n);
+        } catch (IllegalArgumentException e) {
+            // This is the expected exception.
+            assertTrue(e.getMessage().contains("Integer"));
+        }
     }
 }



Mime
View raw message