sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1765864 - in /sis/branches/JDK8/core/sis-utility/src: main/java/org/apache/sis/measure/ test/java/org/apache/sis/measure/
Date Thu, 20 Oct 2016 19:12:59 GMT
Author: desruisseaux
Date: Thu Oct 20 19:12:59 2016
New Revision: 1765864

URL: http://svn.apache.org/viewvc?rev=1765864&view=rev
Log:
UnitsMap is no longer needed now that we handle EPSG codes directly in our Unit implementation.

Removed:
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitsMap.java
Modified:
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/AbstractUnit.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitFormat.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitRegistry.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Units.java
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/UnitsTest.java

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/AbstractUnit.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/AbstractUnit.java?rev=1765864&r1=1765863&r2=1765864&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/AbstractUnit.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/AbstractUnit.java
[UTF-8] Thu Oct 20 19:12:59 2016
@@ -256,7 +256,7 @@ abstract class AbstractUnit<Q extends Qu
      * Invoked on deserialization for returning a unique instance of {@code AbstractUnit}
if possible.
      */
     final Object readResolve() throws ObjectStreamException {
-        if (Units.initialized) {                // Force Units class initialization.
+        if (symbol != null && Units.initialized) {              // Force Units class
initialization.
             final Unit<?> exising = (Unit<?>) UnitRegistry.putIfAbsent(symbol,
this);
             if (equals(exising)) {
                 return exising;

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitFormat.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitFormat.java?rev=1765864&r1=1765863&r2=1765864&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitFormat.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitFormat.java
[UTF-8] Thu Oct 20 19:12:59 2016
@@ -453,7 +453,7 @@ public class UnitFormat extends Format i
         if (unit.isCompatible(Units.HERTZ) && !uom.equals("Bd")) {
             return Units.HERTZ;
         }
-        return UnitsMap.canonicalize(unit);
+        return unit;
     }
 
     /**

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitRegistry.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitRegistry.java?rev=1765864&r1=1765863&r2=1765864&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitRegistry.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitRegistry.java
[UTF-8] Thu Oct 20 19:12:59 2016
@@ -21,6 +21,7 @@ import java.util.Map;
 import java.util.Set;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.io.Serializable;
 import javax.measure.Unit;
 import javax.measure.Quantity;
 import javax.measure.Dimension;
@@ -40,7 +41,12 @@ import org.apache.sis.util.collection.We
  * @version 0.8
  * @module
  */
-final class UnitRegistry implements SystemOfUnits {
+final class UnitRegistry implements SystemOfUnits, Serializable {
+    /**
+     * For cross-version compatibility.
+     */
+    private static final long serialVersionUID = -84557361079506390L;
+
     /**
      * All {@link UnitDimension}, {@link SystemUnit} or {@link ConventionalUnit} that are
hard-coded in Apache SIS.
      * This map is populated by {@link Units} static initializer and shall not be modified
after initialization,
@@ -56,7 +62,7 @@ final class UnitRegistry implements Syst
      *   <tr><td>{@link Short}</td>                       <td>{@link
AbstractUnit}</td>  <td>Key is the EPSG code.</td></tr>
      * </table>
      */
-    private static final Map<Object,Object> HARD_CODED = new HashMap<>(128);
+    private static final Map<Object,Object> HARD_CODED = new HashMap<>(256);
 
     /**
      * Units defined by the user. Accesses to this map implies synchronization.
@@ -91,7 +97,10 @@ final class UnitRegistry implements Syst
         existed  = HARD_CODED.put(unit.dimension,   unit) != null;
         existed |= HARD_CODED.put(unit.quantity,    unit) != null;
         existed |= HARD_CODED.put(unit.getSymbol(), unit) != null;
-        assert !existed || unit.dimension.components.isEmpty() : unit;   // Key collision
tolerated for dimensionless unit only.
+        if (unit.epsg != 0) {
+            existed |= HARD_CODED.put(unit.epsg, unit) != null;
+        }
+        assert !existed || unit.dimension.isDimensionless() : unit;   // Key collision tolerated
for dimensionless unit only.
         return unit;
     }
 
@@ -101,10 +110,23 @@ final class UnitRegistry implements Syst
      */
     static <Q extends Quantity<Q>> ConventionalUnit<Q> init(final ConventionalUnit<Q>
unit) {
         assert !Units.initialized : unit;        // This assertion happens during Units initialization,
but it is okay.
-        if (HARD_CODED.put(unit.getSymbol(), unit) != null) {
-            throw new AssertionError(unit);      // Shall not map the same unit twice.
+        if (HARD_CODED.put(unit.getSymbol(), unit) == null) {
+            if (unit.epsg == 0 || HARD_CODED.put(unit.epsg, unit) == null) {
+                return unit;
+            }
+        }
+        throw new AssertionError(unit);      // Shall not map the same unit twice.
+    }
+
+    /**
+     * Adds an alias for the given unit. The given alias shall be either an instance of {@link
String}
+     * (for a symbol alias) or an instance of {@link Short} (for an EPSG code alias).
+     */
+    static void alias(final Unit<?> unit, final Comparable<?> alias) {
+        assert !Units.initialized : unit;        // This assertion happens during Units initialization,
but it is okay.
+        if (HARD_CODED.put(alias, unit) != null) {
+            throw new AssertionError(unit);      // Shall not map the same alias twice.
         }
-        return unit;
     }
 
     /**
@@ -136,7 +158,7 @@ final class UnitRegistry implements Syst
     /**
      * The value returned by {@link #getUnits()}, created when first needed.
      */
-    private static Set<Unit<?>> units;
+    private transient Set<Unit<?>> units;
 
     /**
      * Creates a new unit system.
@@ -170,7 +192,7 @@ final class UnitRegistry implements Syst
     @SuppressWarnings("ReturnOfCollectionOrArrayField")
     public Set<Unit<?>> getUnits() {
         if (Units.initialized) {                    // Force Units class initialization.
-            synchronized (UnitRegistry.class) {
+            synchronized (this) {
                 if (units == null) {
                     units = new HashSet<>();
                     for (final Object value : HARD_CODED.values()) {

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=1765864&r1=1765863&r2=1765864&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 19:12:59 2016
@@ -678,7 +678,7 @@ public final class Units extends Static
          */
         RADIAN      = rad;
         GRAD        = add(rad, LinearConverter.scale(Math.PI, 200),    "grad", (short) 9105);
-        DEGREE      = add(rad, LinearConverter.scale(Math.PI, 180),       "°", (short) 9102);
+        DEGREE      = add(rad, LinearConverter.scale(Math.PI, 180),       "°", Constants.EPSG_PARAM_DEGREES);
         ARC_MINUTE  = add(rad, LinearConverter.scale(Math.PI, 180*60),    "′", (short)
9103);
         ARC_SECOND  = add(rad, LinearConverter.scale(Math.PI, 180*60*60), "″", (short)
9104);
         MICRORADIAN = add(rad, micro, "µrad", (short) 9109);
@@ -732,6 +732,13 @@ public final class Units extends Static
         PIXEL   = add(Dimensionless.class, dimensionless, "px",    (short) 0);
         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));
+        UnitRegistry.alias(DEGREE,      Short.valueOf(Constants.EPSG_AXIS_DEGREES));
+        UnitRegistry.alias(ARC_MINUTE,  "'");
+        UnitRegistry.alias(ARC_SECOND, "\"");
+        UnitRegistry.alias(KELVIN,      "K");       // Ordinary "K" letter (not the dedicated
Unicode character).
+        UnitRegistry.alias(CELSIUS,    "°C");
+
         initialized = true;
     }
 
@@ -972,7 +979,7 @@ public final class Units extends Static
         if (abs(factor - 1) > EPS) {
             unit = unit.multiply(factor);
         }
-        return UnitsMap.canonicalize(unit);
+        return unit;
     }
 
     /**
@@ -1121,31 +1128,7 @@ public final class Units extends Static
      * @see org.apache.sis.referencing.factory.GeodeticAuthorityFactory#createUnit(String)
      */
     public static Unit<?> valueOfEPSG(final int code) {
-        switch (code) {
-            case Constants.EPSG_PARAM_DEGREES:  // Fall through
-            case Constants.EPSG_AXIS_DEGREES:   return DEGREE;
-            case Constants.EPSG_METRE:          return METRE;
-
-            case 1029: return TROPICAL_YEAR;
-            case 1040: return SECOND;
-            case 9002: return FOOT;
-            case 9003: return US_SURVEY_FOOT;
-            case 9030: return NAUTICAL_MILE;
-            case 9036: return KILOMETRE;
-            case 9101: return RADIAN;
-            case 9103: return ARC_MINUTE;
-            case 9104: return ARC_SECOND;
-            case 9105: return GRAD;
-            case 9109: return MICRORADIAN;
-            case 9107: // Fall through
-            case 9108: return SexagesimalConverter.DMS_SCALED;
-            case 9110: return SexagesimalConverter.DMS;
-            case 9111: return SexagesimalConverter.DM;
-            case 9203: // Fall through
-            case 9201: return UNITY;
-            case 9202: return PPM;
-            default:   return null;
-        }
+        return (code > 0 && code <= Short.MAX_VALUE) ? (Unit<?>) UnitRegistry.get((short)
code) : null;
     }
 
     /**
@@ -1167,11 +1150,22 @@ public final class Units extends Static
      *
      * @since 0.4
      */
-    public static Integer getEpsgCode(final Unit<?> unit, final boolean inAxis) {
-        Integer code = UnitsMap.EPSG_CODES.get(unit);
-        if (inAxis && code != null && code == Constants.EPSG_PARAM_DEGREES)
{
-            code = UnitsMap.EPSG_AXIS_DEGREES;
+    public static Integer getEpsgCode(Unit<?> unit, final boolean inAxis) {
+        if (unit != null) {
+            if (!(unit instanceof AbstractUnit<?>)) {
+                unit = get(unit.getSymbol());
+                if (!(unit instanceof AbstractUnit<?>)) {
+                    return null;
+                }
+            }
+            short code = ((AbstractUnit<?>) unit).epsg;
+            if (code != 0) {
+                if (inAxis && code == Constants.EPSG_PARAM_DEGREES) {
+                    code = Constants.EPSG_AXIS_DEGREES;
+                }
+                return Integer.valueOf(code);
+            }
         }
-        return code;
+        return null;
     }
 }

Modified: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/UnitsTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/UnitsTest.java?rev=1765864&r1=1765863&r2=1765864&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/UnitsTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/UnitsTest.java
[UTF-8] Thu Oct 20 19:12:59 2016
@@ -41,12 +41,11 @@ import static org.apache.sis.test.Assert
 })
 public final strictfp class UnitsTest extends TestCase {
     /**
-     * Sanity check of {@link UnitsMap}. This test fail if at least one code in the
-     * {@link UnitsMap#EPSG_CODES} static initializer is invalid.
+     * Verifies that the {@link Units#initialized} flag has been set.
      */
     @Test
-    public void testUnitsMap() {
-        assertFalse(UnitsMap.EPSG_CODES.containsKey(null));
+    public void testInitialized() {
+        assertTrue(Units.initialized);
     }
 
     /**
@@ -266,6 +265,25 @@ public final strictfp class UnitsTest ex
         assertSame(DEGREE, valueOf("urn:ogc:def:uom:EPSG::9102"));
         assertSame(METRE,  valueOf("http://schemas.opengis.net/iso/19139/20070417/resources/uom/gmxUom.xml#xpointer(//*[@gml:id='m'])"));
         assertSame(METRE,  valueOf("gmxUom.xml#m"));
+
+        assertSame(TROPICAL_YEAR,   valueOfEPSG(1029));
+        assertSame(SECOND,          valueOfEPSG(1040));
+        assertSame(FOOT,            valueOfEPSG(9002));
+        assertSame(US_SURVEY_FOOT,  valueOfEPSG(9003));
+        assertSame(NAUTICAL_MILE,   valueOfEPSG(9030));
+        assertSame(KILOMETRE,       valueOfEPSG(9036));
+        assertSame(RADIAN,          valueOfEPSG(9101));
+        assertSame(ARC_MINUTE,      valueOfEPSG(9103));
+        assertSame(ARC_SECOND,      valueOfEPSG(9104));
+        assertSame(GRAD,            valueOfEPSG(9105));
+        assertSame(MICRORADIAN,     valueOfEPSG(9109));
+        assertSame(SexagesimalConverter.DMS_SCALED, valueOfEPSG(9107));
+        assertSame(SexagesimalConverter.DMS_SCALED, valueOfEPSG(9108));
+        assertSame(SexagesimalConverter.DMS,        valueOfEPSG(9110));
+        assertSame(SexagesimalConverter.DM,         valueOfEPSG(9111));
+        assertSame(UNITY,           valueOfEPSG(9203));
+        assertSame(UNITY,           valueOfEPSG(9201));
+        assertSame(PPM,             valueOfEPSG(9202));
     }
 
     /**
@@ -273,10 +291,25 @@ public final strictfp class UnitsTest ex
      */
     @Test
     public void testGetEpsgCode() {
-        assertEquals(Integer.valueOf(9001), getEpsgCode(METRE,  false));
-        assertEquals(Integer.valueOf(9102), getEpsgCode(DEGREE, false));
-        assertEquals(Integer.valueOf(9122), getEpsgCode(DEGREE, true));
-        assertEquals(Integer.valueOf(9110), getEpsgCode(DMS,    false));
-        assertEquals(Integer.valueOf(9110), getEpsgCode(DMS,    true));
+        assertEquals(Integer.valueOf(9001), getEpsgCode(METRE,          false));
+        assertEquals(Integer.valueOf(9102), getEpsgCode(DEGREE,         false));
+        assertEquals(Integer.valueOf(9122), getEpsgCode(DEGREE,         true));
+        assertEquals(Integer.valueOf(9110), getEpsgCode(DMS,            false));
+        assertEquals(Integer.valueOf(9110), getEpsgCode(DMS,            true));
+        assertEquals(Integer.valueOf(9108), getEpsgCode(DMS_SCALED,     false));
+        assertEquals(Integer.valueOf(9111), getEpsgCode(DM,             false));
+        assertEquals(Integer.valueOf(1029), getEpsgCode(TROPICAL_YEAR,  false));
+        assertEquals(Integer.valueOf(1040), getEpsgCode(SECOND,         false));
+        assertEquals(Integer.valueOf(9002), getEpsgCode(FOOT,           false));
+        assertEquals(Integer.valueOf(9003), getEpsgCode(US_SURVEY_FOOT, false));
+        assertEquals(Integer.valueOf(9030), getEpsgCode(NAUTICAL_MILE,  false));
+        assertEquals(Integer.valueOf(9036), getEpsgCode(KILOMETRE,      false));
+        assertEquals(Integer.valueOf(9101), getEpsgCode(RADIAN,         false));
+        assertEquals(Integer.valueOf(9103), getEpsgCode(ARC_MINUTE,     false));
+        assertEquals(Integer.valueOf(9104), getEpsgCode(ARC_SECOND,     false));
+        assertEquals(Integer.valueOf(9105), getEpsgCode(GRAD,           false));
+        assertEquals(Integer.valueOf(9109), getEpsgCode(MICRORADIAN,    false));
+        assertEquals(Integer.valueOf(9201), getEpsgCode(UNITY,          false));
+        assertEquals(Integer.valueOf(9202), getEpsgCode(PPM,            false));
     }
 }



Mime
View raw message