Author: ruschein
Date: 2011-01-24 13:19:14 -0800 (Mon, 24 Jan 2011)
New Revision: 23573

Modified:
   core3/model-api/trunk/src/main/java/org/cytoscape/model/CyTable.java
   
core3/model-api/trunk/src/test/java/org/cytoscape/model/AbstractCyTableTest.java
   
core3/model-impl/trunk/impl/src/main/java/org/cytoscape/model/internal/CyTableImpl.java
Log:
Extended the virtual column API and made the implementation more robust.

Modified: core3/model-api/trunk/src/main/java/org/cytoscape/model/CyTable.java
===================================================================
--- core3/model-api/trunk/src/main/java/org/cytoscape/model/CyTable.java        
2011-01-24 21:03:52 UTC (rev 23572)
+++ core3/model-api/trunk/src/main/java/org/cytoscape/model/CyTable.java        
2011-01-24 21:19:14 UTC (rev 23573)
@@ -164,17 +164,20 @@
        int getRowCount();
 
        /** Adds a "virtual" column to the the current table.
-        *  @param virtualColumn  the name of the new virtual column
+        *  @param virtualColumn  the name of the new virtual column, if this 
name already exists,
+        *                        new column names with -1, -2 and so appended 
to this name on will
+        *                        be tried until a nonexisting name will be 
found
         *  @param sourceColumn   the name of the column in "sourceTable" that 
will be mapped to
         *                        "virtualColumn"
         *  @param sourceTable    the table that really contains the column 
that we're adding (all
         *                        updates and lookups of this new column will 
be redirected to here)
         *  @param sourceJoinKey  the column in "sourceTable" that will be used 
for the join
         *  @param targetJoinKey  the column in current table that will be used 
for the join
+        *  @return the actual name of the new virtual column
         *  Note: The types of "sourceJoinKey" and "targetJoinKey" have to be 
identical.
         */
-       void addVirtualColumn(String virtualColumn, String sourceColumn, 
CyTable sourceTable,
-                             String sourceJoinKey, String targetJoinKey);
+       String addVirtualColumn(String virtualColumn, String sourceColumn, 
CyTable sourceTable,
+                               String sourceJoinKey, String targetJoinKey);
 
        /** Adds all columns in another table as "virtual" columns to the the 
current table.
         *  @param sourceTable    the table that really contains the column 
that we're adding (all

Modified: 
core3/model-api/trunk/src/test/java/org/cytoscape/model/AbstractCyTableTest.java
===================================================================
--- 
core3/model-api/trunk/src/test/java/org/cytoscape/model/AbstractCyTableTest.java
    2011-01-24 21:03:52 UTC (rev 23572)
+++ 
core3/model-api/trunk/src/test/java/org/cytoscape/model/AbstractCyTableTest.java
    2011-01-24 21:19:14 UTC (rev 23573)
@@ -561,9 +561,12 @@
                table.createColumn("x", Long.class);
                table2.createColumn("x2", Long.class);
                table2.createColumn("s", String.class);
-               table.addVirtualColumn("s1", "s", table2, "x2", "x");
+               assertEquals(table.addVirtualColumn("s1", "s", table2, "x2", 
"x"), "s1");
                assertEquals("Virtual column type should have been String!",
                             String.class, table.getType("s1"));
+               assertEquals(table.addVirtualColumn("s1", "s", table2, "x2", 
"x"), "s1-1");
+               assertEquals("Virtual column type should have been String!",
+                            String.class, table.getType("s1-1"));
        }
 
        @Test

Modified: 
core3/model-impl/trunk/impl/src/main/java/org/cytoscape/model/internal/CyTableImpl.java
===================================================================
--- 
core3/model-impl/trunk/impl/src/main/java/org/cytoscape/model/internal/CyTableImpl.java
     2011-01-24 21:03:52 UTC (rev 23572)
+++ 
core3/model-impl/trunk/impl/src/main/java/org/cytoscape/model/internal/CyTableImpl.java
     2011-01-24 21:19:14 UTC (rev 23573)
@@ -821,11 +821,11 @@
        }
 
        @Override
-       synchronized public final void addVirtualColumn(final String 
virtualColumn,
-                                                       final String 
sourceColumn,
-                                                       final CyTable 
sourceTable,
-                                                       final String 
sourceJoinKey,
-                                                       final String 
targetJoinKey)
+       synchronized public final String addVirtualColumn(final String 
virtualColumn,
+                                                         final String 
sourceColumn,
+                                                         final CyTable 
sourceTable,
+                                                         final String 
sourceJoinKey,
+                                                         final String 
targetJoinKey)
        {
                if (virtualColumn == null)
                        throw new NullPointerException("\"virtualColumn\" 
argument must never be null!");
@@ -838,9 +838,6 @@
                if (targetJoinKey == null)
                        throw new NullPointerException("\"targetJoinKey\" 
argument must never be null!");
 
-               if (types.containsKey(virtualColumn))
-                       throw new IllegalArgumentException("\"virtualColumn\" 
name already in use!");
-
                final Class<?> sourceColumnType = 
sourceTable.getType(sourceColumn);
                if (sourceColumnType == null)
                        throw new IllegalArgumentException("\"sourceColumn\" is 
not a column in \"sourceColumn\"!");
@@ -856,16 +853,33 @@
                if (sourceJoinKeyType != targetJoinKeyType)
                        throw new IllegalArgumentException("\"sourceJoinKey\" 
has a different type from \"targetJoinKey\"!");
 
-               types.put(virtualColumn, sourceColumnType);
+               final String targetColumnName = 
getUniqueColumnName(virtualColumn);
+               types.put(targetColumnName, sourceColumnType);
                if (sourceColumnType == List.class) {
                        final Class<?> listElementType = 
sourceTable.getListElementType(sourceColumn);
-                       listElementTypes.put(virtualColumn, listElementType);
+                       listElementTypes.put(targetColumnName, listElementType);
                }
                virtualColumnMap.put(
-                       virtualColumn,
+                       targetColumnName,
                        new VirtualColumn(sourceTable, sourceColumn, this, 
sourceJoinKey, targetJoinKey));
+
+               return targetColumnName;
        }
 
+       private String getUniqueColumnName(final String preferredName) {
+               if (!types.containsKey(preferredName))
+                       return preferredName;
+
+               String newUniqueName;
+               int i = 0;
+               do {
+                       ++i;
+                       newUniqueName = preferredName + "-" + i;
+               } while (types.containsKey(newUniqueName));
+
+               return newUniqueName;
+       }
+
        @Override
        synchronized public final void addVirtualColumns(final CyTable 
sourceTable,
                                                         final String 
sourceJoinKey,
@@ -889,7 +903,6 @@
                if (sourceJoinKeyType != targetJoinKeyType)
                        throw new IllegalArgumentException("\"sourceJoinKey\" 
has a different type from \"targetJoinKey\"!");
 
-               // Makes sure that none of the column names in "sourceTable" 
clash w/ names in this table:
                final Map<String, Class<?>> nameToTypeMap = 
sourceTable.getColumnTypeMap();
                for (final String column : nameToTypeMap.keySet()) {
                        if (column.equals(sourceJoinKey))

-- 
You received this message because you are subscribed to the Google Groups 
"cytoscape-cvs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/cytoscape-cvs?hl=en.

Reply via email to