sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ama...@apache.org
Subject [sis] branch geoapi-4.0 updated: fix(Feature): slight improvement on feature type builder: automatically replace primitive value class of attributes by their boxed equivalent
Date Wed, 23 Sep 2020 10:01:53 GMT
This is an automated email from the ASF dual-hosted git repository.

amanin 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 de1ac25  fix(Feature): slight improvement on feature type builder: automatically
replace primitive value class of attributes by their boxed equivalent
de1ac25 is described below

commit de1ac2537b2a6dd28e79bfd00939042134076b03
Author: Alexis Manin <alexis.manin@geomatys.com>
AuthorDate: Wed Sep 23 11:31:25 2020 +0200

    fix(Feature): slight improvement on feature type builder: automatically replace primitive
value class of attributes by their boxed equivalent
    
    Keeping primitive value class was breaking value affectation, because only primitive values
where accepted, but only boxed values could be given (as method signature accepts Object).
---
 .../sis/feature/builder/FeatureTypeBuilder.java    |  6 ++++-
 .../feature/builder/AttributeTypeBuilderTest.java  | 28 ++++++++++++++++++++++
 2 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/core/sis-feature/src/main/java/org/apache/sis/feature/builder/FeatureTypeBuilder.java
b/core/sis-feature/src/main/java/org/apache/sis/feature/builder/FeatureTypeBuilder.java
index 29ed49a..ebdd3d3 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/feature/builder/FeatureTypeBuilder.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/feature/builder/FeatureTypeBuilder.java
@@ -25,6 +25,7 @@ import java.util.Map;
 import java.util.Locale;
 import java.util.Set;
 import java.util.Objects;
+import org.apache.sis.util.Numbers;
 import org.opengis.util.NameSpace;
 import org.opengis.util.GenericName;
 import org.opengis.util.NameFactory;
@@ -676,12 +677,15 @@ public class FeatureTypeBuilder extends TypeBuilder {
      *
      * @see #properties()
      */
-    public <V> AttributeTypeBuilder<V> addAttribute(final Class<V> valueClass)
{
+    public <V> AttributeTypeBuilder<V> addAttribute(Class<V> valueClass)
{
         ensureNonNull("valueClass", valueClass);
         if (Feature.class.isAssignableFrom(valueClass)) {
             // We disallow Feature.class because that type shall be handled as association
instead than attribute.
             throw new IllegalArgumentException(errors().getString(Errors.Keys.IllegalArgumentValue_2,
"valueClass", valueClass));
         }
+
+        valueClass = (Class<V>) Numbers.primitiveToWrapper(valueClass);
+
         final AttributeTypeBuilder<V> property = new AttributeTypeBuilder<>(this,
valueClass);
         properties.add(property);
         clearCache();
diff --git a/core/sis-feature/src/test/java/org/apache/sis/feature/builder/AttributeTypeBuilderTest.java
b/core/sis-feature/src/test/java/org/apache/sis/feature/builder/AttributeTypeBuilderTest.java
index b709322..1ae4a64 100644
--- a/core/sis-feature/src/test/java/org/apache/sis/feature/builder/AttributeTypeBuilderTest.java
+++ b/core/sis-feature/src/test/java/org/apache/sis/feature/builder/AttributeTypeBuilderTest.java
@@ -20,6 +20,11 @@ import java.util.Arrays;
 import java.util.Set;
 import java.util.Collections;
 import com.esri.core.geometry.Geometry;
+import org.opengis.feature.Attribute;
+import org.opengis.feature.Feature;
+import org.opengis.feature.FeatureType;
+import org.opengis.feature.Property;
+import org.opengis.feature.PropertyType;
 import org.opengis.referencing.crs.CoordinateReferenceSystem;
 import org.apache.sis.referencing.CommonCRS;
 import org.apache.sis.internal.feature.AttributeConvention;
@@ -240,4 +245,27 @@ public final strictfp class AttributeTypeBuilderTest extends TestCase
{
         assertTrue("isEmpty", roles.isEmpty());
         assertFalse("remove(IDENTIFIER_COMPONENT)", roles.remove(AttributeRole.IDENTIFIER_COMPONENT));
     }
+
+    @Test
+    public void testBoxing() {
+        final FeatureTypeBuilder ftb = new FeatureTypeBuilder().setName("boxing");
+        final AttributeTypeBuilder<Integer> boxBuilder = ftb.addAttribute(int.class).setName("boxed");
+        assertEquals("Attribute value type should have been boxed", Integer.class, boxBuilder.getValueClass());
+
+        final FeatureType ft = ftb.build();
+        final PropertyType boxedProperty = ft.getProperty("boxed");
+        assertTrue(boxedProperty instanceof AttributeType);
+        assertEquals("Attribute value type should have been boxed", Integer.class, ((AttributeType)boxedProperty).getValueClass());
+        final Feature feature = ft.newInstance();
+
+        final Property p = feature.getProperty("boxed");
+        assertTrue(p instanceof Attribute);
+        assertEquals("Attribute value type should have been boxed", Integer.class, ((Attribute<?>)
p).getType().getValueClass());
+        int value = 3;
+        ((Attribute<Integer>) p).setValue(value);
+        assertEquals(3, p.getValue());
+
+        feature.setPropertyValue("boxed", Integer.valueOf(4));
+        assertEquals(4, feature.getPropertyValue("boxed"));
+    }
 }


Mime
View raw message