sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1765887 - in /sis/branches/JDK8/core/sis-utility/src: main/java/org/apache/sis/measure/ main/java/org/apache/sis/util/resources/ test/java/org/apache/sis/measure/
Date Thu, 20 Oct 2016 21:29:57 GMT
Author: desruisseaux
Date: Thu Oct 20 21:29:57 2016
New Revision: 1765887

URL: http://svn.apache.org/viewvc?rev=1765887&view=rev
Log:
Add tests for SystemUnit.asType(Class<Quantity>).

Modified:
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/SystemUnit.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Units.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/SystemUnitTest.java

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/SystemUnit.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/SystemUnit.java?rev=1765887&r1=1765886&r2=1765887&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/SystemUnit.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/SystemUnit.java
[UTF-8] Thu Oct 20 21:29:57 2016
@@ -209,17 +209,32 @@ final class SystemUnit<Q extends Quantit
     public <T extends Quantity<T>> Unit<T> asType(final Class<T>
type) throws ClassCastException {
         ArgumentChecks.ensureNonNull("type", type);
         if (type == quantity) {
+            if (getSymbol() == null) {
+                // If this unit has no symbol, opportunistically supply a symbol if we find
it.
+                final SystemUnit<T> unit = Units.get(type);
+                if (unit != null) {
+                    return unit;
+                }
+            }
             return (Unit<T>) this;
         }
-        if (dimension.isDimensionless()) {
-            final SystemUnit<T> target = Units.get(type);
-            if (target != null && target.dimension.isDimensionless()) {
-                return target;
-            }
+        /*
+         * Verifies what are the expected dimensions of the given type by searching for the
corresponding unit.
+         * If we find that unit, returns it on the assumption that its symbol is right while
the symbol of this
+         * unit may no longer be right for the given type.  If we can not find a pre-defined
units, then create
+         * a new unit with the requested type but no symbol since we do not know yet what
the symbol should be
+         * for the new quantity.
+         */
+        SystemUnit<T> unit = Units.get(type);
+        if (unit == null) {
+            unit = new SystemUnit<>(type, dimension, null, (short) 0);            
 // Intentionally no symbol.
+        }
+        if (!dimension.equals(unit.dimension)) {
+            throw new ClassCastException(Errors.format(Errors.Keys.IncompatibleUnitDimension_5,
new Object[] {
+                    this, (quantity != null) ? quantity.getSimpleName() : "?", dimension,
+                    type.getSimpleName(), unit.dimension}));
         }
-        throw new ClassCastException(Errors.format(Errors.Keys.CanNotConvertFromType_2,
-                "Unit<" + (quantity != null ? quantity.getSimpleName() : "?") + '>',
-                "Unit<" + type.getSimpleName() + '>'));
+        return unit;
     }
 
     /**
@@ -322,14 +337,28 @@ final class SystemUnit<Q extends Quantit
             return this;
         }
         final SystemUnit<Q> alt = new SystemUnit<>(quantity, dimension, symbol,
(short) 0);
-        final Unit<?> existing = (Unit<?>) UnitRegistry.putIfAbsent(symbol, alt);
-        if (existing != null) {
-            if (existing instanceof SystemUnit<?> && ((SystemUnit<?>)
existing).quantity == quantity
-                    && dimension.equals(existing.getDimension()))
-            {
-                return (Unit<Q>) existing;
+        if (quantity != null) {
+            /*
+             * Use the cache only if this unit has a non-null quantity type. Do not use the
cache even
+             * in read-only mode when 'quantity' is null because we would be unable to guarantee
that
+             * the parameterized type <Q> is correct.
+             */
+            final Unit<?> existing = (Unit<?>) UnitRegistry.putIfAbsent(symbol,
alt);
+            if (existing != null) {
+                if (existing instanceof SystemUnit<?>
+                        && ((SystemUnit<?>) existing).quantity == quantity
+                        && dimension.equals(existing.getDimension()))
+                {
+                    return (Unit<Q>) existing;
+                }
+                throw new IllegalArgumentException(Errors.format(Errors.Keys.ElementAlreadyPresent_1,
symbol));
             }
-            throw new IllegalArgumentException(Errors.format(Errors.Keys.ElementAlreadyPresent_1,
symbol));
+            /*
+             * This method may be invoked for a new quantity, after a call to 'asType(Class)'.
+             * Try to register the new unit for that Quantity. But if another unit is already
+             * registered for that Quantity, this is not necessarily an error.
+             */
+            UnitRegistry.putIfAbsent(quantity, alt);
         }
         return alt;
     }

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Units.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Units.java?rev=1765887&r1=1765886&r2=1765887&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Units.java [UTF-8]
(original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Units.java [UTF-8]
Thu Oct 20 21:29:57 2016
@@ -21,20 +21,8 @@ import javax.measure.Unit;
 import javax.measure.UnitConverter;
 import javax.measure.format.ParserException;
 import javax.measure.Quantity;
-import javax.measure.quantity.Angle;
-import javax.measure.quantity.Area;
-import javax.measure.quantity.Dimensionless;
-import javax.measure.quantity.Energy;
-import javax.measure.quantity.Force;
-import javax.measure.quantity.Frequency;
-import javax.measure.quantity.Length;
-import javax.measure.quantity.Mass;
-import javax.measure.quantity.Power;
-import javax.measure.quantity.Pressure;
-import javax.measure.quantity.Speed;
-import javax.measure.quantity.Temperature;
-import javax.measure.quantity.Time;
-import javax.measure.quantity.Volume;
+import javax.measure.quantity.*;
+import javax.measure.quantity.Angle;            // Because of name collision with Angle in
this SIS package.
 
 import org.apache.sis.util.Static;
 import org.apache.sis.util.Workaround;

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java?rev=1765887&r1=1765886&r2=1765887&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
[UTF-8] Thu Oct 20 21:29:57 2016
@@ -388,6 +388,12 @@ public final class Errors extends Indexe
         public static final short IncompatiblePropertyValue_1 = 63;
 
         /**
+         * The “{0}” unit of measurement has dimension of ‘{1}’ ({2}). It is incompatible
with
+         * dimension of ‘{3}’ ({4}).
+         */
+        public static final short IncompatibleUnitDimension_5 = 163;
+
+        /**
          * Unit “{0}” is incompatible with current value.
          */
         public static final short IncompatibleUnit_1 = 64;

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties?rev=1765887&r1=1765886&r2=1765887&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
[ISO-8859-1] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
[ISO-8859-1] Thu Oct 20 21:29:57 2016
@@ -90,6 +90,7 @@ IncompatibleFormat_2              = Can
 IncompatiblePropertyValue_1       = Property \u201c{0}\u201d has an incompatible value.
 IncompatibleUnit_1                = Unit \u201c{0}\u201d is incompatible with current value.
 IncompatibleUnits_2               = Units \u201c{0}\u201d and \u201c{1}\u201d are incompatible.
+IncompatibleUnitDimension_5       = The \u201c{0}\u201d unit of measurement has dimension
of \u2018{1}\u2019 ({2}). It is incompatible with dimension of \u2018{3}\u2019 ({4}).
 InconsistentAttribute_2           = Value \u201c{1}\u201d of attribute \u2018{0}\u2019 is
inconsistent with other attributes.
 InconsistentNamespace_2           = Expected \u201c{0}\u201d namespace for \u201c{1}\u201d.
 InconsistentTableColumns          = Inconsistent table columns.

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties?rev=1765887&r1=1765886&r2=1765887&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
[ISO-8859-1] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
[ISO-8859-1] Thu Oct 20 21:29:57 2016
@@ -87,6 +87,7 @@ IncompatibleFormat_2              = Le f
 IncompatiblePropertyValue_1       = La valeur de la propri\u00e9t\u00e9 \u00ab\u202f{0}\u202f\u00bb
n\u2019est pas compatible.
 IncompatibleUnit_1                = L\u2019unit\u00e9 \u00ab\u202f{0}\u202f\u00bb n\u2019est
pas compatible avec la valeur actuelle.
 IncompatibleUnits_2               = Les unit\u00e9s \u00ab\u202f{0}\u202f\u00bb et \u00ab\u202f{1}\u202f\u00bb
ne sont pas compatibles.
+IncompatibleUnitDimension_5       = L\u2019unit\u00e9 de mesure \u00ab\u202f{0}\u202f\u00bb
a la dimension de \u2018{1}\u2019 ({2}). Elle est incompatible avec la dimension de \u2018{3}\u2019
({4}).
 InconsistentAttribute_2           = La valeur \u00ab\u202f{1}\u202f\u00bb de l\u2019attribut
\u2018{0}\u2019 n\u2019est pas coh\u00e9rente avec celles des autres attributs.
 InconsistentNamespace_2           = L\u2019espace de nom \u201c{0}\u201d \u00e9tait attendu
pour \u201c{1}\u201d.
 InconsistentTableColumns          = Les colonnes des tables ne sont pas coh\u00e9rentes.

Modified: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/SystemUnitTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/SystemUnitTest.java?rev=1765887&r1=1765886&r2=1765887&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/SystemUnitTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/SystemUnitTest.java
[UTF-8] Thu Oct 20 21:29:57 2016
@@ -16,12 +16,16 @@
  */
 package org.apache.sis.measure;
 
+import java.util.Arrays;
 import java.util.Map;
 import java.util.HashMap;
 import java.util.Collections;
 import javax.measure.Unit;
 import javax.measure.UnconvertibleException;
 import javax.measure.IncommensurableException;
+import javax.measure.quantity.Length;
+import javax.measure.quantity.Time;
+import javax.measure.Quantity;
 import org.apache.sis.test.DependsOnMethod;
 import org.apache.sis.test.DependsOn;
 import org.apache.sis.test.TestCase;
@@ -260,4 +264,67 @@ public final strictfp class SystemUnitTe
             assertTrue(message, message.contains("°"));
         }
     }
