sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1409099 - in /sis/branches/JDK7/sis-utility/src: main/java/org/apache/sis/internal/util/ColumnConstant.java main/java/org/apache/sis/util/collection/DefaultTreeTable.java test/java/org/apache/sis/util/collection/DefaultTreeTableTest.java
Date Wed, 14 Nov 2012 06:54:36 GMT
Author: desruisseaux
Date: Wed Nov 14 06:54:35 2012
New Revision: 1409099

URL: http://svn.apache.org/viewvc?rev=1409099&view=rev
Log:
Added a test case for node values, and store column indices in a more compact way in the very
common case
where there is only one column in the table. Provide a constant in ColumnConstant for the
most common set
of columns. This constant will be used by TreeTableFormat.

Modified:
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/ColumnConstant.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/DefaultTreeTable.java
    sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/DefaultTreeTableTest.java

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/ColumnConstant.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/ColumnConstant.java?rev=1409099&r1=1409098&r2=1409099&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/ColumnConstant.java
(original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/ColumnConstant.java
Wed Nov 14 06:54:35 2012
@@ -16,6 +16,8 @@
  */
 package org.apache.sis.internal.util;
 
+import java.util.Map;
+import java.util.Collections;
 import java.io.Serializable;
 import java.io.InvalidObjectException;
 import org.opengis.util.InternationalString;
@@ -57,6 +59,12 @@ public final class ColumnConstant<T> imp
             (Class) Class.class, Vocabulary.Keys.Type);
 
     /**
+     * The default set of columns when parsing a table tree.
+     */
+    public static final Map<TableColumn<?>,Integer> PARSING =
+            Collections.<TableColumn<?>,Integer>singletonMap(NAME, 0);
+
+    /**
      * The programmatic name of the static final field holding this constant.
      */
     private final String name;

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=1409099&r1=1409098&r2=1409099&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/DefaultTreeTable.java
(original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/DefaultTreeTable.java
Wed Nov 14 06:54:35 2012
@@ -16,15 +16,17 @@
  */
 package org.apache.sis.util.collection;
 
-import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
-import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Collections;
 import java.io.Serializable;
 import net.jcip.annotations.NotThreadSafe;
 import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.util.resources.Errors;
 
+import static org.apache.sis.util.collection.Collections.hashMapCapacity;
+
 
 /**
  * A {@link TreeTable} implementation with a {@linkplain #getColumns() list of columns} given
at
@@ -97,6 +99,13 @@ public class DefaultTreeTable implements
     final Map<TableColumn<?>,Integer> columnIndex;
 
     /**
+     * Creates a new table using the given columns.
+     */
+    DefaultTreeTable(final Map<TableColumn<?>,Integer> columnIndex) {
+        this.columnIndex = columnIndex;
+    }
+
+    /**
      * Creates a new tree table with the given columns. The given array shall not be null
or
      * empty, and shall not contain null or duplicated elements.
      *
@@ -134,12 +143,20 @@ public class DefaultTreeTable implements
      * @param  columns The list of columns.
      * @return The map of column indices.
      */
-    static Map<TableColumn<?>,Integer> createColumnIndex(final TableColumn<?>...
columns) {
-        final Map<TableColumn<?>,Integer> map = new HashMap<>(Collections.hashMapCapacity(columns.length));
+    static Map<TableColumn<?>,Integer> createColumnIndex(final TableColumn<?>[]
columns) {
+        Map<TableColumn<?>,Integer> map;
+        switch (columns.length) {
+            case 0:  map = Collections.emptyMap(); break;
+            case 1:  map = null; break; // Will be created inside the loop (common case).
+            default: map = new LinkedHashMap<>(hashMapCapacity(columns.length)); break;
+        }
         for (int i=0; i<columns.length; i++) {
             ArgumentChecks.ensureNonNull("columns", i, columns);
             final TableColumn<?> column = columns[i];
-            if (map.put(column, i) != null) {
+            final Integer pos = i;
+            if (map == null) {
+                map = Collections.<TableColumn<?>,Integer>singletonMap(column,
pos);
+            } else if (map.put(column, pos) != null) {
                 throw new IllegalArgumentException(Errors.format(Errors.Keys.DuplicatedValue_1,
column));
             }
         }
@@ -148,17 +165,10 @@ public class DefaultTreeTable implements
 
     /**
      * Returns all columns in the given map, sorted by increasing index value.
+     * This method relies on {@link LinkedHashSet} preserving insertion order.
      */
     static TableColumn<?>[] getColumns(final Map<TableColumn<?>,Integer>
columnIndex) {
-        @SuppressWarnings({"unchecked","rawtypes"})
-        final Map.Entry<TableColumn<?>,Integer>[] entries =
-                columnIndex.entrySet().toArray(new Map.Entry[columnIndex.size()]);
-        Arrays.sort(entries, Collections.<TableColumn<?>,Integer>valueComparator());
-        final TableColumn<?>[] columns = new TableColumn<?>[entries.length];
-        for (int i=0; i<columns.length; i++) {
-            columns[i] = entries[i].getKey();
-        }
-        return columns;
+        return columnIndex.keySet().toArray(new TableColumn<?>[columnIndex.size()]);
     }
 
     /**

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=1409099&r1=1409098&r2=1409099&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/DefaultTreeTableTest.java
(original)
+++ sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/util/collection/DefaultTreeTableTest.java
Wed Nov 14 06:54:35 2012
@@ -27,7 +27,7 @@ import static org.apache.sis.internal.ut
 
 
 /**
- * Tests the {@link DefaultTreeTable} class.
+ * Tests the {@link DefaultTreeTable} class and its {@code Node} inner class.
  * This will also test indirectly the {@link TreeNodeList} class.
  *
  * @author  Martin Desruisseaux (Geomatys)
@@ -146,4 +146,19 @@ public final strictfp class DefaultTreeT
     public void testTreeTableCreation() {
         testNodeDisplacement(testNodeCreation(testTableCreation()));
     }
+
+    /**
+     * Tests {@link DefaultTreeTable.Node#setValue(TableColumn, Object)}.
+     */
+    @Test
+    public void testNodeValues() {
+        final DefaultTreeTable table = new DefaultTreeTable(NAME, TYPE);
+        final TreeTable.Node   node  = new DefaultTreeTable.Node(table);
+        assertNull(node.getValue(NAME));
+        assertNull(node.getValue(TYPE));
+        node.setValue(NAME, "A number");
+        node.setValue(TYPE, Number.class);
+        assertEquals("A number",   node.getValue(NAME));
+        assertEquals(Number.class, node.getValue(TYPE));
+    }
 }



Mime
View raw message