You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2019/02/14 15:11:33 UTC

[sis] 02/02: Remove methods from Decoder that were just redirections to Convention methods.

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

commit 5cea2e37ad3cb1c806b22624655d76204c3dc75c
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Thu Feb 14 16:05:49 2019 +0100

    Remove methods from Decoder that were just redirections to Convention methods.
---
 .../org/apache/sis/internal/netcdf/Convention.java | 28 +++++++++-----
 .../org/apache/sis/internal/netcdf/Decoder.java    | 45 +++++++---------------
 .../sis/internal/netcdf/impl/ChannelDecoder.java   |  4 +-
 .../apache/sis/internal/netcdf/impl/GridInfo.java  |  4 +-
 .../apache/sis/storage/netcdf/GridResource.java    |  4 +-
 .../apache/sis/storage/netcdf/MetadataReader.java  |  2 +-
 6 files changed, 39 insertions(+), 48 deletions(-)

diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Convention.java b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Convention.java
index df8f27c..a946200 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Convention.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Convention.java
@@ -44,13 +44,18 @@ import org.apache.sis.internal.referencing.LazySet;
  * @since 1.0
  * @module
  */
-public abstract class Convention {
+public class Convention {
     /**
      * All conventions found on the classpath.
      */
     private static final LazySet<Convention> AVAILABLES = new LazySet<>(Convention.class);
 
     /**
+     * The convention to use when no specific conventions were found.
+     */
+    private static final Convention DEFAULT = new Convention();
+
+    /**
      * For subclass constructors.
      */
     protected Convention() {
@@ -64,12 +69,12 @@ public abstract class Convention {
         Convention c;
         synchronized (AVAILABLES) {
             it = AVAILABLES.iterator();
-            if (!it.hasNext()) return null;
+            if (!it.hasNext()) return DEFAULT;
             c = it.next();
         }
         while (!c.isApplicableTo(decoder)) {
             synchronized (AVAILABLES) {
-                if (!it.hasNext()) return null;
+                if (!it.hasNext()) return DEFAULT;
                 c = it.next();
             }
         }
@@ -82,17 +87,19 @@ public abstract class Convention {
      * @param  decoder  the netCDF file to test.
      * @return {@code true} if this set of conventions can apply.
      */
-    protected abstract boolean isApplicableTo(Decoder decoder);
+    protected boolean isApplicableTo(final Decoder decoder) {
+        return false;
+    }
 
     /**
      * Returns the role of the given variable. In particular, this method shall return
      * {@link VariableRole#AXIS} if the given variable seems to be a coordinate system axis.
      *
-     * @param  variable  the variable for which to get the role.
-     * @return role of the given variable.
+     * @param  variable  the variable for which to get the role, or {@code null}.
+     * @return role of the given variable, or {@code null} if the given variable was null.
      */
-    protected VariableRole roleOf(final Variable variable) {
-        return variable.getRole();
+    public VariableRole roleOf(final Variable variable) {
+        return (variable != null) ? variable.getRole() : null;
     }
 
     /**
@@ -101,14 +108,15 @@ public abstract class Convention {
      * The data for a dimension are usually stored in a variable of the same name, but not always.
      * This method gives an opportunity for subclasses to select the axis variables using other criterion.
      * This happen for example if a netCDF file defines two grids for the same dimensions.
+     * The order in returned array will be the axis order in the Coordinate Reference System.
      *
      * <p>The default implementation returns {@code null}.</p>
      *
-     * @param  variable  the variable for which the list of axis variables are desired.
+     * @param  variable  the variable for which the list of axis variables are desired, in CRS order.
      * @return names of the variables containing axis values, or {@code null} if this
      *         method performs applies no special convention for the given variable.
      */
-    protected String[] namesOfAxisVariables(Variable variable) {
+    public String[] namesOfAxisVariables(Variable variable) {
         return null;
     }
 }
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Decoder.java b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Decoder.java
index 23e6f48..f2bb45e 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Decoder.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Decoder.java
@@ -59,7 +59,10 @@ public abstract class Decoder extends ReferencingFactoryContainer implements Clo
     public Path location;
 
     /**
-     * Customized conventions to apply in addition of netCDF conventions, or {@code null} if none.
+     * Conventions to apply in addition of netCDF conventions.
+     * Shall never be {@code null} after {@link #initialize()}.
+     *
+     * @see #convention()
      */
     private Convention convention;
 
@@ -122,6 +125,16 @@ public abstract class Decoder extends ReferencingFactoryContainer implements Clo
     }
 
     /**
+     * Returns information about modifications to apply to netCDF conventions in order to handle this netCDF file.
+     * Customized conventions are necessary when the variables and attributes in a netCDF file do not follow CF-conventions.
+     *
+     * @return conventions to apply.
+     */
+    public final Convention convention() {
+        return convention;
+    }
+
+    /**
      * Returns a filename for formatting error message and for information purpose.
      * The filename should not contain path, but may contain file extension.
      *
@@ -283,34 +296,4 @@ public abstract class Decoder extends ReferencingFactoryContainer implements Clo
      * @throws DataStoreException if a logical error occurred.
      */
     public abstract Grid[] getGrids() throws IOException, DataStoreException;
-
-    /**
-     * Returns the role of the given variable. In particular, this method shall return
-     * {@link VariableRole#AXIS} if the given variable seems to be a coordinate system axis.
-     *
-     * @param  variable  the variable for which to get the role, or {@code null}.
-     * @return role of the given variable, or {@code null} if the given variable was null.
-     */
-    public final VariableRole roleOf(final Variable variable) {
-        if (variable == null) {
-            return null;
-        }
-        if (convention != null) {
-            return convention.roleOf(variable);
-        }
-        return variable.getRole();
-    }
-
-    /**
-     * If there is some specialized convention for current file that mandate a different set of
-     * axes for the given variable, returns the name of the variables for those axes. Otherwise
-     * (i.e. if the file can be parsed as a standard CF-compliant file), returns {@code null}.
-     *
-     * @param  variable  the variable for which the list of axis variables are desired.
-     * @return names of the variables containing axis values, or {@code null} if this
-     *         method performs applies no special convention for the given variable.
-     */
-    protected final String[] namesOfAxisVariables(final Variable variable) {
-        return (convention != null) ? convention.namesOfAxisVariables(variable) : null;
-    }
 }
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java
index aebba26..16e7d4c 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java
@@ -899,7 +899,7 @@ public final class ChannelDecoder extends Decoder {
              */
             final Map<Dimension, List<VariableInfo>> dimToAxes = new IdentityHashMap<>();
             for (final VariableInfo variable : variables) {
-                switch (roleOf(variable)) {
+                switch (convention().roleOf(variable)) {
                     case COVERAGE: {
                         // If Convention.roleOf(…) overwrote the value computed by VariableInfo,
                         // remember the new value for avoiding to ask again in next loops.
@@ -935,7 +935,7 @@ nextVar:    for (final VariableInfo variable : variables) {
                 axes.clear();
                 usedDimensions.clear();
                 if (!listAxes(variable.getCoordinateVariables(), axes, usedDimensions)) {
-                    listAxes(namesOfAxisVariables(variable), axes, usedDimensions);
+                    listAxes(convention().namesOfAxisVariables(variable), axes, usedDimensions);
                 }
                 /*
                  * In theory the "coordinates" attribute would enumerate all axes needed for covering all dimensions,
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/GridInfo.java b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/GridInfo.java
index dedd304..0aea558 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/GridInfo.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/GridInfo.java
@@ -100,8 +100,8 @@ final class GridInfo extends Grid {
      * @param  range   the output values of the "grid to CRS" conversion, in CRS order as much as possible.
      */
     GridInfo(final Dimension[] domain, final VariableInfo[] range) {
-        this.domain   = domain;
-        this.range    = range;
+        this.domain = domain;
+        this.range  = range;
     }
 
     /**
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/GridResource.java b/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/GridResource.java
index 3a84141..fef9e5e 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/GridResource.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/GridResource.java
@@ -157,7 +157,7 @@ final class GridResource extends AbstractGridResource implements ResourceOnFileS
         final List<Resource> resources = new ArrayList<>();
         for (int i=0; i<variables.length; i++) {
             final Variable variable = variables[i];
-            if (decoder.roleOf(variable) != VariableRole.COVERAGE) {
+            if (decoder.convention().roleOf(variable) != VariableRole.COVERAGE) {
                 continue;                                                   // Skip variables that are not grid coverages.
             }
             final Grid grid = variable.getGrid(decoder);
@@ -185,7 +185,7 @@ final class GridResource extends AbstractGridResource implements ResourceOnFileS
                     int suffixLength = name.length() - suffixStart;
                     for (int j=i; ++j < variables.length;) {
                         final Variable candidate = variables[j];
-                        if (decoder.roleOf(candidate) != VariableRole.COVERAGE) {
+                        if (decoder.convention().roleOf(candidate) != VariableRole.COVERAGE) {
                             variables[j] = null;                                // For avoiding to revisit that variable again.
                             continue;
                         }
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/MetadataReader.java b/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/MetadataReader.java
index 0eb9714..be9f2ac 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/MetadataReader.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/MetadataReader.java
@@ -902,7 +902,7 @@ split:  while ((start = CharSequences.skipLeadingWhitespaces(value, start, lengt
     private void addContentInfo() {
         final Map<List<String>, List<Variable>> contents = new HashMap<>(4);
         for (final Variable variable : decoder.getVariables()) {
-            if (decoder.roleOf(variable) == VariableRole.COVERAGE) {
+            if (decoder.convention().roleOf(variable) == VariableRole.COVERAGE) {
                 final List<String> dimensions = Arrays.asList(variable.getGridDimensionNames());
                 CollectionsExt.addToMultiValuesMap(contents, dimensions, variable);
             }