sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1801603 - in /sis/branches/JDK8/core/sis-feature/src: main/java/org/apache/sis/feature/ main/java/org/apache/sis/internal/feature/ test/java/org/apache/sis/internal/feature/
Date Tue, 11 Jul 2017 12:16:30 GMT
Author: desruisseaux
Date: Tue Jul 11 12:16:29 2017
New Revision: 1801603

URL: http://svn.apache.org/viewvc?rev=1801603&view=rev
Log:
Add a convenience method for fetching the default CRS from a PropertyType instead than from
a Property.

Modified:
    sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/LinkOperation.java
    sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/internal/feature/AttributeConvention.java
    sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/internal/feature/FeatureUtilities.java
    sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/internal/feature/AttributeConventionTest.java

Modified: sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/LinkOperation.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/LinkOperation.java?rev=1801603&r1=1801602&r2=1801603&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/LinkOperation.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/LinkOperation.java
[UTF-8] Tue Jul 11 12:16:29 2017
@@ -24,6 +24,7 @@ import org.opengis.parameter.ParameterVa
 import org.opengis.parameter.ParameterDescriptorGroup;
 import org.apache.sis.internal.feature.FeatureUtilities;
 import org.apache.sis.util.ArgumentChecks;
+import org.apache.sis.util.resources.Errors;
 
 // Branch-dependent imports
 import org.opengis.feature.Feature;
@@ -48,11 +49,6 @@ final class LinkOperation extends Abstra
     private static final long serialVersionUID = 765096861589501215L;
 
     /**
-     * The parameter descriptor for the "Link" operation, which does not take any parameter.
-     */
-    private static final ParameterDescriptorGroup EMPTY_PARAMS = FeatureUtilities.parameters("Link");
-
-    /**
      * The type of the result.
      */
     private final PropertyType result;
@@ -74,9 +70,13 @@ final class LinkOperation extends Abstra
         super(identification);
         if (referent instanceof LinkOperation) {
             referent = ((LinkOperation) referent).result;
+            // Avoiding links to links may help performance and reduce the risk of circular
references.
         }
         result = referent;
         referentName = referent.getName().toString();
