sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1756007 - in /sis/branches/JDK8/core/sis-feature/src: main/java/org/apache/sis/feature/ test/java/org/apache/sis/feature/
Date Thu, 11 Aug 2016 16:26:45 GMT
Author: desruisseaux
Date: Thu Aug 11 16:26:45 2016
New Revision: 1756007

URL: http://svn.apache.org/viewvc?rev=1756007&view=rev
Log:
Provide a default formatting for all operations.

Modified:
    sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/AbstractOperation.java
    sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/FeatureFormat.java
    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/feature/StringJoinOperation.java
    sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/AbstractOperationTest.java
    sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/FeatureFormatTest.java

Modified: sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/AbstractOperation.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/AbstractOperation.java?rev=1756007&r1=1756006&r2=1756007&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/AbstractOperation.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/AbstractOperation.java
[UTF-8] Thu Aug 11 16:26:45 2016
@@ -20,17 +20,18 @@ import java.util.Map;
 import java.util.Set;
 import java.util.Collections;
 import java.util.HashMap;
+import java.io.IOException;
 import org.opengis.util.GenericName;
 import org.opengis.parameter.GeneralParameterDescriptor;
 import org.opengis.parameter.ParameterDescriptorGroup;
 import org.opengis.parameter.ParameterValueGroup;
-import org.apache.sis.referencing.IdentifiedObjects;
 import org.apache.sis.util.Classes;
 import org.apache.sis.util.Debug;
 
 // Branch-dependent imports
 import java.util.Objects;
 import java.util.function.BiFunction;
+import java.io.UncheckedIOException;
 import org.opengis.feature.Attribute;
 import org.opengis.feature.AttributeType;
 import org.opengis.feature.Feature;
@@ -39,6 +40,7 @@ import org.opengis.feature.FeatureOperat
 import org.opengis.feature.IdentifiedType;
 import org.opengis.feature.Operation;
 import org.opengis.feature.Property;
+import org.opengis.metadata.Identifier;
 
 
 /**
@@ -232,14 +234,6 @@ public abstract class AbstractOperation
         if (name != null) {
             buffer.append('”');
         }
-        String separator = " (";
-        for (final GeneralParameterDescriptor param : getParameters().descriptors()) {
-            buffer.append(separator).append(IdentifiedObjects.toString(param.getName()));
-            separator = ", ";
-        }
-        if (separator == ", ") {                    // Identity comparaison is okay here.
-            buffer.append(')');
-        }
         final IdentifiedType result = getResult();
         if (result != null) {
             final Object type;
@@ -250,7 +244,11 @@ public abstract class AbstractOperation
             }
             buffer.append(" : ").append(type);
         }
-        formatResultFormula(buffer.append(']'));
+        try {
+            formatResultFormula(buffer.append("] = "));
+        } catch (IOException e) {
+            throw new UncheckedIOException(e);      // Should never happen since we write
in a StringBuilder.
+        }
         return buffer.toString();
     }
 
@@ -259,9 +257,35 @@ public abstract class AbstractOperation
      * The "formula" may be for example a link to another property.
      *
      * @param  buffer  where to format the "formula".
-     * @return {@code true} if this method has formatted a formula, or {@code false} otherwise.
+     * @throws IOException if an error occurred while writing in {@code buffer}.
      */
-    boolean formatResultFormula(Appendable buffer) {
-        return false;
+    void formatResultFormula(Appendable buffer) throws IOException {
+        defaultFormula(getParameters(), buffer);
+    }
+
+    /**
+     * Default implementation of {@link #formatResultFormula(Appendable)},
+     * to be used also for operations that are not instance of {@link AbstractOperation}.
+     */
+    static void defaultFormula(final ParameterDescriptorGroup parameters, final Appendable
buffer) throws IOException {
+        buffer.append(parameters != null ? name(parameters.getName()) : "operation").append('(');
+        if (parameters != null) {
+            boolean hasMore = false;
+            for (GeneralParameterDescriptor p : parameters.descriptors()) {
+                if (p != null) {
+                    if (hasMore) buffer.append(", ");
+                    buffer.append(name(p.getName()));
+                    hasMore = true;
+                }
+            }
+        }
+        buffer.append(')');
+    }
+
+    /**
+     * Returns a short string representation of the given identifier, or {@code null} if
none.
+     */
+    private static String name(final Identifier id) {
+        return (id != null) ? id.getCode() : null;
     }
 }

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=1756007&r1=1756006&r2=1756007&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 Aug 11 16:26:45 2016
@@ -39,6 +39,7 @@ import org.apache.sis.util.resources.Voc
 import org.apache.sis.referencing.IdentifiedObjects;
 
 // Branch-dependent imports
