Author: mes
Date: 2011-09-23 17:43:23 -0700 (Fri, 23 Sep 2011)
New Revision: 26960

Modified:
   core3/api/trunk/model-api/src/main/java/org/cytoscape/model/CyColumn.java
   core3/api/trunk/model-api/src/main/java/org/cytoscape/model/CyRow.java
   core3/api/trunk/model-api/src/main/java/org/cytoscape/model/CyTable.java
   
core3/api/trunk/model-api/src/test/java/org/cytoscape/model/AbstractCyTableTest.java
   
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/ArrayGraph.java
   
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/CyColumnImpl.java
   
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/CyTableImpl.java
Log:
added feature allowing for default values in columns and rows

Modified: 
core3/api/trunk/model-api/src/main/java/org/cytoscape/model/CyColumn.java
===================================================================
--- core3/api/trunk/model-api/src/main/java/org/cytoscape/model/CyColumn.java   
2011-09-23 23:04:13 UTC (rev 26959)
+++ core3/api/trunk/model-api/src/main/java/org/cytoscape/model/CyColumn.java   
2011-09-24 00:43:23 UTC (rev 26960)
@@ -72,4 +72,10 @@
         * This method will return an instance even if the column is not 
virtual.
         */
        VirtualColumnInfo getVirtualColumnInfo();
-}
\ No newline at end of file
+
+       /**
+        * Returns the default value for the column, possibly null.
+        * @return The default value for the column, possibly null.
+        */
+        Object getDefaultValue();
+}

Modified: core3/api/trunk/model-api/src/main/java/org/cytoscape/model/CyRow.java
===================================================================
--- core3/api/trunk/model-api/src/main/java/org/cytoscape/model/CyRow.java      
2011-09-23 23:04:13 UTC (rev 26959)
+++ core3/api/trunk/model-api/src/main/java/org/cytoscape/model/CyRow.java      
2011-09-24 00:43:23 UTC (rev 26960)
@@ -50,6 +50,17 @@
         * Returns the value found for this row in the specified column
         * with the specified type.
         * @param columnName The name identifying the attribute.
+        * @param type The type of the column.
+        * @param defaultValue The value to return if the column has not 
previously been set. 
+        * @return the value found for this row in the specified column
+        * Please not that this method cannot be used to retrieve values that 
are Lists!
+        */
+       <T> T get(String columnName, Class<?extends T> type, T defaultValue);
+
+       /**
+        * Returns the value found for this row in the specified column
+        * with the specified type.
+        * @param columnName The name identifying the attribute.
         * @param listElementType  The type of the elements of the list that we 
wish to retrieve.
         * @return the value found for this row in the specified column
         * Please not that this method can only be used to retrieve values that 
are Lists!
@@ -57,6 +68,17 @@
        <T> List<T> getList(String columnName, Class<T> listElementType);
 
        /**
+        * Returns the value found for this row in the specified column
+        * with the specified type.
+        * @param columnName The name identifying the attribute.
+        * @param listElementType  The type of the elements of the list that we 
wish to retrieve.
+        * @param defaultValue The List to return if the column has not 
previously been set. 
+        * @return the value found for this row in the specified column
+        * Please not that this method can only be used to retrieve values that 
are Lists!
+        */
+       <T> List<T> getList(String columnName, Class<T> listElementType, 
List<T> defaultValue);
+
+       /**
         * Set the specified column for this row to the specified value.
         * To unset a column entry use null for value.
         * @param columnName The name identifying the attribute.

Modified: 
core3/api/trunk/model-api/src/main/java/org/cytoscape/model/CyTable.java
===================================================================
--- core3/api/trunk/model-api/src/main/java/org/cytoscape/model/CyTable.java    
2011-09-23 23:04:13 UTC (rev 26959)
+++ core3/api/trunk/model-api/src/main/java/org/cytoscape/model/CyTable.java    
2011-09-24 00:43:23 UTC (rev 26960)
@@ -118,25 +118,47 @@
 
        /**
         * Create a column of the specified name and the specified type. The 
column
+        * type is limited to Integer, Long, Double, String, and Boolean. The
+        * default value for the column will be null.
+        * @param columnName The name identifying the attribute.
+        * @param type The type of the column.
+        * @param isImmutable  if true, this column can never be deleted
+        */
+       <T> void createColumn(String columnName, Class<?extends T> type, 
boolean isImmutable);
+
+       /**
+        * Create a column of the specified name and the specified type. The 
column
         * type is limited to Integer, Long, Double, String, and Boolean.
         * @param columnName The name identifying the attribute.
         * @param type The type of the column.
         * @param isImmutable  if true, this column can never be deleted
+        * @param defaultValue The default value for the column. Must be of 
+        * the specified type or null.
         */