+        if (referentName.equals(getName().toString())) {
+            throw new IllegalArgumentException(Errors.format(Errors.Keys.CircularReference));
+        }
     }
 
     /**
@@ -84,7 +84,7 @@ final class LinkOperation extends Abstra
      */
     @Override
     public ParameterDescriptorGroup getParameters() {
-        return EMPTY_PARAMS;
+        return FeatureUtilities.LINK_PARAMS;
     }
 
     /**

Modified: sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/internal/feature/AttributeConvention.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/internal/feature/AttributeConvention.java?rev=1801603&r1=1801602&r2=1801603&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/internal/feature/AttributeConvention.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/internal/feature/AttributeConvention.java
[UTF-8] Tue Jul 11 12:16:29 2017
@@ -29,6 +29,8 @@ import org.opengis.feature.AttributeType
 import org.opengis.feature.IdentifiedType;
 import org.opengis.feature.Operation;
 import org.opengis.feature.Property;
+import org.opengis.feature.PropertyType;
+import org.opengis.feature.FeatureType;
 
 
 /**
@@ -251,6 +253,25 @@ public final class AttributeConvention e
     }
 
     /**
+     * Returns the Coordinate Reference Systems characteristic for the given property type,
or {@code null} if none.
+     * This method gets the default value from the characteristic named {@link #CRS_CHARACTERISTIC}.
+     * If the given property is a link, then this method follows the link in the given feature
type (if non-null).
+     *
+     * <p>This method should be used only when the actual property instance is unknown.
+     * Otherwise, {@link #getCRSCharacteristic(Property)} should be used because the CRS
+     * may vary for each property instance.</p>
+     *
+     * @param  feature    the feature type in which to follow links, or {@code null} if none.
+     * @param  attribute  the attribute type for which to get the CRS, or {@code null}.
+     * @return the Coordinate Reference System characteristic of the given property type,
or {@code null} if none.
+     * @throws ClassCastException if {@link #CRS_CHARACTERISTIC} has been found but is associated
+     *         to an object which is not a {@link CoordinateReferenceSystem} instance.
+     */
+    public static CoordinateReferenceSystem getCRSCharacteristic(final FeatureType feature,
final PropertyType attribute) {
+        return (CoordinateReferenceSystem) getCharacteristic(feature, attribute, CRS_CHARACTERISTIC.toString());
+    }
+
+    /**
      * Returns whether the given operation or attribute type is characterized by a maximal
length.
      * This method verifies whether a characteristic named {@link #MAXIMAL_LENGTH_CHARACTERISTIC}
      * with values of class {@link Integer} exists (directly or indirectly) for the given
type.
@@ -266,8 +287,8 @@ public final class AttributeConvention e
      * Returns the maximal length characteristic for the given attribute, or {@code null}
if none.
      * This method gets the value or default value from the characteristic named {@link #MAXIMAL_LENGTH_CHARACTERISTIC}.
      *
-     * @param  attribute  the attribute for which to get the CRS, or {@code null}.
-     * @return the Coordinate Reference System characteristic of the given attribute, or
{@code null} if none.
+     * @param  attribute  the attribute for which to get the maximal length, or {@code null}.
+     * @return the maximal length characteristic of the given attribute, or {@code null}
if none.
      * @throws ClassCastException if {@link #MAXIMAL_LENGTH_CHARACTERISTIC} has been found
but is associated
      *         to an object which is not an {@link Integer} instance.
      *
@@ -278,6 +299,25 @@ public final class AttributeConvention e
     }
 
     /**
+     * Returns the maximal length characteristic for the given property type, or {@code null}
if none.
+     * This method gets the default value from the characteristic named {@link #MAXIMAL_LENGTH_CHARACTERISTIC}.
+     * If the given property is a link, then this method follows the link in the given feature
type (if non-null).
+     *
+     * <p>This method should be used only when the actual property instance is unknown.
+     * Otherwise, {@link #getMaximalLengthCharacteristic(Property)} should be used because
+     * the maximal length may vary for each property instance.</p>
+     *
+     * @param  feature    the feature type in which to follow links, or {@code null} if none.
+     * @param  attribute  the attribute type for which to get the maximal length, or {@code
null}.
+     * @return the maximal length characteristic of the given property type, or {@code null}
if none.
+     * @throws ClassCastException if {@link #MAXIMAL_LENGTH_CHARACTERISTIC} has been found
but is associated
+     *         to an object which is not a {@link CoordinateReferenceSystem} instance.
+     */
+    public static Integer getMaximalLengthCharacteristic(final FeatureType feature, final
PropertyType attribute) {
+        return (Integer) getCharacteristic(feature, attribute, MAXIMAL_LENGTH_CHARACTERISTIC.toString());
+    }
+
+    /**
      * Returns {@code true} if the given operation or attribute type has a characteristic
of the given name,
      * and the values of that characteristic are assignable to the given {@code valueClass}.
      *
@@ -321,6 +361,30 @@ public final class AttributeConvention e
             if (type != null) {
                 return type.getDefaultValue();
             }
+        }
+        return null;
+    }
+
+    /**
+     * Fetches from the given property the default value of the characteristic of the given
name.
+     * If the given property is a link, then this method follows the link in the given feature
type
+     * (unless that feature type is null).
+     *
+     * @param  feature         the feature type in which to follow links, or {@code null}
if none.
+     * @param  property        the property from which to get the characteristic value, or
{@code null}.
+     * @param  characteristic  name of the characteristic from which to get the default value.
+     * @return the default value of the named characteristic in the given property, or {@code
null} if none.
+     */
+    private static Object getCharacteristic(final FeatureType feature, PropertyType property,
final String characteristic) {
+        final String referent = FeatureUtilities.linkOf(property);
+        if (referent != null && feature != null) {
+            property = feature.getProperty(referent);
+        }
+        if (property instanceof AttributeType<?>) {
+            final AttributeType<?> type = ((AttributeType<?>) property).characteristics().get(characteristic);
+            if (type != null) {
+                return type.getDefaultValue();
+            }
         }
         return null;
     }

