sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1677787 - in /sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs: AbstractDerivedCRS.java DefaultDerivedCRS.java DefaultProjectedCRS.java
Date Tue, 05 May 2015 11:29:39 GMT
Author: desruisseaux
Date: Tue May  5 11:29:38 2015
New Revision: 1677787

URL: http://svn.apache.org/r1677787
Log:
Referencing: make AbstractDerivedCRS more type-safe.

Modified:
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/AbstractDerivedCRS.java
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultDerivedCRS.java
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultProjectedCRS.java

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/AbstractDerivedCRS.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/AbstractDerivedCRS.java?rev=1677787&r1=1677786&r2=1677787&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/AbstractDerivedCRS.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/AbstractDerivedCRS.java
[UTF-8] Tue May  5 11:29:38 2015
@@ -41,6 +41,8 @@ import static org.apache.sis.util.Utilit
  * {@linkplain org.apache.sis.referencing.operation.DefaultConversion conversion} from another
CRS
  * (not by a {@linkplain org.apache.sis.referencing.datum.AbstractDatum datum}).
  *
+ * @param <C> The conversion type, either {@code Conversion} or {@code Projection}.
+ *
  * @author  Martin Desruisseaux (IRD, Geomatys)
  * @since   0.6
  * @version 0.6
@@ -51,7 +53,7 @@ import static org.apache.sis.util.Utilit
 @XmlSeeAlso({
     DefaultProjectedCRS.class
 })
