sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1797229 - in /sis/branches/JDK8/core/sis-feature/src: main/java/org/apache/sis/feature/DefaultAssociationRole.java main/java/org/apache/sis/feature/FeatureFormat.java test/java/org/apache/sis/feature/FeatureFormatTest.java
Date Thu, 01 Jun 2017 14:18:27 GMT
Author: desruisseaux
Date: Thu Jun  1 14:18:27 2017
New Revision: 1797229

URL: http://svn.apache.org/viewvc?rev=1797229&view=rev
Log:
Fix formatting of Feature associations and improve formatting of cardinalities.

Modified:
    sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/DefaultAssociationRole.java
    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

Modified: sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/DefaultAssociationRole.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/DefaultAssociationRole.java?rev=1797229&r1=1797228&r2=1797229&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/DefaultAssociationRole.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/DefaultAssociationRole.java
[UTF-8] Thu Jun  1 14:18:27 2017
@@ -22,6 +22,7 @@ import java.util.ArrayList;
 import java.util.IdentityHashMap;
 import org.opengis.util.GenericName;
 import org.opengis.util.InternationalString;
+import org.opengis.metadata.Identifier;
 import org.apache.sis.internal.feature.Resources;
 import org.apache.sis.util.Debug;
 
@@ -33,6 +34,7 @@ import org.opengis.feature.AttributeType
 import org.opengis.feature.FeatureType;
 import org.opengis.feature.FeatureAssociation;
 import org.opengis.feature.FeatureAssociationRole;
+import org.opengis.feature.PropertyNotFoundException;
 
 
 /**
@@ -378,35 +380,62 @@ public class DefaultAssociationRole exte
 
     /**
      * Returns the name of the property to use as a title for the associated feature, or
{@code null} if none.
-     * This method searches for the first attribute having a value class assignable to {@link
CharSequence}.
+     * This method applies the following heuristic rules:
+     *
+     * <ul>
+     *   <li>If associated feature has a property named {@code "sis:identifier"}, then
this method returns that name.</li>
+     *   <li>Otherwise if the associated feature has a mandatory property of type {@link
CharSequence}, {@link GenericName}
+     *       or {@link Identifier}, then this method returns the name of that property.</li>
+     *   <li>Otherwise if the associated feature has an optional property of type {@link
CharSequence}, {@link GenericName}
+     *       or {@link Identifier}, then this method returns the name of that property.</li>
+     *   <li>Otherwise this method returns {@code null}.</li>
+     * </ul>
+     *
+     * This method should be used only for display purpose, not as a reliable or stable way
to get the identifier.
+     * The heuristic rules implemented in this method may change in any future Apache SIS
version.
      */
     static String getTitleProperty(final FeatureAssociationRole role) {
         if (role instanceof DefaultAssociationRole) {
-            String p = ((DefaultAssociationRole) role).titleProperty; // No synchronization
- not a big deal if computed twice.
+            String p = ((DefaultAssociationRole) role).titleProperty;       // No synchronization
- not a big deal if computed twice.
             if (p != null) {
                 return p.isEmpty() ? null : p;
             }
-            p = searchTitleProperty(role);
+            p = searchTitleProperty(role.getValueType());
             ((DefaultAssociationRole) role).titleProperty = (p != null) ? p : "";
             return p;
         }
-        return searchTitleProperty(role);
+        return searchTitleProperty(role.getValueType());
     }
 
     /**
      * Implementation of {@link #getTitleProperty(FeatureAssociationRole)} for first search,
      * or for non-SIS {@code FeatureAssociationRole} implementations.
      */
