sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1477685 - in /sis/branches/JDK7/sis-utility/src: main/java/org/apache/sis/util/ main/java/org/apache/sis/util/collection/ test/java/org/apache/sis/util/collection/
Date Tue, 30 Apr 2013 15:22:44 GMT
Author: desruisseaux
Date: Tue Apr 30 15:22:43 2013
New Revision: 1477685

URL: http://svn.apache.org/r1477685
Log:
Relaxed the return type of TreeTable.Node.getChildren() from List to Collection.
The reason is that most usage (except widgets) doesn't need a List, and the List
contract is difficult to meet in the metadata tree table views.

Modified:
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/About.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/DefaultTreeTable.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/TreeTable.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/TreeTableFormat.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/TreeTables.java
    sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/DefaultTreeTableTest.java
    sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/TreeTablesTest.java

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/About.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/About.java?rev=1477685&r1=1477684&r2=1477685&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/About.java [UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/About.java [UTF-8] Tue
Apr 30 15:22:43 2013
@@ -597,15 +597,17 @@ pathTree:   for (int j=0; ; j++) {
      * node but lost all value of the parent node. For this reason, we perform the merge
only if the
      * parent has no value.
      *
-     * <p>See the "<cite>Reduce the depth of a tree</cite>" example in
{@link TreeTables}
-     * for more information.</p>
+     * <p>See the "<cite>Reduce the depth of a tree</cite>" example in
{@link TreeTables} for more information.
+     * In particular, note that this implementation assumes that children collections are
{@link List} (this is
+     * guaranteed for {@link DefaultTreeTable.Node} implementations).</p>
      *
      * @param  node The root of the node to simplify.
      * @param  skip {@code true} for disabling concatenation of root node.
      * @return The root of the simplified tree. May be the given {@code node} or a child.
      */
     private static TreeTable.Node concatenateSingletons(final TreeTable.Node node, final
boolean skip) {
-        final List<TreeTable.Node> children = node.getChildren();
+        // DefaultTreeTable.Node instances are known to handle their children in a List.
+        final List<TreeTable.Node> children = (List<TreeTable.Node>) node.getChildren();
         final int size = children.size();
         for (int i=0; i<size; i++) {
             children.set(i, concatenateSingletons(children.get(i), false));

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/DefaultTreeTable.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/DefaultTreeTable.java?rev=1477685&r1=1477684&r2=1477685&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/DefaultTreeTable.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/DefaultTreeTable.java
[UTF-8] Tue Apr 30 15:22:43 2013
@@ -20,6 +20,7 @@ import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
 import java.util.LinkedHashMap;
+import java.util.Collection;
 import java.util.Collections;
 import java.io.Serializable;
 import net.jcip.annotations.NotThreadSafe;
@@ -756,7 +757,10 @@ public class DefaultTreeTable implements
             }
             String name = getClass().getSimpleName();
             if (parent != null) {
-                name = name + '-' + parent.getChildren().indexOf(this);
+                final Collection<TreeTable.Node> children = parent.getChildren();
+                if (children instanceof List<?>) {
+                    name = name + '-' + ((List<TreeTable.Node>) children).indexOf(this);
+                }
             }
             return name;
         }

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/TreeTable.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/TreeTable.java?rev=1477685&r1=1477684&r2=1477685&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/TreeTable.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/TreeTable.java
[UTF-8] Tue Apr 30 15:22:43 2013
@@ -16,6 +16,7 @@
  */
 package org.apache.sis.util.collection;
 
+import java.util.Collection;
 import java.util.List;
 
 
@@ -124,10 +125,10 @@ public interface TreeTable {
         /**
          * Returns the parent node, or {@code null} if this node is the root of the tree.
          *
-         * <p>There is intentionally no {@code setParent(Node)} method, as children
and parent
-         * managements are highly implementation-dependant. If the {@linkplain #getChildren()
-         * children list} is modifiable, then implementations are encouraged to update automatically
-         * the parent when a child is <em>added to</em> or <em>removed
from</em> the children list.</p>
+         * <p>There is intentionally no {@code setParent(Node)} method, as children
and parent managements
+         * are highly implementation-dependant. If the {@linkplain #getChildren() children
collection} is
+         * modifiable, then implementations are encouraged to update automatically the parent
when a child
+         * is <em>added to</em> or <em>removed from</em> that collection.</p>
          *
          * @return The parent, or {@code null} if none.
          * @category tree
@@ -135,8 +136,8 @@ public interface TreeTable {
         Node getParent();
 
         /**
-         * Returns {@code true} if this node can not have any children. The {@linkplain #getChildren()
-         * children list} of a leaf node can only be empty, and adding {@linkplain #newChild()
new child}
+         * Returns {@code true} if this node can not have any children. The {@linkplain #getChildren()
children
+         * collection} of a leaf node can only be empty, and adding {@linkplain #newChild()
new child}
          * is an unsupported operation.
          *
          * <p>This value is provided as a tip for graphical user interfaces, in order
to determine if
@@ -147,9 +148,9 @@ public interface TreeTable {
         boolean isLeaf();
 
         /**
-         * Returns the children of this node. The returned list may or may not be modifiable,
at
-         * implementation choice. If the list is modifiable, then it shall be <cite>live</cite>,
-         * i.e. any modification to the returned list are reflected immediately in the tree.
+         * Returns the children of this node. The returned collection may or may not be modifiable,
at
+         * implementation choice. If the collection is modifiable, then it shall be <cite>live</cite>,
+         * i.e. any modification to the returned collection are reflected immediately in
the tree.
          * This allows addition or removal of child nodes as below:
          *
          * {@preformat java
@@ -157,15 +158,19 @@ public interface TreeTable {
          *     parent.getChildren().add(newNode);
          * }
          *
-         * @return The children, or an empty list if none.
+         * The collection is often a {@link List}, but not necessarily. For some implementations
like the
+         * {@linkplain org.apache.sis.metadata.AbstractMetadata#asTreeTable() metadata tree
table view},
+         * compliance to the {@code List} contract is impractical or inefficient.
+         *
+         * @return The children, or an empty collection if none.
          * @category tree
          */
-        List<Node> getChildren();
+        Collection<Node> getChildren();
 
         /**
-         * Creates a new child with the same columns than the other children, and add it
to
-         * the {@linkplain #getChildren() children list}. The new child is typically added
at
-         * the end of the list, but this is not mandatory: implementations can add the child
+         * Creates a new child with the same columns than the other children, and adds it
to
+         * the {@linkplain #getChildren() children collection}. The new child is typically
added at
+         * the end of the collection, but this is not mandatory: implementations can add
the child
          * at whatever position they see fit.
          *
          * @return The new child.

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/TreeTableFormat.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/TreeTableFormat.java?rev=1477685&r1=1477684&r2=1477685&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/TreeTableFormat.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/TreeTableFormat.java
[UTF-8] Tue Apr 30 15:22:43 2013
@@ -17,6 +17,7 @@
 package org.apache.sis.util.collection;
 
 import java.util.Arrays;
+import java.util.Iterator;
 import java.util.Map;
 import java.util.List;
 import java.util.Locale;
@@ -623,11 +624,13 @@ public class TreeTableFormat extends Tab
             if (level >= isLast.length) {
                 isLast = Arrays.copyOf(isLast, level*2);
             }
-            final List<? extends TreeTable.Node> children = node.getChildren();
-            final int count = children.size();
-            for (int i=0; i<count; i++) {
-                isLast[level] = (i == count-1);
-                format(children.get(i), level+1);
+            final Iterator<? extends TreeTable.Node> it = node.getChildren().iterator();
+            boolean hasNext = it.hasNext();
+            while (hasNext) {
+                final TreeTable.Node child = it.next();
+                hasNext = it.hasNext();
+                isLast[level] = !hasNext; // Must be set before the call to 'format' below.
+                format(child, level+1);
             }
         }
     }

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/TreeTables.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/TreeTables.java?rev=1477685&r1=1477684&r2=1477685&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/TreeTables.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/TreeTables.java
[UTF-8] Tue Apr 30 15:22:43 2013
@@ -66,16 +66,19 @@ import org.apache.sis.util.ArgumentCheck
  * </td></tr></table>
  * There is no pre-defined method for this task because there is too many parameters that
  * developers may want to customize (columns to merge, conditions for accepting the merge,
- * kind of objects to merge, name separator, <i>etc.</i>). The following code
snippet concatenates
- * the {@code NAME} column only if the {@code VALUE} column has no value (for avoiding data
lost
- * when the node is discarded) and use the system file separator as name separator:
+ * kind of objects to merge, name separator, <i>etc.</i>). In the following code
snippet,
+ * the content of the {@code NAME} columns are concatenated only if the {@code VALUE} column
+ * has no value (for avoiding data lost when the node is discarded) and use the system file
+ * separator as name separator:
  *
  * {@preformat java
  *     final TableColumn columnToProtect = TableColumn.VALUE;
  *     final TableColumn columnToConcatenate = TableColumn.NAME;
  *
  *     TreeTable.Node concatenateSingletons(final TreeTable.Node node) {
- *         final List<TreeTable.Node> children = node.getChildren();
+ *         // This simple example is restricted to nodes which are known to handle
+ *         // their children in a list instead than some other kind of collection.
+ *         final List<TreeTable.Node> children = (List<TreeTable.Node>) node.getChildren();
  *         final int size = children.size();
  *         for (int i=0; i<size; i++) {
  *             children.set(i, concatenateSingletons(children.get(i)));
@@ -111,7 +114,7 @@ public final class TreeTables extends St
     /**
      * Finds the node for the given path, or creates a new node if none exists.
      * First, this method searches in the node {@linkplain TreeTable.Node#getChildren()
-     * children list} for the root element of the given path. If no such node is found,
+     * children collection} for the root element of the given path. If no such node is found,
      * a {@linkplain TreeTable.Node#newChild() new child} is created. Then this method
      * repeats the process (searching in the children of the child for the second path
      * element), until the last path element is reached.

Modified: sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/DefaultTreeTableTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/DefaultTreeTableTest.java?rev=1477685&r1=1477684&r2=1477685&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/DefaultTreeTableTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/DefaultTreeTableTest.java
[UTF-8] Tue Apr 30 15:22:43 2013
@@ -17,6 +17,7 @@
 package org.apache.sis.util.collection;
 
 import java.util.List;
+import java.util.Collection;
 import org.junit.Test;
 import org.apache.sis.test.TestCase;
 import org.apache.sis.test.TestStep;
@@ -114,7 +115,7 @@ public final strictfp class DefaultTreeT
      */
     @TestStep
     private static void testNodeDisplacement(final TreeTable.Node root) {
-        final List<TreeTable.Node> rootChildren, nodeChildren;
+        final Collection<TreeTable.Node> rootChildren, nodeChildren;
         final TreeTable.Node node1 = getSingleton(rootChildren = root .getChildren());
         final TreeTable.Node node2 = getSingleton(nodeChildren = node1.getChildren());
         try {
@@ -149,7 +150,7 @@ public final strictfp class DefaultTreeT
         assertNotSame("clone", table, newTable);
         assertEquals("newTable.equals(table)", table, newTable);
         assertEquals("hashCode", table.hashCode(), newTable.hashCode());
-        newTable.getRoot().getChildren().get(1).setValue(NAME, "New name");
+        getChildrenList(newTable).get(1).setValue(NAME, "New name");
         assertFalse("newTable.equals(table)", newTable.equals(table));
     }
 
@@ -162,11 +163,22 @@ public final strictfp class DefaultTreeT
     @TestStep
     private void testSerialization(final TreeTable table) {
         final TreeTable newTable = assertSerializedEquals(table);
-        newTable.getRoot().getChildren().get(1).setValue(NAME, "New name");
+        getChildrenList(newTable).get(1).setValue(NAME, "New name");
         assertFalse("newTable.equals(table)", newTable.equals(table));
     }
 
     /**
+     * Returns the children of the root of the given table as a list.
+     * Instances of {@link DefaultTreeTable.Node} shall be guaranteed
+     * to store their children in a list.
+     */
+    private static List<TreeTable.Node> getChildrenList(final TreeTable table) {
+        final Collection<TreeTable.Node> children = table.getRoot().getChildren();
+        assertInstanceOf("TreeTable.Node.getChildren()", List.class, children);
+        return (List<TreeTable.Node>) children;
+    }
+
+    /**
      * Tests the creation of a tree table with a few nodes, and tests the displacement of
a node
      * from one branch to another. Finally tests the serialization of that table and the
comparison
      * with the original object.

Modified: sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/TreeTablesTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/TreeTablesTest.java?rev=1477685&r1=1477684&r2=1477685&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/TreeTablesTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/TreeTablesTest.java
[UTF-8] Tue Apr 30 15:22:43 2013
@@ -45,12 +45,13 @@ import static org.apache.sis.util.collec
 public final strictfp class TreeTablesTest extends TestCase {
     /**
      * The {@code concatenateSingletons(…)} example documented in the {@link TreeTables}
class javadoc.
+     * This simple code assumes that the children collection in the given node is a {@link
List}.
      *
      * @param  node The root of the node to simplify.
      * @return The root of the simplified tree. May be the given {@code node} or a child.
      */
     public static TreeTable.Node concatenateSingletons(final TreeTable.Node node) {
-        final List<TreeTable.Node> children = node.getChildren();
+        final List<TreeTable.Node> children = (List<TreeTable.Node>) node.getChildren();
         final int size = children.size();
         for (int i=0; i<size; i++) {
             children.set(i, concatenateSingletons(children.get(i)));



Mime
View raw message