sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] branch geoapi-4.0 updated: Fix an erroneous conversion factor in unit conversions with salinity. https://issues.apache.org/jira/browse/SIS-413
Date Thu, 19 Jul 2018 14:47:11 GMT
This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git


The following commit(s) were added to refs/heads/geoapi-4.0 by this push:
     new 3fb3b22  Fix an erroneous conversion factor in unit conversions with salinity. https://issues.apache.org/jira/browse/SIS-413
3fb3b22 is described below

commit 3fb3b22f5a1f89059310bffaf8b30a2d1ba4a462
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Thu Jul 19 16:46:29 2018 +0200

    Fix an erroneous conversion factor in unit conversions with salinity.
    https://issues.apache.org/jira/browse/SIS-413
---
 .../main/java/org/apache/sis/measure/Salinity.java |  3 ++-
 .../java/org/apache/sis/measure/UnitRegistry.java  | 22 ++++++++++------------
 .../main/java/org/apache/sis/measure/Units.java    | 15 ++++++++++++---
 .../java/org/apache/sis/measure/package-info.java  |  2 +-
 .../java/org/apache/sis/measure/UnitsTest.java     | 16 +++++++++++++++-
 5 files changed, 40 insertions(+), 18 deletions(-)

diff --git a/core/sis-utility/src/main/java/org/apache/sis/measure/Salinity.java b/core/sis-utility/src/main/java/org/apache/sis/measure/Salinity.java
index d7e241b..1c26c35 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/measure/Salinity.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/measure/Salinity.java
@@ -26,7 +26,7 @@ import javax.measure.Quantity;
  * However Apache SIS uses the "psu" symbol for avoiding confusion with other dimensionless
units.
  *
  * <p>Most quantity types are defined in the {@link javax.measure.quantity}.
- * This {@code AngularVelocity} type is an extension to the standard types.</p>
+ * This {@code Salinity} type is an extension to the standard types.</p>
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @version 0.8
@@ -34,6 +34,7 @@ import javax.measure.Quantity;
  * @module
  *
  * @see Units#PSU
+ * @see AngularVelocity
  */
 public interface Salinity extends Quantity<Salinity> {
 }
diff --git a/core/sis-utility/src/main/java/org/apache/sis/measure/UnitRegistry.java b/core/sis-utility/src/main/java/org/apache/sis/measure/UnitRegistry.java
index fa8a0f2..fb12216 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/measure/UnitRegistry.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/measure/UnitRegistry.java
@@ -37,7 +37,7 @@ import org.apache.sis.util.collection.WeakValueHashMap;
  * rather uses the static methods directly since we define all units in terms of SI.
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 0.8
+ * @version 1.0
  * @since   0.8
  * @module
  */