-    private static String searchTitleProperty(final FeatureAssociationRole role) {
-        for (final PropertyType type : role.getValueType().getProperties(true)) {
+    private static String searchTitleProperty(final FeatureType ft) {
+        String fallback = null;
+        try {
+            return ft.getProperty("sis:identifier").getName().toString();
+        } catch (PropertyNotFoundException e) {
+            // Ignore.
+        }
+        for (final PropertyType type : ft.getProperties(true)) {
             if (type instanceof AttributeType<?>) {
                 final AttributeType<?> pt = (AttributeType<?>) type;
-                if (pt.getMaximumOccurs() != 0 && CharSequence.class.isAssignableFrom(pt.getValueClass()))
{
-                    return pt.getName().toString();
+                final Class<?> valueClass = pt.getValueClass();
+                if (CharSequence.class.isAssignableFrom(valueClass) ||
+                    GenericName .class.isAssignableFrom(valueClass) ||
+                    Identifier.class.isAssignableFrom(valueClass))
+                {
+                    final String name = pt.getName().toString();
+                    if (pt.getMaximumOccurs() != 0) {
+                        return name;
+                    } else if (fallback == null) {
+                        fallback = name;
+                    }
                 }
             }
         }
-        return null;
+        return fallback;
     }
 
     /**

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=1797229&r1=1797228&r2=1797229&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  1 14:18:27 2017
@@ -21,6 +21,7 @@ import java.util.ArrayList;
 import java.util.Set;
 import java.util.EnumSet;
 import java.util.Iterator;
+import java.util.Collection;
 import java.util.Locale;
 import java.util.TimeZone;
 import java.io.IOException;
@@ -35,6 +36,7 @@ import org.opengis.util.GenericName;
 import org.apache.sis.io.TableAppender;
 import org.apache.sis.io.TabularFormat;
 import org.apache.sis.util.Deprecable;
+import org.apache.sis.util.Characters;
 import org.apache.sis.util.CharSequences;
 import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.util.resources.Errors;
@@ -54,7 +56,6 @@ import org.opengis.feature.FeatureType;
 import org.opengis.feature.FeatureAssociation;
 import org.opengis.feature.FeatureAssociationRole;
 import org.opengis.feature.Operation;
-import org.apache.sis.util.Characters;
 
 
 /**
@@ -374,6 +375,7 @@ public class FeatureFormat extends Tabul
         final List<String>  remarks = new ArrayList<>();
         for (final PropertyType propertyType : featureType.getProperties(true)) {
             Object value = null;
+            int cardinality = -1;
             if (feature != null) {
                 if (!(propertyType instanceof AttributeType<?>) &&
                     !(propertyType instanceof FeatureAssociationRole) &&
@@ -383,16 +385,21 @@ public class FeatureFormat extends Tabul
                 }
                 value = feature.getPropertyValue(propertyType.getName().toString());
                 if (value == null) {
-                    if (propertyType instanceof AttributeType &&
-                            ((AttributeType) propertyType).getMinimumOccurs() == 0)
+                    if (propertyType instanceof AttributeType
+                            && ((AttributeType) propertyType).getMinimumOccurs()
== 0)
                     {
-                        continue;                                       // If no value, skip
the full row.
+                        continue;                           // If optional and no value,
skip the full row.
                     }
-                    if (propertyType instanceof FeatureAssociationRole &&
-                            ((FeatureAssociationRole) propertyType).getMinimumOccurs() ==
0)
+                    if (propertyType instanceof FeatureAssociationRole
+                            && ((FeatureAssociationRole) propertyType).getMinimumOccurs()
== 0)
                     {
-                        continue;                                       // If no value, skip
the full row.
+                        continue;                           // If optional and no value,
skip the full row.
                     }
+                    cardinality = 0;
+                } else if (value instanceof Collection<?>) {
+                    cardinality = ((Collection<?>) value).size();
+                } else {
+                    cardinality = 1;
                 }
             } else if (propertyType instanceof AttributeType<?>) {
                 value = ((AttributeType<?>) propertyType).getDefaultValue();
@@ -461,7 +468,17 @@ public class FeatureFormat extends Tabul
                         break;
                     }
                     case CARDINALITY: {
+                        table.setCellAlignment(TableAppender.ALIGN_RIGHT);
+                        if (cardinality >= 0) {
+                            table.append(getFormat(Integer.class).format(cardinality, buffer,
dummyFP));
+                            buffer.setLength(0);
+                        }
                         if (maximumOccurs >= 0) {
+                            if (cardinality >= 0) {
+                                table.append(' ')
+                                     .append((cardinality >= minimumOccurs &&
cardinality <= maximumOccurs) ? '∈' : '∉')
+                                     .append(' ');
+                            }
                             final Format format = getFormat(Integer.class);
                             table.append('[').append(format.format(minimumOccurs, buffer,
dummyFP)).append(" … ");
                             buffer.setLength(0);
@@ -476,26 +493,28 @@ public class FeatureFormat extends Tabul
                         break;
                     }
                     case VALUE: {
-                        final Format format = getFormat(valueClass);                    
   // Null if valueClass is null.
+                        table.setCellAlignment(TableAppender.ALIGN_LEFT);
+                        final Format format = getFormat(valueClass);                    
       // Null if valueClass is null.
                         final Iterator<?> it = CollectionsExt.toCollection(value).iterator();
                         String separator = "";
                         int length = 0;
                         while (it.hasNext()) {
                             value = it.next();
                             if (value != null) {
-                                if (format != null && valueClass.isInstance(value))
{       // Null safe of getFormat(valueClass) contract.
-                                    value = format.format(value, buffer, dummyFP);
-                                } else if (value instanceof Feature && propertyType
instanceof FeatureAssociationRole) {
+                                if (propertyType instanceof FeatureAssociationRole) {
                                     final String p = DefaultAssociationRole.getTitleProperty((FeatureAssociationRole)
propertyType);
                                     if (p != null) {
                                         value = ((Feature) value).getPropertyValue(p);
                                         if (value == null) continue;
                                     }
+                                } else if (format != null && valueClass.isInstance(value))
{    // Null safe because of getFormat(valueClass) contract.
+                                    value = format.format(value, buffer, dummyFP);
                                 }
                                 length = formatValue(value, table.append(separator), length);
                                 buffer.setLength(0);
-                                separator = ", ";
                                 if (length < 0) break;      // Value is too long, abandon
remaining iterations.
+                                separator = ", ";
+                                length += 2;
                             }
                         }
                         break;

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=1797229&r1=1797228&r2=1797229&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  1 14:18:27 2017
@@ -70,12 +70,12 @@ public final strictfp class FeatureForma
                 "┌──────────────┬─────────────────────┬─────────────┬───────────────┬────────────────────────────┐\n"
+
                 "│ Name         │ Type                │ Cardinality │ Default value
│ Characteristics            │\n" +
                 "├──────────────┼─────────────────────┼─────────────┼───────────────┼────────────────────────────┤\n"
+
-                "│ city         │ String              │ [1 … 1]     │ Utopia  
     │                            │\n" +
-                "│ population   │ Integer             │ [1 … 1]     │         
     │                            │\n" +
-                "│ region       │ InternationalString │ [1 … 1]     │         
     │                            │\n" +
-                "│ isGlobal     │ Boolean             │ [1 … 1]     │         
     │                            │\n" +
-                "│ universities │ String              │ [0 … ∞]     │       
       │                            │\n" +
-                "│ temperature  │ Float               │ [1 … 1]     │         
     │ accuracy = 0.1, units = °C │\n" +
+                "│ city         │ String              │     [1 … 1] │ Utopia  
     │                            │\n" +
+                "│ population   │ Integer             │     [1 … 1] │         
     │                            │\n" +
+                "│ region       │ InternationalString │     [1 … 1] │         
     │                            │\n" +
+                "│ isGlobal     │ Boolean             │     [1 … 1] │         
     │                            │\n" +
+                "│ universities │ String              │     [0 … ∞] │       
       │                            │\n" +
+                "│ temperature  │ Float               │     [1 … 1] │         
     │ accuracy = 0.1, units = °C │\n" +
                 "└──────────────┴─────────────────────┴─────────────┴───────────────┴────────────────────────────┘\n",
text);
     }
 
@@ -98,11 +98,11 @@ public final strictfp class FeatureForma
                 "┌────────────┬─────────┬─────────────┬─────────────────────┐\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"
+
+                "│ 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);
     }
 
@@ -132,9 +132,9 @@ public final strictfp class FeatureForma
                 "┌────────────┬─────────┬─────────────┬───────────────┬─────────────┐\n"
+
                 "│ Name       │ Type    │ Cardinality │ Default value │ Remarks
    │\n" +
                 "├────────────┼─────────┼─────────────┼───────────────┼─────────────┤\n"
+
-                "│ city       │ String  │ [1 … 1]     │ Utopia        │     
       │\n" +
-                "│ population │ Integer │ [1 … 1]     │               │     
       │\n" +
-                "│ highway    │ String  │ [0 … 2]     │               │ Deprecated¹
│\n" +
+                "│ city       │ String  │     [1 … 1] │ Utopia        │     
       │\n" +
+                "│ population │ Integer │     [1 … 1] │               │     
       │\n" +
+                "│ highway    │ String  │     [0 … 2] │               │ Deprecated¹
│\n" +
                 "└────────────┴─────────┴─────────────┴───────────────┴─────────────┘\n"
+
                 "¹ Replaced by pedestrian areas.\n", text);
     }
@@ -159,12 +159,12 @@ public final strictfp class FeatureForma
                 "┌──────────────┬─────────────────────┬─────────────┬──────────────┬─────────────────┐\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" +
+                "│ city         │ String              │ 1 ∈ [1 … 1] │ Tokyo 
      │                 │\n" +
+                "│ population   │ Integer             │ 1 ∈ [1 … 1] │ 13,185,502
  │                 │\n" +
+                "│ region       │ InternationalString │ 0 ∉ [1 … 1] │       
      │                 │\n" +
+                "│ isGlobal     │ Boolean             │ 0 ∉ [1 … 1] │       
      │                 │\n" +
+                "│ universities │ String              │ 2 ∈ [0 … ∞] │ Waseda,
Keio │                 │\n" +
+                "│ temperature  │ Float               │ 0 ∉ [1 … 1] │       
      │ accuracy, units │\n" +
                 "└──────────────┴─────────────────────┴─────────────┴──────────────┴─────────────────┘\n",
text);
     }
 
@@ -192,9 +192,9 @@ public final strictfp class FeatureForma
                 "┌────────────┬─────────┬─────────────┬───────────┐\n"
+
                 "│ Name       │ Type    │ Cardinality │ Value     │\n" +
                 "├────────────┼─────────┼─────────────┼───────────┤\n"
+
-                "│ city       │ String  │ [1 … 1]     │ Paderborn │\n" +
-                "│ population │ Integer │ [1 … 1]     │ 143,174   │\n" +
-                "│ twin town  │ City    │ [0 … 1]     │ Le Mans   │\n" +
+                "│ city       │ String  │ 1 ∈ [1 … 1] │ Paderborn │\n" +
+                "│ population │ Integer │ 1 ∈ [1 … 1] │ 143,174   │\n" +
+                "│ twin town  │ City    │ 1 ∈ [0 … 1] │ Le Mans   │\n" +
                 "└────────────┴─────────┴─────────────┴───────────┘\n",
text);
     }
 }



Mime
View raw message