-       <T> void createColumn(String columnName, Class<?extends T> type,
-                             boolean isImmutable);
+       <T> void createColumn(String columnName, Class<?extends T> type, 
boolean isImmutable, T defaultValue);
 
        /**
         * Create a column of Lists with the specified name and the specified 
element type.
-        * The column type is limited to Integer, Long, Double, String, and 
Boolean.
+        * The column type is limited to Integer, Long, Double, String, and 
Boolean. The
+        * default value for the column will be null.
         * @param columnName The name identifying the attribute.
         * @param listElementType The type of the elements of the list.
         * @param isImmutable  if true, this column can never be deleted
         */
-       <T> void createListColumn(String columnName, Class<T> listElementType,
-                                 boolean isImmutable);
+       <T> void createListColumn(String columnName, Class<T> listElementType, 
boolean isImmutable);
 
        /**
+        * Create a column of Lists with the specified name and the specified 
element type.
+        * The column type is limited to Integer, Long, Double, String, and 
Boolean. 
+        * @param columnName The name identifying the attribute.
+        * @param listElementType The type of the elements of the list.
+        * @param isImmutable  if true, this column can never be deleted
+        * @param defaultValue A default list for the column. Must be a List of 
+        * the specified element type or null.
+        */
+       <T> void createListColumn(String columnName, Class<T> listElementType, 
boolean isImmutable, List<T> defaultValue );
+
+       /**
         * Returns the row specified by the primary key object and if a row
         * for the specified key does not yet exist in the table, a new row
         * will be created and the new row will be returned.

Modified: 
core3/api/trunk/model-api/src/test/java/org/cytoscape/model/AbstractCyTableTest.java
===================================================================
--- 
core3/api/trunk/model-api/src/test/java/org/cytoscape/model/AbstractCyTableTest.java
        2011-09-23 23:04:13 UTC (rev 26959)
+++ 
core3/api/trunk/model-api/src/test/java/org/cytoscape/model/AbstractCyTableTest.java
        2011-09-24 00:43:23 UTC (rev 26960)
@@ -866,4 +866,65 @@
                assertNotNull(table.getColumn("someOtherInt"));
                assertNotNull(table2.getColumn("someInt"));
        }
+
+       @Test
+       public void testDefaultCyColumn() {
+               table.createColumn("someInt", Integer.class, false, 5);
+               
assertEquals(Integer.valueOf(5),attrs.get("someInt",Integer.class));
+       }
+
+       @Test
+       public void testNullDefaultCyColumn() {
+               table.createColumn("someInt", Integer.class, false, null);
+               assertNull(attrs.get("someInt",Integer.class));
+       }
+
+       @Test
+       public void testDefaultListCyColumn() {
+               List<Integer> l = new ArrayList<Integer>();
+               l.add(5);
+               table.createListColumn("someInt", Integer.class, false, l);
+               assertEquals(l,attrs.getList("someInt",Integer.class));
+       }
+
+       @Test
+       public void testNullDefaultListCyColumn() {
+               table.createListColumn("someInt", Integer.class, false, null);
+               assertNull(attrs.getList("someInt",Integer.class));
+       }
+
+       @Test
+       public void testOverrideDefaultCyColumn() {
+               table.createColumn("someInt", Integer.class, false, 5);
+               
assertEquals(Integer.valueOf(3),attrs.get("someInt",Integer.class,3));
+       }
+
+       @Test
+       public void testOverrideDefaultListCyColumn() {
+               List<Integer> l1 = new ArrayList<Integer>();
+               l1.add(5);
+               List<Integer> l2 = new ArrayList<Integer>();
+               l2.add(3);
+               table.createListColumn("someInt", Integer.class, false, l1);
+               assertEquals(l2,attrs.getList("someInt",Integer.class,l2));
+       }
+
+       @Test
+       public void testNullCantOverrideDefaultCyColumn() {
+               table.createColumn("someInt", Integer.class, false, 5);
+               
assertEquals(Integer.valueOf(5),attrs.get("someInt",Integer.class,null));
+       }
+
+       @Test
+       public void testNullCantOverrideDefaultListCyColumn() {
+               List<Integer> l1 = new ArrayList<Integer>();
+               l1.add(5);
+               table.createListColumn("someInt", Integer.class, false, l1);
+               assertEquals(l1,attrs.getList("someInt",Integer.class,null));
+       }
+
+       @Test
+       public void testGetAttrBeforeColumnExists() {
+               assertNull(attrs.get("nonexistentColumnX",Integer.class));
+       }
 }

Modified: 
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/ArrayGraph.java
===================================================================
--- 
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/ArrayGraph.java
 2011-09-23 23:04:13 UTC (rev 26959)
+++ 
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/ArrayGraph.java
 2011-09-24 00:43:23 UTC (rev 26960)
@@ -164,7 +164,7 @@
         
nodeAttrMgr.get(CyNetwork.DEFAULT_ATTRS).createColumn(CyTableEntry.NAME,
                                       String.class, true);
         
nodeAttrMgr.get(CyNetwork.DEFAULT_ATTRS).createColumn(CyNetwork.SELECTED,
-                                      Boolean.class, true);
+                                      Boolean.class, true, Boolean.FALSE);
 
                return nodeAttrMgr;
 
@@ -182,7 +182,7 @@
         
edgeAttrMgr.get(CyNetwork.DEFAULT_ATTRS).createColumn(CyTableEntry.NAME,
                                       String.class, true);
         
edgeAttrMgr.get(CyNetwork.DEFAULT_ATTRS).createColumn(CyNetwork.SELECTED,
-                                      Boolean.class, true);
+                                      Boolean.class, true, Boolean.FALSE);
         
edgeAttrMgr.get(CyNetwork.DEFAULT_ATTRS).createColumn(CyEdge.INTERACTION,
                                       String.class, true);
 

Modified: 
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/CyColumnImpl.java
===================================================================
--- 
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/CyColumnImpl.java
       2011-09-23 23:04:13 UTC (rev 26959)
+++ 
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/CyColumnImpl.java
       2011-09-24 00:43:23 UTC (rev 26960)
@@ -44,10 +44,11 @@
        private final VirtualColumnInfo virtualInfo;
        private final boolean isPrimaryKey;
        private final boolean isImmutable;
+       private final Object defaultValue;
 
        CyColumnImpl(final CyTableImpl table, final String columnName, final 
Class<?> columnType,
                     final Class<?> listElementType, final VirtualColumnInfo 
virtualInfo,
-                    final boolean isPrimaryKey, final boolean isImmutable)
+                    final boolean isPrimaryKey, final boolean isImmutable, 
final Object defaultValue)
        {
                this.table           = table;
                this.columnName      = columnName;
@@ -56,6 +57,13 @@
                this.virtualInfo     = virtualInfo;
                this.isPrimaryKey    = isPrimaryKey;
                this.isImmutable     = isImmutable;
+
+               if ( defaultValue != null && 
!columnType.isAssignableFrom(defaultValue.getClass()) )
+                       throw new IllegalArgumentException("The type of the 
defaultValue (" + 
+                                                          
defaultValue.getClass().getName() + 
+                                                                               
           ") cannot be assigned to type of this column (" +
+                                                                               
           columnType.getName() + ")" );
+               this.defaultValue    = defaultValue;
        }
 
        @Override
@@ -108,4 +116,8 @@
        public VirtualColumnInfo getVirtualColumnInfo() {
                return virtualInfo;
        }
+
+       public Object getDefaultValue() {
+               return defaultValue;
+       }
 }

Modified: 
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/CyTableImpl.java
===================================================================
--- 
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/CyTableImpl.java
        2011-09-23 23:04:13 UTC (rev 26959)
+++ 
core3/impl/trunk/model-impl/impl/src/main/java/org/cytoscape/model/internal/CyTableImpl.java
        2011-09-24 00:43:23 UTC (rev 26960)
@@ -127,7 +127,8 @@
                                                       /* listElementType = */ 
