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: Provide a way to avoid potentially costly PropertyNotFoundException creation. Users do not always know in advance if a property exists and may need a way to get this information without being forced to use exceptions for branching.
Date Wed, 25 Nov 2020 23:08:37 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 57c225f  Provide a way to avoid potentially costly PropertyNotFoundException creation.
Users do not always know in advance if a property exists and may need a way to get this information
without being forced to use exceptions for branching.
57c225f is described below

commit 57c225fa5d10248f00d20bc8a49d1c71c69494a9
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Wed Nov 25 23:59:14 2020 +0100

    Provide a way to avoid potentially costly PropertyNotFoundException creation.
    Users do not always know in advance if a property exists and may need a way
    to get this information without being forced to use exceptions for branching.
---
 .../apache/sis/gui/dataset/ExpandedFeature.java    |  8 +++++
 .../org/apache/sis/feature/AbstractFeature.java    | 37 ++++++++++++++++++++--
 .../java/org/apache/sis/feature/DenseFeature.java  | 25 +++++++++++++--
 .../java/org/apache/sis/feature/SparseFeature.java | 27 +++++++++++++---
 .../apache/sis/feature/AbstractFeatureTest.java    | 10 +++++-
 5 files changed, 97 insertions(+), 10 deletions(-)

diff --git a/application/sis-javafx/src/main/java/org/apache/sis/gui/dataset/ExpandedFeature.java
b/application/sis-javafx/src/main/java/org/apache/sis/gui/dataset/ExpandedFeature.java
index 536e6d8..fe0550a 100644
--- a/application/sis-javafx/src/main/java/org/apache/sis/gui/dataset/ExpandedFeature.java
+++ b/application/sis-javafx/src/main/java/org/apache/sis/gui/dataset/ExpandedFeature.java
@@ -171,6 +171,14 @@ final class ExpandedFeature implements Feature {
     }
 
     /**
+     * Synonymous of {@link #getPropertyValue(String)} since we do not check property existence.
+     */
+    @Override
+    public Object getPropertyValue(final String name, final Object missingPropertyFallback)
{
+        return getPropertyValue(name);
+    }
+
+    /**
      * Unsupported operation.
      */
     @Override
diff --git a/core/sis-feature/src/main/java/org/apache/sis/feature/AbstractFeature.java b/core/sis-feature/src/main/java/org/apache/sis/feature/AbstractFeature.java
index 8cfb110..77687b9 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/feature/AbstractFeature.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/feature/AbstractFeature.java
@@ -78,7 +78,7 @@ import org.opengis.feature.Operation;
  * @author  Travis L. Pinney
  * @author  Johann Sorel (Geomatys)
  * @author  Martin Desruisseaux (Geomatys)
- * @version 0.8
+ * @version 1.1
  *
  * @see DefaultFeatureType#newInstance()
  *