-abstract class AbstractDerivedCRS extends AbstractCRS implements GeneralDerivedCRS {
+abstract class AbstractDerivedCRS<C extends Conversion> extends AbstractCRS implements
GeneralDerivedCRS {
     /**
      * Serial number for inter-operability with different versions.
      */
@@ -62,7 +64,7 @@ abstract class AbstractDerivedCRS extend
      * The base CRS of this {@code GeneralDerivedCRS} is {@link Conversion#getSourceCRS()}.
      */
     @XmlElement(name = "conversion", required = true)
-    private final Conversion conversionFromBase;
+    private final C conversionFromBase;
 
     /**
      * Constructs a new object in which every attributes are set to a null value.
@@ -79,29 +81,29 @@ abstract class AbstractDerivedCRS extend
      * {@linkplain AbstractCRS#AbstractCRS(Map, CoordinateSystem) super-class constructor}.
      *
      * @param  properties The properties to be given to the new derived CRS object.
-     * @param  baseCRS Coordinate reference system to base the derived CRS on.
-     * @param  conversionFromBase The conversion from the base CRS to this derived CRS.
-     * @param  derivedCS The coordinate system for the derived CRS. The number of axes
+     * @param  baseCRS    Coordinate reference system to base the derived CRS on.
+     * @param  conversion The conversion from the base CRS to this derived CRS.
+     * @param  derivedCS  The coordinate system for the derived CRS. The number of axes
      *         must match the target dimension of the {@code baseToDerived} transform.
      * @throws MismatchedDimensionException if the source and target dimension of {@code
baseToDerived}
      *         do not match the dimension of {@code base} and {@code derivedCS} respectively.
      */
     AbstractDerivedCRS(final Map<String,?>    properties,
+                       final Class<C>         baseType,
                        final SingleCRS        baseCRS,
-                       final Conversion       conversionFromBase,
-                       final CoordinateSystem derivedCS,
-                       final Class<? extends Conversion> type)
+                       final Conversion       conversion,
+                       final CoordinateSystem derivedCS)
             throws MismatchedDimensionException
     {
         super(properties, derivedCS);
         ArgumentChecks.ensureNonNull("baseCRS", baseCRS);
-        ArgumentChecks.ensureNonNull("conversionFromBase", conversionFromBase);
-        final MathTransform baseToDerived = conversionFromBase.getMathTransform();
+        ArgumentChecks.ensureNonNull("conversionFromBase", conversion);   // "conversionFromBase"
is the name used by subclass constructors.
+        final MathTransform baseToDerived = conversion.getMathTransform();
         if (baseToDerived != null) {
             ArgumentChecks.ensureDimensionMatches("baseCRS",   baseToDerived.getSourceDimensions(),
baseCRS);
             ArgumentChecks.ensureDimensionMatches("derivedCS", baseToDerived.getTargetDimensions(),
derivedCS);
         }
-        this.conversionFromBase = DefaultConversion.castOrCopy(conversionFromBase).specialize(type,
baseCRS, this);
+        conversionFromBase = DefaultConversion.castOrCopy(conversion).specialize(baseType,
baseCRS, this);
     }
 
     /**
@@ -113,22 +115,17 @@ abstract class AbstractDerivedCRS extend
      *
      * @param crs The coordinate reference system to copy.
      */
-    protected AbstractDerivedCRS(final GeneralDerivedCRS crs) {
+    AbstractDerivedCRS(final GeneralDerivedCRS crs, final Class<C> baseType) {
         super(crs);
-        conversionFromBase = crs.getConversionFromBase();
+        conversionFromBase = DefaultConversion.castOrCopy(crs.getConversionFromBase())
+                .specialize(baseType, crs.getBaseCRS(), this);
     }
 
     /**
      * Returns the GeoAPI interface implemented by this class.
-     * The default implementation returns {@code GeneralDerivedCRS.class}.
-     * Subclasses implementing a more specific GeoAPI interface shall override this method.
-     *
-     * @return The coordinate reference system interface implemented by this class.
      */
     @Override
-    public Class<? extends GeneralDerivedCRS> getInterface() {
-        return GeneralDerivedCRS.class;
-    }
+    public abstract Class<? extends GeneralDerivedCRS> getInterface();
 
     /**
      * Returns the datum of the {@linkplain #getBaseCRS() base CRS}.
@@ -136,19 +133,7 @@ abstract class AbstractDerivedCRS extend
      * @return The datum of the base CRS.
      */
     @Override
-    public Datum getDatum() {
-        return getBaseCRS().getDatum();
-    }
-
-    /**
-     * Returns the base coordinate reference system.
-     *
-     * @return The base coordinate reference system.
-     */
-    @Override
-    public SingleCRS getBaseCRS() {
-        return (SingleCRS) getConversionFromBase().getSourceCRS();
-    }
+    public abstract Datum getDatum();
 
     /**
      * Returns the conversion from the {@linkplain #getBaseCRS() base CRS} to this CRS.
@@ -156,7 +141,7 @@ abstract class AbstractDerivedCRS extend
      * @return The conversion to this CRS.
      */
     @Override
-    public Conversion getConversionFromBase() {
+    public C getConversionFromBase() {
         return conversionFromBase;
     }
 
@@ -171,9 +156,6 @@ abstract class AbstractDerivedCRS extend
      */
     @Override
     public boolean equals(final Object object, final ComparisonMode mode) {
-        if (object == this) {
-            return true; // Slight optimization.
-        }
         if (super.equals(object, mode)) {
             final boolean strict = (mode == ComparisonMode.STRICT);
             /*

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultDerivedCRS.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultDerivedCRS.java?rev=1677787&r1=1677786&r2=1677787&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultDerivedCRS.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultDerivedCRS.java
[UTF-8] Tue May  5 11:29:38 2015
@@ -31,6 +31,7 @@ import org.apache.sis.referencing.operat
 import org.apache.sis.internal.referencing.WKTUtilities;
 import org.apache.sis.io.wkt.FormattableObject;
 import org.apache.sis.io.wkt.Formatter;
+import org.apache.sis.util.ComparisonMode;
 
 
 /**
@@ -53,7 +54,7 @@ import org.apache.sis.io.wkt.Formatter;
  * @module
  */
 @XmlTransient   // TODO: GML not yet investigated
-public class DefaultDerivedCRS extends AbstractDerivedCRS implements DerivedCRS {
+public class DefaultDerivedCRS extends AbstractDerivedCRS<Conversion> implements DerivedCRS
{
     /**
      * Serial number for inter-operability with different versions.
      */
@@ -126,7 +127,7 @@ public class DefaultDerivedCRS extends A
                              final CoordinateSystem derivedCS)
             throws MismatchedDimensionException
     {
-        super(properties, baseCRS, conversionFromBase, derivedCS, Conversion.class);
+        super(properties, Conversion.class, baseCRS, conversionFromBase, derivedCS);
     }
 
     /**
@@ -141,7 +142,7 @@ public class DefaultDerivedCRS extends A
      * @see #castOrCopy(DerivedCRS)
      */
     protected DefaultDerivedCRS(final DerivedCRS crs) {
-        super(crs);
+        super(crs, Conversion.class);
     }
 
     /**
@@ -182,18 +183,20 @@ public class DefaultDerivedCRS extends A
      */
     @Override
     public Datum getDatum() {
-        return super.getDatum();
+        return getBaseCRS().getDatum();
     }
 
     /**
-     * Returns the {@linkplain org.apache.sis.referencing.operation.DefaultConversion#getSourceCRS()
source}
+     * Returns the CRS on which the conversion is applied.
+     * This CRS defines the {@linkplain #getDatum() datum} of this CRS and (at least implicitly)
+     * the {@linkplain org.apache.sis.referencing.operation.DefaultConversion#getSourceCRS()
source}
      * of the {@linkplain #getConversionFromBase() conversion from base}.
      *
      * @return The base coordinate reference system.
      */
     @Override
     public SingleCRS getBaseCRS() {
-        return super.getBaseCRS();
+        return (SingleCRS) super.getConversionFromBase().getSourceCRS();
     }
 
     /**
@@ -202,7 +205,7 @@ public class DefaultDerivedCRS extends A
      *
      * <ul>
      *   <li>The conversion {@linkplain org.apache.sis.referencing.operation.DefaultConversion#getSourceCRS()
-     *       source CRS} defines the {@linkplain #getBaseCRS() base CRS} of {@code this}
CRS.</li>
+     *       source CRS} is the {@linkplain #getBaseCRS() base CRS} of {@code this} CRS.</li>
      *   <li>The conversion {@linkplain org.apache.sis.referencing.operation.DefaultConversion#getTargetCRS()
      *       target CRS} is {@code this} CRS.
      * </ul>
@@ -218,6 +221,30 @@ public class DefaultDerivedCRS extends A
     }
 
     /**
+     * Compares this coordinate reference system with the specified object for equality.
+     *
+     * @param  object The object to compare to {@code this}.
+     * @param  mode {@link ComparisonMode#STRICT STRICT} for performing a strict comparison,
or
+     *         {@link ComparisonMode#IGNORE_METADATA IGNORE_METADATA} for comparing only
properties
+     *         relevant to coordinate transformations.
+     * @return {@code true} if both objects are equal.
+     */
+    @Override
+    public boolean equals(final Object object, final ComparisonMode mode) {
+        return (object == this) || super.equals(object, mode);
+    }
+
+    /**
+     * {@inheritDoc}
+     *
+     * @return {@inheritDoc}
+     */
+    @Override
+    protected long computeHashCode() {
+        return super.computeHashCode();
+    }
+
+    /**
      * Formats the inner part of the <cite>Well Known Text</cite> (WKT) representation
of this CRS.
      *
      * @return {@code "Fitted_CS"} (WKT 1) or a type-dependent keyword (WKT 2).
@@ -226,13 +253,13 @@ public class DefaultDerivedCRS extends A
     protected String formatTo(final Formatter formatter) {
         WKTUtilities.appendName(this, formatter, null);
         final boolean isWKT1 = (formatter.getConvention().majorVersion() == 1);
-        final Conversion conversionFromBase = getConversionFromBase();  // Gives to users
a chance to override.
+        final Conversion conversion = getConversionFromBase();  // Gives to users a chance
to override.
         /*
          * Both WKT 1 and WKT 2 format the base CRS. But WKT 1 formats the MathTransform
before the base CRS,
          * while WKT 2 formats the conversion method and parameter values after the base
CRS.
          */
         if (isWKT1) {
-            MathTransform inverse = conversionFromBase.getMathTransform();
+            MathTransform inverse = conversion.getMathTransform();
             try {
                 inverse = inverse.inverse();
             } catch (NoninvertibleTransformException exception) {
@@ -247,37 +274,22 @@ public class DefaultDerivedCRS extends A
         if (isWKT1) {
             return "Fitted_CS";
         } else {
-            formatter.append(new Parameters(this));    // Format inside a "DefiningConversion"
element.
+            formatter.append(new FormattableObject() {     // Format inside a "DefiningConversion"
element.
+                @Override protected String formatTo(final Formatter formatter) {
+                    WKTUtilities.appendName(conversion, formatter, null);
+                    formatter.newLine();
+                    formatter.append(DefaultOperationMethod.castOrCopy(conversion.getMethod()));
+                    formatter.newLine();
+                    for (final GeneralParameterValue param : conversion.getParameterValues().values())
{
+                        WKTUtilities.append(param, formatter);
+                    }
+                    return "DerivingConversion";
+                }
+            });
             if (!isBaseCRS(formatter)) {
                 formatCS(formatter, getCoordinateSystem(), isWKT1);
             }
             return "EngineeringCRS"; // TODO: may be GeodeticCRS, VerticalCRS, etc.
         }
     }
-
-    /**
-     * Temporary object for formatting the conversion method and parameters inside a
-     * a {@code DerivingConversion} element. This is used in WKT 2 formatting only.
-     */
-    private static final class Parameters extends FormattableObject {
-        /** The conversion which specify the operation method and parameters. */
-        private final Conversion conversion;
-
-        /** Creates a new temporary {@code DerivingConversion} elements for the parameters
of the given CRS. */
-        Parameters(final AbstractDerivedCRS crs) {
-            conversion = crs.getConversionFromBase();
-        }
-
-        /** Formats this {@code Conversion} element. */
-        @Override protected String formatTo(final Formatter formatter) {
-            WKTUtilities.appendName(conversion, formatter, null);
-            formatter.newLine();
-            formatter.append(DefaultOperationMethod.castOrCopy(conversion.getMethod()));
-            formatter.newLine();
-            for (final GeneralParameterValue param : conversion.getParameterValues().values())
{
-                WKTUtilities.append(param, formatter);
-            }
-            return "DerivingConversion";
-        }
-    }
 }

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultProjectedCRS.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultProjectedCRS.java?rev=1677787&r1=1677786&r2=1677787&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultProjectedCRS.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/crs/DefaultProjectedCRS.java
[UTF-8] Tue May  5 11:29:38 2015
@@ -43,6 +43,7 @@ import org.apache.sis.internal.util.Cons
 import org.apache.sis.io.wkt.FormattableObject;
 import org.apache.sis.io.wkt.Formatter;
 import org.apache.sis.io.wkt.Convention;
+import org.apache.sis.util.ComparisonMode;
 
 import static org.apache.sis.internal.referencing.WKTUtilities.toFormattable;
 
@@ -72,7 +73,7 @@ import static org.apache.sis.internal.re
     "coordinateSystem"
 })
 @XmlRootElement(name = "ProjectedCRS")
-public class DefaultProjectedCRS extends AbstractDerivedCRS implements ProjectedCRS {
+public class DefaultProjectedCRS extends AbstractDerivedCRS<Projection> implements
ProjectedCRS {
     /**
      * Serial number for inter-operability with different versions.
      */
@@ -145,7 +146,7 @@ public class DefaultProjectedCRS extends
                                final CartesianCS   derivedCS)
             throws MismatchedDimensionException
     {
-        super(properties, baseCRS, conversionFromBase, derivedCS, Projection.class);
+        super(properties, Projection.class, baseCRS, conversionFromBase, derivedCS);
     }
 
     /**
@@ -160,7 +161,7 @@ public class DefaultProjectedCRS extends
      * @see #castOrCopy(ProjectedCRS)
      */
     protected DefaultProjectedCRS(final ProjectedCRS crs) {
-        super(crs);
+        super(crs, Projection.class);
     }
 
     /**
@@ -205,7 +206,9 @@ public class DefaultProjectedCRS extends
     }
 
     /**
-     * Returns the {@linkplain org.apache.sis.referencing.operation.DefaultConversion#getSourceCRS()
source}
+     * Returns the geographic CRS on which the map projection is applied.
+     * This CRS defines the {@linkplain #getDatum() datum} of this CRS and (at least implicitly)
+     * the {@linkplain org.apache.sis.referencing.operation.DefaultConversion#getSourceCRS()
source}
      * of the {@linkplain #getConversionFromBase() conversion from base}.
      *
      * @return The base coordinate reference system, which must be geographic.
@@ -213,7 +216,7 @@ public class DefaultProjectedCRS extends
     @Override
     @XmlElement(name = "baseGeodeticCRS", required = true)  // Note: older GML version used
"baseGeographicCRS".
     public GeographicCRS getBaseCRS() {
-        return (GeographicCRS) getConversionFromBase().getSourceCRS();
+        return (GeographicCRS) super.getConversionFromBase().getSourceCRS();
     }
 
     /**
@@ -222,7 +225,7 @@ public class DefaultProjectedCRS extends
      *
      * <ul>
      *   <li>The conversion {@linkplain org.apache.sis.referencing.operation.DefaultConversion#getSourceCRS()
-     *       source CRS} defines the {@linkplain #getBaseCRS() base CRS} of {@code this}
CRS.</li>
+     *       source CRS} is the {@linkplain #getBaseCRS() base CRS} of {@code this} CRS.</li>
      *   <li>The conversion {@linkplain org.apache.sis.referencing.operation.DefaultConversion#getTargetCRS()
      *       target CRS} is {@code this} CRS.
      * </ul>
@@ -234,7 +237,7 @@ public class DefaultProjectedCRS extends
      */
     @Override
     public Projection getConversionFromBase() {
-        return (Projection) super.getConversionFromBase();
+        return super.getConversionFromBase();
     }
 
     /**
@@ -254,6 +257,30 @@ public class DefaultProjectedCRS extends
     }
 
     /**
+     * Compares this coordinate reference system with the specified object for equality.
+     *
+     * @param  object The object to compare to {@code this}.
+     * @param  mode {@link ComparisonMode#STRICT STRICT} for performing a strict comparison,
or
+     *         {@link ComparisonMode#IGNORE_METADATA IGNORE_METADATA} for comparing only
properties
+     *         relevant to coordinate transformations.
+     * @return {@code true} if both objects are equal.
+     */
+    @Override
+    public boolean equals(final Object object, final ComparisonMode mode) {
+        return (object == this) || super.equals(object, mode);
+    }
+
+    /**
+     * {@inheritDoc}
+     *
+     * @return {@inheritDoc}
+     */
+    @Override
+    protected long computeHashCode() {
+        return super.computeHashCode();
+    }
+
+    /**
      * Formats the inner part of the <cite>Well Known Text</cite> (WKT) representation
of this CRS.
      *
      * <div class="note"><b>Example:</b> Well-Known Text (version 2)



Mime
View raw message