null,
                                                       virtualInfo,
                                                       /* isPrimaryKey = */ 
true,
-                                                      /* isImmutable = */ 
true));
+                                                      /* isImmutable = */ 
true, 
+                                                          null));
                attributes.put(primaryKey, new HashMap<Object, Object>());
                reverse.put(primaryKey, HashMultimap.create());
 
@@ -335,8 +336,15 @@
 
        @Override
        public <T> void createColumn(final String columnName, final Class<? 
extends T> type,
-                                    final boolean isImmutable)
+                                    final boolean isImmutable) 
        {
+               createColumn(columnName,type,isImmutable,null);
+       }
+
+       @Override
+       public <T> void createColumn(final String columnName, final Class<? 
extends T> type,
+                                    final boolean isImmutable, final T 
defaultValue)
+       {
                synchronized(this) {
                        if (columnName == null)
                                throw new NullPointerException("attribute name 
is null");
@@ -361,7 +369,8 @@
                                                               /* 
listElementType = */ null,
                                                               virtualInfo ,
                                                               /* isPrimaryKey 
= */ false,
-                                                              isImmutable));
+                                                              isImmutable,
+                                                                  
defaultValue));
                        attributes.put(columnName, new HashMap<Object, 
Object>(10000));
                        reverse.put(columnName, HashMultimap.create());
                }