@@ -121,19 +121,18 @@ final class UnitRegistry implements SystemOfUnits, Serializable {
      */
     static <Q extends Quantity<Q>> SystemUnit<Q> init(final SystemUnit<Q>
unit) {
         assert !Units.initialized : unit;        // This assertion happens during Units initialization,
but it is okay.
+        final String symbol = unit.getSymbol();
         int existed;
-        existed  = (HARD_CODED.put(unit.dimension,   unit) == null) ? 0 : 1;
-        existed |= (HARD_CODED.put(unit.quantity,    unit) == null) ? 0 : 2;
-        existed |= (HARD_CODED.put(unit.getSymbol(), unit) == null) ? 0 : 4;
-        if (unit.epsg != 0) {
-            existed |= (HARD_CODED.put(unit.epsg, unit) == null) ? 0 : 8;
-        }
+        /* Unconditional */ existed  = (HARD_CODED.put(unit.dimension, unit) == null) ? 0
: 1;
+        /* Unconditional */ existed |= (HARD_CODED.put(unit.quantity,  unit) == null) ? 0
: 2;
+        if (symbol != null) existed |= (HARD_CODED.put(symbol,         unit) == null) ? 0
: 4;
+        if (unit.epsg != 0) existed |= (HARD_CODED.put(unit.epsg,      unit) == null) ? 0
: 8;
         /*
          * Key collision on dimension and quantity tolerated for dimensionless units only,
with an
          * an exception for "candela" because "lumen" is candela divided by a dimensionless
unit.
          * Another exception is "Hz" because it come after rad/s, which has the same dimension.
          */
-        assert filter(existed, unit) == 0 : unit;
+        assert filter(existed, unit, symbol) == 0 : unit;
         return unit;
     }
 
@@ -141,11 +140,10 @@ final class UnitRegistry implements SystemOfUnits, Serializable {
      * Clears the {@code existed} bits for the cases where we allow dimension or quantity
type collisions.
      * This method is invoked for assertions only.
      */
-    private static int filter(int existed, final SystemUnit<?> unit) {
-        final String s = unit.getSymbol();
+    private static int filter(int existed, final SystemUnit<?> unit, final String s)
{
         if (unit.dimension.isDimensionless()) existed &= ~(1 | 2);      // Accepts dimension
and quantity collisions.
-        if (s.equals("cd") || s.equals("Hz")) existed &= ~(1    );      // Accepts dimension
collisions only;
-        return s.isEmpty() ? 0 : existed;
+        if ("cd".equals(s) || "Hz".equals(s)) existed &= ~(1    );      // Accepts dimension
collisions only;
+        return (s == null) || s.isEmpty() ? 0 : existed;
     }
 
     /**
diff --git a/core/sis-utility/src/main/java/org/apache/sis/measure/Units.java b/core/sis-utility/src/main/java/org/apache/sis/measure/Units.java
index 0ede16b..a29c81e 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/measure/Units.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/measure/Units.java
@@ -1217,11 +1217,13 @@ public final class Units extends Static {
         /*
          * All Unit<Dimensionless>.
          */
-        PERCENT = add(one, centi,                                                    "%",
    OTHER, (short) 0);
-        PPM     = add(one, micro,                                                    "ppm",
  OTHER, (short) 9202);
-        PSU     = add(Salinity.class,      null,                      dimensionless, "psu",
  OTHER, (short) 0);
+        final SystemUnit<Salinity> sal;
         SIGMA   = add(Dimensionless.class, Scalar.Dimensionless::new, dimensionless, "sigma",
OTHER, (short) 0);
         PIXEL   = add(Dimensionless.class, Scalar.Dimensionless::new, dimensionless, "px",
   OTHER, (short) 0);
+        sal     = add(Salinity.class,      null,                      dimensionless, null,
   OTHER, (short) 0);
+        PSU     = add(sal, milli,                                                    "psu",
  OTHER, (short) 0);
+        PERCENT = add(one, centi,                                                    "%",
    OTHER, (short) 0);
+        PPM     = add(one, micro,                                                    "ppm",
  OTHER, (short) 9202);
         UNITY   = UnitRegistry.init(one);  // Must be last in order to take precedence over
all other units associated to UnitDimension.NONE.
 
         UnitRegistry.alias(UNITY,       Short.valueOf((short) 9203));
@@ -1244,6 +1246,13 @@ public final class Units extends Static {
     /**
      * Invoked by {@code Units} static class initializer for registering SI base and derived
units.
      * This method shall be invoked in a single thread by the {@code Units} class initializer
only.
+     *
+     * @param  quantity   the type of quantity that uses this unit (should not be null).
+     * @param  factory    the factory to use for creating quantities, or {@code null} if
none.
+     * @param  dimension  the unit dimension.
+     * @param  symbol     the unit symbol, or {@code null} if this unit has no specific symbol.
+     * @param  scope      {@link UnitRegistry#SI}, {@link UnitRegistry#ACCEPTED}, other constants
or 0 if unknown.
+     * @param  epsg       the EPSG code, or 0 if this unit has no EPSG code.
      */
     private static <Q extends Quantity<Q>> SystemUnit<Q> add(Class<Q>
quantity, ScalarFactory<Q> factory,
             UnitDimension dimension, String symbol, byte scope, short epsg)
diff --git a/core/sis-utility/src/main/java/org/apache/sis/measure/package-info.java b/core/sis-utility/src/main/java/org/apache/sis/measure/package-info.java
index b983562..90c3631 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/measure/package-info.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/measure/package-info.java
@@ -63,7 +63,7 @@
  * </ul>
  *
  * @author  Martin Desruisseaux (MPO, IRD, Geomatys)
- * @version 0.8
+ * @version 1.0
  * @since   0.3
  * @module
  */
diff --git a/core/sis-utility/src/test/java/org/apache/sis/measure/UnitsTest.java b/core/sis-utility/src/test/java/org/apache/sis/measure/UnitsTest.java
index 833527a..fe396d1 100644
--- a/core/sis-utility/src/test/java/org/apache/sis/measure/UnitsTest.java
+++ b/core/sis-utility/src/test/java/org/apache/sis/measure/UnitsTest.java
@@ -29,6 +29,7 @@ import javax.measure.quantity.Speed;
 import javax.measure.quantity.Temperature;
 import javax.measure.quantity.Time;
 import javax.measure.quantity.Volume;
+import javax.measure.IncommensurableException;
 import org.apache.sis.test.DependsOn;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
@@ -42,7 +43,7 @@ import static org.apache.sis.test.Assert.*;
  * Test conversions using the units declared in {@link Units}.
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 0.8
+ * @version 1.0
  * @since   0.3
  * @module
  */
@@ -196,6 +197,19 @@ public final strictfp class UnitsTest extends TestCase {
     }
 
     /**
+     * Verifies the conversion factory of {@link Units#PSU}.
+     *
+     * @see <a href="https://issues.apache.org/jira/browse/SIS-413">SIS-413</a>
+     *
+     * @throws IncommensurableException if the conversion can not be applied.
+     */
+    @Test
+    public void testSalinityConversionFactor() throws IncommensurableException {
+        assertEquals(0.001, PSU.getConverterToAny(UNITY)  .convert(1), STRICT);
+        assertEquals(0.1,   PSU.getConverterToAny(PERCENT).convert(1), STRICT);
+    }
+
+    /**
      * Tests getting a unit for a given quantity type.
      */
     @Test


Mime
View raw message