sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1686211 - in /sis/branches/JDK8/core/sis-feature/src: main/java/org/apache/sis/feature/FeatureFormat.java test/java/org/apache/sis/feature/FeatureFormatTest.java test/java/org/apache/sis/feature/FeatureTestCase.java
Date Thu, 18 Jun 2015 13:10:53 GMT
Author: desruisseaux
Date: Thu Jun 18 13:10:53 2015
New Revision: 1686211

URL: http://svn.apache.org/r1686211
Log:
Feature: bug fix when the value to format is a collection.

Modified:
    sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/FeatureFormat.java
    sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/FeatureFormatTest.java
    sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/FeatureTestCase.java

Modified: sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/FeatureFormat.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/FeatureFormat.java?rev=1686211&r1=1686210&r2=1686211&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/FeatureFormat.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/FeatureFormat.java
[UTF-8] Thu Jun 18 13:10:53 2015
@@ -16,6 +16,9 @@
  */
 package org.apache.sis.feature;
 
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
 import java.util.Locale;
 import java.util.TimeZone;
 import java.io.IOException;
@@ -71,7 +74,7 @@ import org.opengis.feature.Operation;
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.5
- * @version 0.5
+ * @version 0.6
  * @module
  */
 public class FeatureFormat extends TabularFormat<Object> {
@@ -301,19 +304,28 @@ header: for (int i=0; ; i++) {
              * Column 3 - Value or default value.
              */
             if (value != null) {
-                final Format format = getFormat(valueClass);
-                if (format != null) {
-                    value = format.format(value, buffer, dummyFP);
-                } else if (value instanceof Feature && propertyType instanceof FeatureAssociationRole)
{
-                    final String p = DefaultAssociationRole.getTitleProperty((FeatureAssociationRole)
propertyType);
-                    if (p != null) {
-                        value = ((Feature) value).getPropertyValue(p);
+                final boolean isInstance = valueClass != null && valueClass.isInstance(value);
+                final Format format = isInstance ? getFormat(valueClass) : null;
+                final Iterator<?> it = (!isInstance && (value instanceof Collection<?>)
+                        ? (Collection<?>) value : Collections.singleton(value)).iterator();
+                String separator = "";
+                while (it.hasNext()) {
+                    value = it.next();
+                    if (value != null) {
+                        if (format != null) {
+                            value = format.format(value, buffer, dummyFP);
+                        } else if (value instanceof Feature && propertyType instanceof
FeatureAssociationRole) {
+                            final String p = DefaultAssociationRole.getTitleProperty((FeatureAssociationRole)
propertyType);
+                            if (p != null) {
+                                value = ((Feature) value).getPropertyValue(p);
+                                if (value == null) continue;
+                            }
+                        }
+                        table.append(separator).append(formatValue(value));
+                        buffer.setLength(0);
+                        separator = ", ";
                     }
                 }
-                if (value != null) {
-                    table.append(formatValue(value));
-                }
-                buffer.setLength(0);
             }
             /*
              * Column 4 - Characteristics.

Modified: sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/FeatureFormatTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/FeatureFormatTest.java?rev=1686211&r1=1686210&r2=1686211&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/FeatureFormatTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/FeatureFormatTest.java
[UTF-8] Thu Jun 18 13:10:53 2015
@@ -16,6 +16,7 @@
  */
 package org.apache.sis.feature;
 
+import java.util.Arrays;
 import java.util.Locale;
 import java.util.Random;
 import org.apache.sis.test.DependsOn;
@@ -31,7 +32,7 @@ import static org.apache.sis.test.Assert
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.5
- * @version 0.5
+ * @version 0.6
  * @module
  */
 @DependsOn({
@@ -66,7 +67,47 @@ public final strictfp class FeatureForma
     @Test
     public void testFeature() {
         final Random random = TestUtilities.createRandomNumberGenerator();
-        final AbstractFeature feature = FeatureTestCase.twinTown(random.nextBoolean());
+        final boolean isSparse = random.nextBoolean();
+
+        final DefaultFeatureType type = DefaultFeatureTypeTest.worldMetropolis();
+        final AbstractFeature feature = isSparse ? new SparseFeature(type) : new DenseFeature(type);
+        feature.setPropertyValue("city", "Tokyo");
+        feature.setPropertyValue("population", 13185502); // In 2011.
+        feature.setPropertyValue("universities", Arrays.asList("Waseda", "Keio"));
+
+        final FeatureFormat format = new FeatureFormat(Locale.US, null);
+        final String text = format.format(feature);
+        assertMultilinesEquals("World metropolis\n" +
+                "┌──────────────┬─────────────────────┬─────────────┬──────────────┬─────────────────┐\n"
+
+                "│ Name         │ Type                │ Cardinality │ Value     
  │ Characteristics │\n" +
+                "├──────────────┼─────────────────────┼─────────────┼──────────────┼─────────────────┤\n"
+
+                "│ city         │ String              │ [1 … 1]     │ Tokyo   
    │                 │\n" +
+                "│ population   │ Integer             │ [1 … 1]     │ 13,185,502
  │                 │\n" +
+                "│ region       │ InternationalString │ [1 … 1]     │         
    │                 │\n" +
+                "│ isGlobal     │ Boolean             │ [1 … 1]     │         
    │                 │\n" +
+                "│ universities │ String              │ [0 … ∞]     │ Waseda,
Keio │                 │\n" +
+                "│ temperature  │ Float               │ [1 … 1]     │         
    │ accuracy, units │\n" +
+                "└──────────────┴─────────────────────┴─────────────┴──────────────┴─────────────────┘\n",
text);
+    }
+
+    /**
+     * Tests the formatting of an {@link AbstractFeature} with an association to another
feature of the same type.
+     */
+    @Test
+    public void testFeatureWithAssociation() {
+        final Random random = TestUtilities.createRandomNumberGenerator();
+        final boolean isSparse = random.nextBoolean();
+
+        final DefaultFeatureType type = DefaultAssociationRoleTest.twinTownCity(false);
+        final AbstractFeature twinTown = isSparse ? new SparseFeature(type) : new DenseFeature(type);
+        twinTown.setPropertyValue("city", "Le Mans");
+        twinTown.setPropertyValue("population", 143240); // In 2011.
+
+        final AbstractFeature feature = isSparse ? new SparseFeature(type) : new DenseFeature(type);
+        feature.setPropertyValue("city", "Paderborn");
+        feature.setPropertyValue("population", 143174); // December 31th, 2011
+        feature.setPropertyValue("twin town", twinTown);
+
         final FeatureFormat format = new FeatureFormat(Locale.US, null);
         final String text = format.format(feature);
         assertMultilinesEquals("Twin town\n" +

Modified: sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/FeatureTestCase.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/FeatureTestCase.java?rev=1686211&r1=1686210&r2=1686211&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/FeatureTestCase.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/FeatureTestCase.java
[UTF-8] Thu Jun 18 13:10:53 2015
@@ -60,23 +60,6 @@ public abstract strictfp class FeatureTe
     }
 
     /**
-     * Creates a feature for twin towns.
-     */
-    static AbstractFeature twinTown(final boolean isSparse) {
-        final DefaultFeatureType twinTown = DefaultAssociationRoleTest.twinTownCity(false);
-
-        final AbstractFeature leMans = isSparse ? new SparseFeature(twinTown) : new DenseFeature(twinTown);
-        leMans.setPropertyValue("city", "Le Mans");
-        leMans.setPropertyValue("population", 143240); // In 2011.
-
-        final AbstractFeature paderborn = isSparse ? new SparseFeature(twinTown) : new DenseFeature(twinTown);
-        paderborn.setPropertyValue("city", "Paderborn");
-        paderborn.setPropertyValue("population", 143174); // December 31th, 2011
-        paderborn.setPropertyValue("twin town", leMans);
-        return paderborn;
-    }
-
-    /**
      * Creates a new feature for the given type.
      */
     abstract AbstractFeature createFeature(final DefaultFeatureType type);



Mime
View raw message