@@ -373,6 +382,14 @@
        public <T> void createListColumn(final String columnName, final 
Class<T> listElementType,
                                         final boolean isImmutable)
        {
+               createListColumn(columnName,listElementType,isImmutable,null);
+       }
+
+
+       @Override
+       public <T> void createListColumn(final String columnName, final 
Class<T> listElementType,
+                                        final boolean isImmutable, final 
List<T> defaultValue)
+       {
                synchronized(this) {
                        if (columnName == null)
                                throw new NullPointerException("attribute name 
is null");
@@ -392,7 +409,8 @@
                                                               listElementType,
                                                               virtualInfo,
                                                               /* isPrimaryKey 
= */ false,
-                                                              isImmutable));
+                                                              isImmutable,
+                                                                  
defaultValue));
                        attributes.put(columnName, new HashMap<Object, 
Object>(10000));
                        reverse.put(columnName, HashMultimap.create());
                }
@@ -692,7 +710,7 @@
                return keyToValueMap.get(key);
        }
 
-       synchronized private <T> T getX(final Object key, final String 
columnName, final Class<? extends T> type) {
+       synchronized private <T> T getX(final Object key, final String 
columnName, final Class<? extends T> type, final T defaultValue) {
                if (type.isAssignableFrom(List.class))
                        logger.debug("risky use of get() instead of getList() 
for retrieving list");
                lastInternalError = null;
@@ -703,7 +721,7 @@
 
                final Object vl = getValueOrEquation(key, columnName);
                if (vl == null)
-                       return null;
+                       return getDefaultValue(columnName,defaultValue);
 
                if (vl instanceof Equation) {
                        final StringBuilder errorMsg = new StringBuilder();
@@ -712,11 +730,23 @@
                                                        
currentlyActiveAttributes, columnName,
                                                        errorMsg, this);
                        lastInternalError = errorMsg.toString();
-                       return result == null ? null : 
(T)EqnSupport.convertEqnResultToColumnType(type, result);
+                       return result == null ? 
getDefaultValue(columnName,defaultValue) : 
(T)EqnSupport.convertEqnResultToColumnType(type, result);
                } else
                        return type.cast(vl);
        }
 
+       private <T> T getDefaultValue(final String columnName, final T 
defaultValue) {
+               if ( defaultValue == null ) {
+                       final CyColumn column = types.get(columnName);
+                       if ( column == null )
+                               return null;
+                       else
+                               return (T)(column.getDefaultValue());
+               } else {
+                       return defaultValue;
+               }
+       }
+
        Object getValue(Object key, String columnName) {
                final VirtualColumn virtColumn = 
virtualColumnMap.get(columnName);
                if (virtColumn != null)
@@ -739,7 +769,7 @@
        }
 
        synchronized private <T> List<T> getListX(final Object key, final 
String columnName,
-                                                          final Class<? 
extends T> listElementType)
+                                                          final Class<? 
extends T> listElementType, final List<T> defaultValue)
        {
                if (!types.containsKey(columnName))
                        throw new IllegalArgumentException("'" + columnName + 
"' does not yet exist!");
@@ -760,7 +790,7 @@
 
                final Object vl = getValueOrEquation(key, columnName);
                if (vl == null)
-                       return null;
+                       return getDefaultValue(columnName,defaultValue);
 
                if (vl instanceof Equation) {
                        final StringBuilder errorMsg = new StringBuilder();
@@ -854,7 +884,8 @@
                                                 
sourceColumn.getListElementType(),
                                                 virtualInfo,
                                                 /* isPrimaryKey = */ false,
-                                                isImmutable);
+                                                isImmutable,
+                                                null);
                        types.put(targetName, targetColumn);
                        virtualColumnMap.put(targetName,
                                             new VirtualColumn(sourceTable, 
sourceColumnName, this,
@@ -965,15 +996,25 @@
 
                @Override
                public <T> T get(String attributeName, Class<? extends T> c) {
-                       return getX(key, attributeName, c);
+                       return getX(key, attributeName, c, null);
                }
 
                @Override
+               public <T> T get(String attributeName, Class<? extends T> c, T 
defValue) {
+                       return getX(key, attributeName, c, defValue);
+               }
+
+               @Override
                public <T> List<T> getList(String attributeName, Class<T> c) {
-                       return getListX(key, attributeName, c);
+                       return getListX(key, attributeName, c, null);
                }
 
                @Override
+               public <T> List<T> getList(String attributeName, Class<T> c, 
List<T> defValue) {
+                       return getListX(key, attributeName, c, defValue);
+               }
+
+               @Override
                public Object getRaw(String attributeName) {
                        return getValueOrEquation(key, attributeName);
                }
@@ -991,9 +1032,9 @@
                                final Class<?> type = column.getType();
                                if (type == List.class) {
                                        final Class<?> elementType = 
column.getListElementType();
-                                       nameToValueMap.put(columnName, 
getListX(key, columnName, elementType));
+                                       nameToValueMap.put(columnName, 
getListX(key, columnName, elementType, null));
                                } else
-                                       nameToValueMap.put(columnName, 
getX(key, columnName, type));
+                                       nameToValueMap.put(columnName, 
getX(key, columnName, type, null));
                        }
 
                        return nameToValueMap;

-- 
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