@@ -92,6 +92,13 @@ public abstract class AbstractFeature implements Feature, Serializable
{
     private static final long serialVersionUID = -5637918246427380190L;
 
     /**
+     * Sentinel value for missing property.
+     *
+     * @see #getPropertyValue(String, Object)
+     */
+    static final Object MISSING = new Object();
+
+    /**
      * Information about the feature (name, characteristics, <i>etc.</i>).
      */
     final FeatureType type;
@@ -326,7 +333,7 @@ public abstract class AbstractFeature implements Feature, Serializable
{
      * }
      *
      * @param  name  the property name.
-     * @return the value for the given property, or {@code null} if none.
+     * @return the value for the specified property, or {@code null} if none.
      * @throws PropertyNotFoundException if the given argument is not an attribute or association
name of this feature.
      *
      * @see AbstractAttribute#getValue()
@@ -335,6 +342,32 @@ public abstract class AbstractFeature implements Feature, Serializable
{
     public abstract Object getPropertyValue(final String name) throws PropertyNotFoundException;
 
     /**
+     * Returns the value for the property of the given name if that property exists, or a
fallback value otherwise.
+     * This method is equivalent to the following code, but potentially more efficient:
+     *
+     * {@preformat java
+     *     try {
+     *         return getPropertyValue(name);
+     *     } catch (PropertyNotFoundException ignore) {
+     *         return missingPropertyFallback
+     *     }
+     * }
+     *
+     * Note that if a property of the given name exists but has no value, then this method
returns {@code null}.
+     * <cite>Property without value</cite> is not equivalent to <cite>non-existent
property</cite>.
+     *
+     * @param  name  the property name.
+     * @param  missingPropertyFallback  the (potentially {@code null}) value to return
+     *         if no attribute or association of the given name exists.
+     * @return the value for the specified property, or the fallback value if no attribute
or association
+     *         of that name exists. This value may be {@code null}.
+     *
+     * @since 1.1
+     */
+    @Override
+    public abstract Object getPropertyValue(final String name, Object missingPropertyFallback);
+
+    /**
      * Sets the value for the property of the given name.
      *
      * <h4>Validation</h4>
diff --git a/core/sis-feature/src/main/java/org/apache/sis/feature/DenseFeature.java b/core/sis-feature/src/main/java/org/apache/sis/feature/DenseFeature.java
index 79b8204..e45b6f5 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/feature/DenseFeature.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/feature/DenseFeature.java
@@ -37,7 +37,7 @@ import org.opengis.feature.PropertyNotFoundException;
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @author  Marc le Bihan
- * @version 0.6
+ * @version 1.1
  *
  * @see SparseFeature
  * @see DefaultFeatureType
@@ -180,8 +180,27 @@ final class DenseFeature extends AbstractFeature implements Cloneable
{
      */
     @Override
     public Object getPropertyValue(final String name) throws PropertyNotFoundException {
+        final Object value = getPropertyValue(name, MISSING);
+        if (value != MISSING) return value;
+        throw new PropertyNotFoundException(propertyNotFound(type, getName(), name));
+    }
+
+    /**
+     * Returns the value for the property of the given name if that property exists, or a
fallback value otherwise.
+     *
+     * @param  name  the property name.
+     * @param  missingPropertyFallback  the value to return if no attribute or association
of the given name exists.
+     * @return the value for the given property, or {@code null} if none.
+     *
+     * @since 1.1
+     */
+    @Override
+    public final Object getPropertyValue(final String name, final Object missingPropertyFallback)
{
         ArgumentChecks.ensureNonNull("name", name);
-        final int index = getIndex(name);
+        final Integer index = indices.get(name);
+        if (index == null) {
+            return missingPropertyFallback;
+        }
         if (index < 0) {
             return getOperationValue(name);
         }
@@ -189,7 +208,7 @@ final class DenseFeature extends AbstractFeature implements Cloneable
{
             final Object element = properties[index];
             if (element != null) {
                 if (!(properties instanceof Property[])) {
-                    return element; // Most common case.
+                    return element;                                         // Most common
case.
                 } else if (element instanceof Attribute<?>) {
                     return getAttributeValue((Attribute<?>) element);
                 } else if (element instanceof FeatureAssociation) {
diff --git a/core/sis-feature/src/main/java/org/apache/sis/feature/SparseFeature.java b/core/sis-feature/src/main/java/org/apache/sis/feature/SparseFeature.java
index 59562b1..67bb4d9 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/feature/SparseFeature.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/feature/SparseFeature.java
@@ -41,7 +41,7 @@ import org.opengis.feature.PropertyNotFoundException;
  * @author  Travis L. Pinney
  * @author  Johann Sorel (Geomatys)
  * @author  Martin Desruisseaux (Geomatys)
- * @version 0.8
+ * @version 1.1
  *
  * @see DenseFeature
  * @see DefaultFeatureType
@@ -122,7 +122,7 @@ final class SparseFeature extends AbstractFeature implements Cloneable
{
      *         or a negative value if the property is a parameterless operation.
      * @throws PropertyNotFoundException if the given argument is not a property name of
this feature.
      */
-    private int getIndex(final String name) throws PropertyNotFoundException {
+    private Integer getIndex(final String name) throws PropertyNotFoundException {
         final Integer index = indices.get(name);
         if (index != null) {
             return index;
@@ -228,15 +228,34 @@ final class SparseFeature extends AbstractFeature implements Cloneable
{
      */
     @Override
     public Object getPropertyValue(final String name) throws PropertyNotFoundException {
+        final Object value = getPropertyValue(name, MISSING);
+        if (value != MISSING) return value;
+        throw new PropertyNotFoundException(propertyNotFound(type, getName(), name));
+    }
+
+    /**
+     * Returns the value for the property of the given name if that property exists, or a
fallback value otherwise.
+     *
+     * @param  name  the property name.
+     * @param  missingPropertyFallback  the value to return if no attribute or association
of the given name exists.
+     * @return the value for the given property, or {@code null} if none.
+     *
+     * @since 1.1
+     */
+    @Override
+    public final Object getPropertyValue(final String name, final Object missingPropertyFallback)
{
         ArgumentChecks.ensureNonNull("name", name);
-        final Integer index = getIndex(name);
+        final Integer index = indices.get(name);
+        if (index == null) {
+            return missingPropertyFallback;
+        }
         if (index < 0) {
             return getOperationValue(name);
         }
         final Object element = properties.get(index);
         if (element != null) {
             if (valuesKind == VALUES) {
-                return element; // Most common case.
+                return element;                                         // Most common case.
             } else if (element instanceof Attribute<?>) {
                 return getAttributeValue((Attribute<?>) element);
             } else if (element instanceof FeatureAssociation) {
diff --git a/core/sis-feature/src/test/java/org/apache/sis/feature/AbstractFeatureTest.java
b/core/sis-feature/src/test/java/org/apache/sis/feature/AbstractFeatureTest.java
index 010801d..0abda30 100644
--- a/core/sis-feature/src/test/java/org/apache/sis/feature/AbstractFeatureTest.java
+++ b/core/sis-feature/src/test/java/org/apache/sis/feature/AbstractFeatureTest.java
@@ -38,7 +38,7 @@ import org.opengis.feature.PropertyType;
  * <p>This class inherits all tests defined in {@link FeatureTestCase}.</p>
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 0.8
+ * @version 1.1
  * @since   0.8
  * @module
  */
@@ -102,6 +102,14 @@ public final strictfp class AbstractFeatureTest extends FeatureTestCase
{
         }
 
         /**
+         * Synonymous of {@link #getPropertyValue(String)} for this test.
+         */
+        @Override
+        public Object getPropertyValue(final String name, final Object missingPropertyFallback)
{
+            return getPropertyValue(name);
+        }
+
+        /**
          * Sets the value for the property of the given name. In order to allow the tests
to pass,
          * we need to reproduce in this method some of the verifications performed by the
          * {@link SingletonAttribute} and {@link MultiValuedAttribute} implementations.


Mime
View raw message