+import java.io.UncheckedIOException;
 import org.opengis.feature.IdentifiedType;
 import org.opengis.feature.Property;
 import org.opengis.feature.PropertyType;
@@ -258,11 +259,19 @@ header: for (int i=0; ; i++) {
                 }
             } else if (propertyType instanceof AttributeType<?>) {
                 value = ((AttributeType<?>) propertyType).getDefaultValue();
-            } else if (propertyType instanceof AbstractOperation) {
-                if (((AbstractOperation) propertyType).formatResultFormula(buffer)) {
-                    value = CharSequences.trimWhitespaces(buffer).toString();
-                    buffer.setLength(0);
+            } else if (propertyType instanceof Operation) {
+                buffer.append(" = ");
+                try {
+                    if (propertyType instanceof AbstractOperation) {
+                        ((AbstractOperation) propertyType).formatResultFormula(buffer);
+                    } else {
+                        AbstractOperation.defaultFormula(((Operation) propertyType).getParameters(),
buffer);
+                    }
+                } catch (IOException e) {
+                    throw new UncheckedIOException(e);      // Should never happen since
we write in a StringBuffer.
                 }
+                value = CharSequences.trimWhitespaces(buffer).toString();
+                buffer.setLength(0);
             }
             /*
              * Column 0 - Name.
@@ -322,7 +331,7 @@ header: for (int i=0; ; i++) {
                 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();
+                        ? (Iterable<?>) value : Collections.singleton(value)).iterator();
                 String separator = "";
                 while (it.hasNext()) {
                     value = it.next();

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=1756007&r1=1756006&r2=1756007&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] Thu Aug 11 16:26:45 2016
@@ -30,7 +30,6 @@ import org.apache.sis.metadata.iso.citat
 import org.apache.sis.util.ArgumentChecks;
 
 // Branch-dependent imports
-import java.io.UncheckedIOException;
 import org.opengis.feature.Feature;
 import org.opengis.feature.IdentifiedType;
 import org.opengis.feature.Property;
@@ -158,15 +157,9 @@ final class LinkOperation extends Abstra
      * Appends a string representation of the "formula" used for computing the result.
      *
      * @param  buffer where to format the "formula".
-     * @return {@code true} since this method has formatted a formula.
      */
     @Override
-    boolean formatResultFormula(final Appendable buffer) {
-        try {
-            buffer.append(" → ").append(referentName);
-        } catch (IOException e) {
-            throw new UncheckedIOException(e);
-        }
-        return true;
+    void formatResultFormula(final Appendable buffer) throws IOException {
+        buffer.append(referentName);
     }
 }

Modified: sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/StringJoinOperation.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/StringJoinOperation.java?rev=1756007&r1=1756006&r2=1756007&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/StringJoinOperation.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/StringJoinOperation.java
[UTF-8] Thu Aug 11 16:26:45 2016
@@ -34,7 +34,6 @@ import org.apache.sis.util.Classes;
 
 // Branch-dependent imports
 import java.util.Objects;
-import java.io.UncheckedIOException;
 import org.opengis.feature.AttributeType;
 import org.opengis.feature.Feature;
 import org.opengis.feature.IdentifiedType;
@@ -428,21 +427,14 @@ final class StringJoinOperation extends
      * Appends a string representation of the "formula" used for computing the result.
      *
      * @param  buffer where to format the "formula".
-     * @return {@code true} since this method has formatted a formula.
      */
     @Override
-    boolean formatResultFormula(final Appendable buffer) {
-        try {
-            buffer.append(" → ");
-            String separator = "(";
-            for (final String element : attributeNames) {
-                buffer.append(separator).append(element);
-                separator = ", ";
-            }
-            buffer.append(')');
-        } catch (IOException e) {
-            throw new UncheckedIOException(e);
+    void formatResultFormula(final Appendable buffer) throws IOException {
+        if (prefix != null) buffer.append(prefix);
+        for (int i=0; i<attributeNames.length; i++) {
+            if (i != 0) buffer.append(delimiter);
+            buffer.append(attributeNames[i]);
         }
-        return true;
+        if (suffix != null) buffer.append(suffix);
     }
 }

Modified: sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/AbstractOperationTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/AbstractOperationTest.java?rev=1756007&r1=1756006&r2=1756007&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/AbstractOperationTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/AbstractOperationTest.java
[UTF-8] Thu Aug 11 16:26:45 2016
@@ -44,8 +44,8 @@ public final strictfp class AbstractOper
         final ParameterDescriptor<?>[] parameters = {
             builder.addName("founder").create(String.class, null)
         };
-        return new NoOperation(singletonMap(AbstractOperation.NAME_KEY, "found city"),
-                builder.addName("found city").createGroup(parameters),
+        return new NoOperation(singletonMap(AbstractOperation.NAME_KEY, "new city"),
+                builder.addName("create").createGroup(parameters),
                 DefaultAttributeTypeTest.city());
     }
 
@@ -62,6 +62,6 @@ public final strictfp class AbstractOper
      */
     @Test
     public void testToString() {
-        assertEquals("NoOperation[“found city” (founder) : String]", foundCity().toString());
+        assertEquals("NoOperation[“new city” : String] = create(founder)", foundCity().toString());
     }
 }

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=1756007&r1=1756006&r2=1756007&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 Aug 11 16:26:45 2016
@@ -17,7 +17,9 @@
 package org.apache.sis.feature;
 
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.Locale;
+import java.util.Map;
 import java.util.Random;
 import org.apache.sis.test.DependsOn;
 import org.apache.sis.test.TestCase;
@@ -26,6 +28,9 @@ import org.junit.Test;
 
 import static org.apache.sis.test.Assert.*;
 
+// Branch-dependent import
+import org.opengis.feature.PropertyType;
+
 
 /**
  * Tests {@link FeatureFormat}.
@@ -62,6 +67,40 @@ public final strictfp class FeatureForma
     }
 
     /**
+     * Tests the formatting of a {@link DefaultFeatureType} that contains operations.
+     */
+    @Test
+    @SuppressWarnings("serial")
+    public void testFeatureTypeWithOperations() {
+        DefaultFeatureType feature = DefaultFeatureTypeTest.city();
+        final PropertyType city = feature.getProperty("city");
+        feature = new DefaultFeatureType(name("Identified city"), false, new DefaultFeatureType[]
{feature},
+                FeatureOperations.link(name("someId"), city),
+                FeatureOperations.compound(name("anotherId"), ":", "<", ">", city,
feature.getProperty("population")),
+                AbstractOperationTest.foundCity());
+
+        final FeatureFormat format = new FeatureFormat(Locale.US, null);
+        final String text = format.format(feature);
+        assertMultilinesEquals("Identified city ⇾ City\n" +
+                "┌────────────┬─────────┬─────────────┬─────────────────────┐\n"
+
+                "│ Name       │ Type    │ Cardinality │ Default value       │\n"
+
+                "├────────────┼─────────┼─────────────┼─────────────────────┤\n"
+
+                "│ city       │ String  │ [1 … 1]     │ Utopia              │\n"
+
+                "│ population │ Integer │ [1 … 1]     │                     │\n"
+
+                "│ someId     │ String  │ [1 … 1]     │ = city              │\n"
+
+                "│ anotherId  │ String  │ [1 … 1]     │ = <city:population>
│\n" +
+                "│ new city   │ String  │ [1 … 1]     │ = create(founder)   │\n"
+
+                "└────────────┴─────────┴─────────────┴─────────────────────┘\n",
text);
+    }
+
+    /**
+     * Convenience method returning the given name in a {@code properties} map.
+     */
+    private static Map<String,?> name(final String name) {
+        return Collections.singletonMap(DefaultFeatureType.NAME_KEY, name);
+    }
+
+    /**
      * Tests the formatting of an {@link AbstractFeature}.
      */
     @Test
@@ -72,7 +111,7 @@ public final strictfp class FeatureForma
         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("population", 13185502);                               //
In 2011.
         feature.setPropertyValue("universities", Arrays.asList("Waseda", "Keio"));
 
         final FeatureFormat format = new FeatureFormat(Locale.US, null);
@@ -101,11 +140,11 @@ public final strictfp class FeatureForma
         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.
+        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("population", 143174);                     // December 31th,
2011
         feature.setPropertyValue("twin town", twinTown);
 
         final FeatureFormat format = new FeatureFormat(Locale.US, null);



Mime
View raw message