+
+    /**
+     * Tests {@link SystemUnit#asType(Class)}.
+     */
+    @Test
+    public void testAsType() {
+        assertSame(Units.METRE,  Units.METRE .asType(Length.class));
+        assertSame(Units.SECOND, Units.SECOND.asType(Time.class));
+        /*
+         * Test with units outside the pre-defined constants in the Units class.
+         */
+        final Unit<Length> anonymous = new SystemUnit<>(Length.class, (UnitDimension)
Units.METRE.getDimension(), null,  (short) 0);
+        final Unit<Length> otherName = new SystemUnit<>(Length.class, (UnitDimension)
Units.METRE.getDimension(), "Foo", (short) 0);
+        assertSame(Units.METRE, anonymous.asType(Length.class));
+        assertSame(otherName,   otherName.asType(Length.class));
+        /*
+         * Verify that the unit can not be casted to an incompatible units.
+         */
+        for (final Unit<Length> unit : Arrays.asList(Units.METRE, anonymous, otherName))
{
+            try {
+                unit.asType(Time.class);
+                fail("Expected an exception for incompatible quantity types.");
+            } catch (ClassCastException e) {
+                final String message = e.getMessage();
+                assertTrue(message, message.contains("Length"));
+                assertTrue(message, message.contains("Time"));
+            }
+        }
+    }
+
+    /**
+     * Tests {@link SystemUnit#asType(Class)} for a quantity unknown to Apache SIS.
+     */
+    @Test
+    @DependsOnMethod({"testAsType", "testAlternate"})
+    public void testAsTypeForNewQuantity() {
+        /*
+         * Tests with a new quantity type unknown to Apache SIS.
+         * SIS can not proof that the type is wrong, so it should accept it.
+         */
+        final Unit<Strange> strange = Units.METRE.asType(Strange.class);
+        final Unit<Strange> named   = strange.alternate("strange");
+        assertNull  ("Should not have symbol since this is a unit for a new quantity.", strange.getSymbol());
+        assertEquals("Should have a name since we invoked 'alternate'.", "strange", named.getSymbol());
+        assertSame  ("Should prefer the named instance.", named, Units.METRE.asType(Strange.class));
+        assertSame  ("Go back to the fundamental unit.",  Units.METRE, named.asType(Length.class));
+        for (final Unit<Strange> unit : Arrays.asList(strange, named)) {
+            try {
+                unit.asType(Time.class);
+                fail("Expected an exception for incompatible quantity types.");
+            } catch (ClassCastException e) {
+                final String message = e.getMessage();
+                assertTrue(message, message.contains("Strange"));
+                assertTrue(message, message.contains("Time"));
+            }
+        }
+    }
+
+    /**
+     * A dummy quantity type for tests using a quantity type unknown to Apache SIS.
+     */
+    private static interface Strange extends Quantity<Strange> {
+    }
 }



Mime
View raw message