sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1760275 - in /sis/branches/JDK8/core: sis-feature/src/main/java/org/apache/sis/feature/FeatureFormat.java sis-utility/src/main/java/org/apache/sis/internal/util/CollectionsExt.java
Date Sun, 11 Sep 2016 18:57:41 GMT
Author: desruisseaux
Date: Sun Sep 11 18:57:41 2016
New Revision: 1760275

URL: http://svn.apache.org/viewvc?rev=1760275&view=rev
Log:
FeatureFormat should be able to format array too.
This implies that we should limit the maximal length of values to format.

Modified:
    sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/FeatureFormat.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/CollectionsExt.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=1760275&r1=1760274&r2=1760275&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] Sun Sep 11 18:57:41 2016
@@ -16,8 +16,6 @@
  */
 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;
@@ -36,6 +34,7 @@ import org.apache.sis.util.CharSequences
 import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.util.resources.Errors;
 import org.apache.sis.util.resources.Vocabulary;
+import org.apache.sis.internal.util.CollectionsExt;
 import org.apache.sis.referencing.IdentifiedObjects;
 
 // Branch-dependent imports
@@ -96,6 +95,16 @@ public class FeatureFormat extends Tabul
     private final Locale displayLocale;
 
     /**
+     * Maximal length of attribute values, in number of characters.
+     * If a value is longer than this length, it will be truncated.
+     *
+     * <p>This is defined as a static final variable for now because its value is approximative:
+     * it is a number of characters instead than a number of code points, and that length
may be
+     * exceeded by a few characters if the overflow happen while appending the list separator.</p>
+     */
+    private static final int MAXIMAL_VALUE_LENGTH = 40;
+
+    /**
      * Creates a new formatter for the default locale and timezone.
      */
     public FeatureFormat() {
@@ -328,15 +337,14 @@ header: for (int i=0; ; i++) {
              * Column 3 - Value or default value.
              */
             if (value != null) {
-                final boolean isInstance = valueClass != null && valueClass.isInstance(value);
-                final Format format = isInstance ? getFormat(valueClass) : null;
-                final Iterator<?> it = (!isInstance && (value instanceof Collection<?>)
-                        ? (Iterable<?>) value : Collections.singleton(value)).iterator();
+                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) {
+                        if (format != null && valueClass.isInstance(value)) {
                             value = format.format(value, buffer, dummyFP);
                         } else if (value instanceof Feature && propertyType instanceof
FeatureAssociationRole) {
                             final String p = DefaultAssociationRole.getTitleProperty((FeatureAssociationRole)
propertyType);
@@ -345,9 +353,10 @@ header: for (int i=0; ; i++) {
                                 if (value == null) continue;
                             }
                         }
-                        table.append(separator).append(formatValue(value));
+                        length = formatValue(value, table.append(separator), length);
                         buffer.setLength(0);
                         separator = ", ";
+                        if (length < 0) break;      // Value is too long, abandon remaining
iterations.
                     }
                 }
             }
@@ -368,7 +377,7 @@ header: for (int i=0; ; i++) {
                             }
                         }
                         if (c != null) {
-                            table.append(" = ").append(formatValue(c));
+                            formatValue(c, table.append(" = "), 0);
                         }
                         separator = ", ";
                     }
@@ -398,19 +407,35 @@ header: for (int i=0; ; i++) {
     }
 
     /**
-     * Formats the given attribute value.
+     * Appends the given attribute value, in a truncated form if it exceed the maximal value
length.
+     *
+     * @param  value   the value to append.
+     * @param  table   where to append the value.
+     * @param  length  number of characters appended before this method call in the current
table cell.
+     * @return number of characters appended after this method call in the current table
cell, or -1 if
+     *         the length exceed the maximal length (in which case the caller should break
iteration).
      */
-    private String formatValue(final Object value) {
+    private int formatValue(final Object value, final TableAppender table, final int length)
{
+        final String text;
         if (value instanceof InternationalString) {
-            return ((InternationalString) value).toString(displayLocale);
+            text = ((InternationalString) value).toString(displayLocale);
         } else if (value instanceof GenericName) {
-            return toString((GenericName) value);
+            text = toString((GenericName) value);
         } else if (value instanceof IdentifiedType) {
-            return toString(((IdentifiedType) value).getName());
+            text = toString(((IdentifiedType) value).getName());
         } else if (value instanceof IdentifiedObject) {
-            return IdentifiedObjects.getIdentifierOrName((IdentifiedObject) value);
+            text = IdentifiedObjects.getIdentifierOrName((IdentifiedObject) value);
+        } else {
+            text = value.toString();
+        }
+        final int remaining = MAXIMAL_VALUE_LENGTH - length;
+        if (remaining >= text.length()) {
+            table.append(text);
+            return length + text.length();
+        } else {
+            table.append(text, 0, Math.max(0, remaining - 1)).append('…');
+            return -1;
         }
-        return value.toString();
     }
 
     /**

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/CollectionsExt.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/CollectionsExt.java?rev=1760275&r1=1760274&r2=1760275&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/CollectionsExt.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/CollectionsExt.java
[UTF-8] Sun Sep 11 18:57:41 2016
@@ -547,6 +547,7 @@ public final class CollectionsExt extend
      *   <li>If the value is null, then this method returns an {@linkplain Collections#emptyList()
empty list}.</li>
      *   <li>If the value is an instance of {@link Collection}, then it is returned
unchanged.</li>
      *   <li>If the value is an array of objects, then it is returned {@linkplain Arrays#asList(Object[])
as a list}.</li>
+     *   <li>If the value is an array of primitive type, then it is returned as a list
of their wrapper class.</li>
      *   <li>If the value is an instance of {@link Iterable}, {@link Iterator} or {@link
Enumeration}, copies the values in a new list.</li>
      *   <li>Otherwise the value is returned as a {@linkplain Collections#singletonList(Object)
singleton list}.</li>
      * </ul>
@@ -571,8 +572,29 @@ public final class CollectionsExt extend
         if (value instanceof Collection<?>) {
             return (Collection<?>) value;
         }
-        if (value instanceof Object[]) {
-            return Arrays.asList((Object[]) value);
+        if (value.getClass().isArray()) {
+            if (value instanceof Object[]) {
+                return Arrays.asList((Object[]) value);
+            } else {
+                return new AbstractList<Object>() {
+                    /** Returns the number of elements in the backing array. */
+                    @Override public int size() {
+                        return Array.getLength(value);
+                    }
+
+                    /** Returns the element at the given index. Primitive numbers as returned
as instance of their wrapper class. */
+                    @Override public Object get(final int index) {
+                        return Array.get(value, index);
+                    }
+
+                    /** Sets the element at the given index. Primitive numbers shall be given
as instance of their wrapper class. */
+                    @Override public Object set(final int index, final Object value) {
+                        final Object old = Array.get(value, index);
+                        Array.set(value, index, value);
+                        return old;
+                    }
+                };
+            }
         }
         if (value instanceof Iterable<?>) {
             final List<Object> list = new ArrayList<>();



Mime
View raw message