Modified: sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/internal/feature/FeatureUtilities.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/internal/feature/FeatureUtilities.java?rev=1801603&r1=1801602&r2=1801603&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/internal/feature/FeatureUtilities.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/internal/feature/FeatureUtilities.java
[UTF-8] Tue Jul 11 12:16:29 2017
@@ -23,8 +23,12 @@ import org.opengis.parameter.ParameterDe
 import org.opengis.parameter.ParameterDescriptorGroup;
 import org.apache.sis.parameter.DefaultParameterDescriptorGroup;
 import org.apache.sis.metadata.iso.citation.Citations;
+import org.apache.sis.feature.AbstractOperation;
 import org.apache.sis.util.Static;
 
+// Branch-dependent imports
+import org.opengis.feature.PropertyType;
+
 
 /**
  * Non-public utility methods for Apache SIS internal usage.
@@ -37,6 +41,13 @@ import org.apache.sis.util.Static;
  */
 public final class FeatureUtilities extends Static {
     /**
+     * The parameter descriptor for the "Link" operation, which does not take any parameter.
+     * We use those parameters as a way to identify the link operation without making the
+     * {@code LinkOperation} class public.
+     */
+    public static final ParameterDescriptorGroup LINK_PARAMS = parameters("Link");
+
+    /**
      * Do not allow instantiation of this class.
      */
     private FeatureUtilities() {
@@ -56,4 +67,21 @@ public final class FeatureUtilities exte
         properties.put(Identifier.AUTHORITY_KEY, Citations.SIS);
         return new DefaultParameterDescriptorGroup(properties, 1, 1);
     }
+
+    /**
+     * If the given property is a link, returns the name of the referenced property.
+     * Otherwise returns {@code null}.
+     *
+     * @param  property  the property to test, or {@code null} if none.
+     * @return the referenced property name, or {@code null} if none.
+     */
+    static String linkOf(final PropertyType property) {
+        if (property instanceof AbstractOperation) {
+            final AbstractOperation op = (AbstractOperation) property;
+            if (op.getParameters() == LINK_PARAMS) {
+                return op.getDependencies().iterator().next();          // Should always
have exactly one element.
+            }
+        }
+        return null;
+    }
 }

Modified: sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/internal/feature/AttributeConventionTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/internal/feature/AttributeConventionTest.java?rev=1801603&r1=1801602&r2=1801603&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/internal/feature/AttributeConventionTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/internal/feature/AttributeConventionTest.java
[UTF-8] Tue Jul 11 12:16:29 2017
@@ -22,6 +22,8 @@ import com.esri.core.geometry.Point;
 import org.opengis.referencing.crs.CoordinateReferenceSystem;
 import org.apache.sis.referencing.crs.HardCodedCRS;
 import org.apache.sis.feature.DefaultAttributeType;
+import org.apache.sis.feature.DefaultFeatureType;
+import org.apache.sis.feature.FeatureOperations;
 import org.apache.sis.util.iso.Names;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
@@ -30,6 +32,7 @@ import static org.junit.Assert.*;
 
 // Branch-dependent imports
 import org.opengis.feature.Property;
+import org.opengis.feature.Operation;
 import org.opengis.feature.IdentifiedType;
 
 
@@ -88,6 +91,14 @@ public final strictfp class AttributeCon
         type = new DefaultAttributeType<>(properties, Point.class, 1, 1, null, characteristic);
         assertTrue("characterizedByCRS", AttributeConvention.characterizedByCRS(type));
         assertEquals(HardCodedCRS.WGS84, AttributeConvention.getCRSCharacteristic(type.newInstance()));
+        assertEquals(HardCodedCRS.WGS84, AttributeConvention.getCRSCharacteristic(null, type));
+        /*
+         * Test again AttributeConvention.getCRSCharacteristic(…, PropertyType), but following
link.
+         */
+        final Operation link = FeatureOperations.link(Collections.singletonMap(DefaultAttributeType.NAME_KEY,
"geom"), type);
+        final DefaultFeatureType feat = new DefaultFeatureType(Collections.singletonMap(DefaultAttributeType.NAME_KEY,
"feat"), false, null, type, link);
+        assertEquals(HardCodedCRS.WGS84, AttributeConvention.getCRSCharacteristic(feat, link));
+        assertNull(                      AttributeConvention.getCRSCharacteristic(null, link));
     }
 
     /**



Mime
View raw message