Author: mes
Date: 2010-09-05 16:44:35 -0700 (Sun, 05 Sep 2010)
New Revision: 21706

Modified:
   
core3/model-impl/trunk/src/main/java/org/cytoscape/model/internal/ArrayGraph.java
   
core3/model-impl/trunk/src/main/java/org/cytoscape/model/internal/CyDataTableFactoryImpl.java
   
core3/model-impl/trunk/src/main/java/org/cytoscape/model/internal/CyDataTableImpl.java
   
core3/model-impl/trunk/src/main/java/org/cytoscape/model/internal/CyNetworkFactoryImpl.java
   core3/model-impl/trunk/src/main/resources/META-INF/spring/bundle-context.xml
   core3/model-impl/trunk/src/test/java/org/cytoscape/model/CyDataTableTest.java
   
core3/model-impl/trunk/src/test/java/org/cytoscape/model/TestCyNetworkFactory.java
Log:
added the concept of primary key to CyDataTable

Modified: 
core3/model-impl/trunk/src/main/java/org/cytoscape/model/internal/ArrayGraph.java
===================================================================
--- 
core3/model-impl/trunk/src/main/java/org/cytoscape/model/internal/ArrayGraph.java
   2010-09-05 23:44:18 UTC (rev 21705)
+++ 
core3/model-impl/trunk/src/main/java/org/cytoscape/model/internal/ArrayGraph.java
   2010-09-05 23:44:35 UTC (rev 21706)
@@ -46,6 +46,7 @@
 import org.cytoscape.event.CyEventHelper;
 import org.cytoscape.model.CyDataTable;
 import org.cytoscape.model.CyTableManager;
+import org.cytoscape.model.CyDataTableFactory;
 import org.cytoscape.model.CyEdge;
 import org.cytoscape.model.CyNetwork;
 import org.cytoscape.model.CyNode;
@@ -107,7 +108,7 @@
         * Creates a new ArrayGraph object.
         * @param eh The CyEventHelper used for firing events.
         */
-       public ArrayGraph(final CyEventHelper eh, final CyTableManager 
tableMgr) {
+       public ArrayGraph(final CyEventHelper eh, final CyTableManager 
tableMgr, final CyDataTableFactory tableFactory) {
                this.tableMgr = tableMgr;
                suid = SUIDFactory.getNextSUID();
                numSubNetworks = 0;
@@ -118,23 +119,23 @@
                edgePointers = new ArrayList<EdgePointer>();
 
                netAttrMgr = new HashMap<String, CyDataTable>();
-               netAttrMgr.put(CyNetwork.DEFAULT_ATTRS, new 
CyDataTableImpl(null, suid + " network", true,eh));
-               netAttrMgr.put(CyNetwork.HIDDEN_ATTRS, new 
CyDataTableImpl(null, suid + " network", false,eh));
+               netAttrMgr.put(CyNetwork.DEFAULT_ATTRS, 
tableFactory.createTable( suid + " network", "SUID", Long.class, true));
+               netAttrMgr.put(CyNetwork.HIDDEN_ATTRS, 
tableFactory.createTable( suid + " network", "SUID", Long.class, false));
 
                
netAttrMgr.get(CyNetwork.DEFAULT_ATTRS).createColumn("name",String.class,false);
                attrs().set("name","");
                // potential leak since "this" isn't yet fully constructed
 
                nodeAttrMgr = new HashMap<String, CyDataTable>();
-               nodeAttrMgr.put(CyNetwork.DEFAULT_ATTRS, new 
CyDataTableImpl(null, suid + " node", true,eh));
-               nodeAttrMgr.put(CyNetwork.HIDDEN_ATTRS, new 
CyDataTableImpl(null, suid + " node", false,eh));
+               nodeAttrMgr.put(CyNetwork.DEFAULT_ATTRS, 
tableFactory.createTable( suid + " node", "SUID", Long.class, true));
+               nodeAttrMgr.put(CyNetwork.HIDDEN_ATTRS, 
tableFactory.createTable( suid + " node", "SUID", Long.class, false));
 
                
nodeAttrMgr.get(CyNetwork.DEFAULT_ATTRS).createColumn("name",String.class,false);
                
nodeAttrMgr.get(CyNetwork.DEFAULT_ATTRS).createColumn("selected",Boolean.class,false);
 
                edgeAttrMgr = new HashMap<String, CyDataTable>();
-               edgeAttrMgr.put(CyNetwork.DEFAULT_ATTRS, new 
CyDataTableImpl(null, suid + " edge", true,eh));
-               edgeAttrMgr.put(CyNetwork.HIDDEN_ATTRS, new 
CyDataTableImpl(null, suid + " edge", false,eh));
+               edgeAttrMgr.put(CyNetwork.DEFAULT_ATTRS, 
tableFactory.createTable( suid + " edge", "SUID", Long.class, true));
+               edgeAttrMgr.put(CyNetwork.HIDDEN_ATTRS, 
tableFactory.createTable( suid + " edge", "SUID", Long.class, false));
 
                
edgeAttrMgr.get(CyNetwork.DEFAULT_ATTRS).createColumn("name",String.class,false);
                
edgeAttrMgr.get(CyNetwork.DEFAULT_ATTRS).createColumn("selected",Boolean.class,false);

Modified: 
core3/model-impl/trunk/src/main/java/org/cytoscape/model/internal/CyDataTableFactoryImpl.java
===================================================================
--- 
core3/model-impl/trunk/src/main/java/org/cytoscape/model/internal/CyDataTableFactoryImpl.java
       2010-09-05 23:44:18 UTC (rev 21705)
+++ 
core3/model-impl/trunk/src/main/java/org/cytoscape/model/internal/CyDataTableFactoryImpl.java
       2010-09-05 23:44:35 UTC (rev 21706)
@@ -60,15 +60,8 @@
                tables = new HashMap<Long,CyDataTable>();
        }
 
-       /**
-        * @param name The name of the CyDataTable.
-        * @param pub Whether or not the CyDataTable should be public.
-        *
-        * @return A new {...@link CyDataTable} with the specified name that is 
either public or not (see
-        *         {...@link CyDataTable#isPublic}.
-        */
-       public CyDataTable createTable(String name, boolean pub) {
-               CyDataTable cdt = new CyDataTableImpl(null,name,pub,help);
+       public CyDataTable createTable(String name, String primaryKey, Class<?> 
primaryKeyType, boolean pub) {
+               CyDataTable cdt = new 
CyDataTableImpl(name,primaryKey,primaryKeyType,pub,help);
                tables.put( cdt.getSUID(), cdt );
                return cdt;
        }

Modified: 
core3/model-impl/trunk/src/main/java/org/cytoscape/model/internal/CyDataTableImpl.java
===================================================================
--- 
core3/model-impl/trunk/src/main/java/org/cytoscape/model/internal/CyDataTableImpl.java
      2010-09-05 23:44:18 UTC (rev 21705)
+++ 
core3/model-impl/trunk/src/main/java/org/cytoscape/model/internal/CyDataTableImpl.java
      2010-09-05 23:44:35 UTC (rev 21706)
@@ -39,6 +39,7 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Collections;
 
 import org.cytoscape.event.CyEventHelper;
 import org.cytoscape.model.CyDataTable;
@@ -53,8 +54,8 @@
  */
 public class CyDataTableImpl implements CyDataTable {
 
-       private final Map<String, Map<Long, Object>> attributes;
-       private final Map<Long, CyRow> rows;
+       private final Map<String, Map<Object, Object>> attributes;
+       private final Map<Object, CyRow> rows;
 
        private final Map<String, Class<?>> types;
        private final Map<String, Boolean> unique;
@@ -68,9 +69,10 @@
        // Unique ID.
        private final long suid;
 
-       private CyEventHelper eventHelper;
+       private final String primaryKey;
+       private final Class<?> primaryKeyType;
 
-       private long rowSUID = -1;
+       private final CyEventHelper eventHelper;
 
        /**
         * Creates a new CyDataTableImpl object.
@@ -82,24 +84,19 @@
         * @param pub
         *            DOCUMENT ME!
         */
-       public CyDataTableImpl(Map<String, Class<?>> typeMap, String name,
+       public CyDataTableImpl(String title, String primaryKey, Class<?> 
primaryKeyType, 
                        boolean pub, final CyEventHelper eventHelper) {
-               this.title = name;
+               this.title = title;
+               this.primaryKey = primaryKey;
+               this.primaryKeyType = primaryKeyType;
                this.pub = pub;
                this.suid = SUIDFactory.getNextSUID();
                this.eventHelper = eventHelper;
-               attributes = new HashMap<String, Map<Long, Object>>();
-               rows = new HashMap<Long, CyRow>();
-
-               if (typeMap == null) {
-                       types = new HashMap<String, Class<?>>();
-                       unique = new HashMap<String, Boolean>();
-               } else {
-                       types = new HashMap<String, Class<?>>(typeMap);
-                       // TODO!
-                       unique = new HashMap<String, Boolean>();
-               }
-               
+               attributes = new HashMap<String, Map<Object, Object>>();
+               rows = new HashMap<Object, CyRow>();
+               types = new HashMap<String, Class<?>>();
+               types.put(primaryKey,primaryKeyType);
+               unique = new HashMap<String, Boolean>();
        }
 
        /**
@@ -145,9 +142,17 @@
         * @return DOCUMENT ME!
         */
        public Map<String, Class<?>> getColumnTypeMap() {
-               return new HashMap<String, Class<?>>(types);
+               return Collections.unmodifiableMap(types);
        }
 
+       public String getPrimaryKey() {
+               return primaryKey;
+       }
+
+       public Class<?> getPrimaryKeyType() {
+               return primaryKeyType;
+       }
+
        /**
         * DOCUMENT ME!
         * 
@@ -189,7 +194,7 @@
 
                if (curr == null) {
                        types.put(attributeName, cls);
-                       attributes.put(attributeName, new HashMap<Long, 
Object>());
+                       attributes.put(attributeName, new HashMap<Object, 
Object>());
                        unique.put(attributeName, u);
                        
                        // Fire event
@@ -235,7 +240,7 @@
                if (columnName == null)
                        throw new NullPointerException("column name is null");
 
-               Map<Long, Object> vals = attributes.get(columnName);
+               Map<Object, Object> vals = attributes.get(columnName);
 
                if (vals == null)
                        throw new NullPointerException("attribute does not 
exist");
@@ -256,7 +261,8 @@
         * 
         * @return DOCUMENT ME!
         */
-       public CyRow getRow(final long suid) {
+       public CyRow getRow(final Object suid) {
+               checkKey(suid);
                CyRow row = rows.get(suid);
                if (row != null)
                        return row;
@@ -266,39 +272,31 @@
                return row;
        }
 
-       public List<CyRow> getAllRows()
-       {
-               return new ArrayList<CyRow>(rows.values());
-       }
+    private void checkKey(final Object suid) {
+        if ( suid == null )
+            throw new NullPointerException("key is null");
 
-       /**
-        * DOCUMENT ME!
-        * 
-        * @return DOCUMENT ME!
-        */
-       public CyRow addRow() {
-               long r;
-               synchronized (this) {
-                       r = rowSUID--;
-               }
+        if ( !primaryKeyType.isAssignableFrom( suid.getClass() ) )
+            throw new IllegalArgumentException("key of type " + 
suid.getClass() + " and not the expected: " + primaryKeyType);
 
-               CyRow row = new InternalRow(r, this);
-               rows.put(r, row);
+    }
 
-               return row;
+       public List<CyRow> getAllRows() {
+               return new ArrayList<CyRow>(rows.values());
        }
 
+
        // internal methods
-       private void removeX(long suid, String attributeName) {
+       private void removeX(Object suid, String attributeName) {
                if (attributes.containsKey(attributeName)) {
-                       Map<Long, Object> map = attributes.get(attributeName);
+                       Map<Object, Object> map = attributes.get(attributeName);
 
                        if (map.containsKey(suid))
                                map.remove(suid);
                }
        }
 
-       private void setX(long suid, String attrName, Object value) {
+       private void setX(Object suid, String attrName, Object value) {
                if (value == null)
                        throw new NullPointerException("value is null");
 
@@ -308,19 +306,19 @@
 
                checkType(value);
 
-               Map<Long, Object> vls = attributes.get(attrName);
+               Map<Object, Object> vls = attributes.get(attrName);
 
                if (types.get(attrName).isAssignableFrom(value.getClass())) {
                        // TODO this is an implicit addRow - not sure if we 
want to refactor this or not
                        vls.put(suid, value);
-                       eventHelper.getMicroListener(RowSetMicroListener.class, 
getRow(suid)).handleRowSet(attrName, value);
+                       eventHelper.getMicroListener(RowSetMicroListener.class, 
getRow(suid)).handleRowSet(attrName,value);
                } else
                        throw new IllegalArgumentException("value is not of 
type: "
                                        + types.get(attrName));
        }
 
-       private Object getRawX(long suid, String attrName) {
-               Map<Long, Object> vls = attributes.get(attrName);
+       private Object getRawX(Object suid, String attrName) {
+               Map<Object, Object> vls = attributes.get(attrName);
 
                if (vls == null)
                        return null;
@@ -328,7 +326,7 @@
                return vls.get(suid);
        }
 
-       private <T> T getX(long suid, String attrName, Class<? extends T> type) 
{
+       private <T> T getX(Object suid, String attrName, Class<? extends T> 
type) {
                Object vl = getRawX(suid, attrName);
 
                if (vl == null)
@@ -337,9 +335,9 @@
                return type.cast(vl);
        }
 
-       private <T> boolean containsX(long suid, String attrName,
+       private <T> boolean containsX(Object suid, String attrName,
                        Class<? extends T> type) {
-               Map<Long, Object> vls = attributes.get(attrName);
+               Map<Object, Object> vls = attributes.get(attrName);
 
                if (vls == null)
                        return false;
@@ -356,8 +354,8 @@
                        return false;
        }
 
-       private Class<?> containsX(long suid, String attrName) {
-               Map<Long, Object> vls = attributes.get(attrName);
+       private Class<?> containsX(Object suid, String attrName) {
+               Map<Object, Object> vls = attributes.get(attrName);
 
                if (vls == null)
                        return null;
@@ -420,10 +418,10 @@
        }
 
        private class InternalRow implements CyRow {
-               private final long suid;
+               private final Object suid;
                private final CyDataTable table;
 
-               InternalRow(long suid, CyDataTable table) {
+               InternalRow(Object suid, CyDataTable table) {
                        this.suid = suid;
                        this.table = table;
                }
@@ -469,16 +467,17 @@
 
                        InternalRow ir = (InternalRow) o;
 
+                       // TODO is this sufficent since we're not using long 
any more?
                        if (ir.suid == this.suid)
                                return true;
                        else
-
                                return false;
                }
 
                public @Override
                int hashCode() {
-                       return (int) (suid ^ (suid >>> 32));
+                       // TODO is this right?
+                       return suid.hashCode();
                }
 
                public CyDataTable getDataTable() {

Modified: 
core3/model-impl/trunk/src/main/java/org/cytoscape/model/internal/CyNetworkFactoryImpl.java
===================================================================
--- 
core3/model-impl/trunk/src/main/java/org/cytoscape/model/internal/CyNetworkFactoryImpl.java
 2010-09-05 23:44:18 UTC (rev 21705)
+++ 
core3/model-impl/trunk/src/main/java/org/cytoscape/model/internal/CyNetworkFactoryImpl.java
 2010-09-05 23:44:35 UTC (rev 21706)
@@ -38,6 +38,7 @@
 import org.cytoscape.model.CyNetwork;
 import org.cytoscape.model.CyNetworkFactory;
 import org.cytoscape.model.CyTableManager;
+import org.cytoscape.model.CyDataTableFactory;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -51,31 +52,35 @@
        
        private final CyEventHelper help;
        private final CyTableManager mgr;
+       private final CyDataTableFactory tableFactory;
 
        /**
         * Creates a new CyNetworkFactoryImpl object.
         *
         * @param help An instance of CyEventHelper. 
         */
-       public CyNetworkFactoryImpl(final CyEventHelper help, final 
CyTableManager mgr) {
+       public CyNetworkFactoryImpl(final CyEventHelper help, final 
CyTableManager mgr, final CyDataTableFactory tableFactory) {
                if (help == null)
                        throw new NullPointerException("CyEventHelper is null");
 
                if (mgr == null)
                        throw new NullPointerException("CyTableManager is 
null");
 
+               if (tableFactory == null)
+                       throw new NullPointerException("CyDataTableFactory is 
null");
+
                this.help = help;
                this.mgr = mgr;
+               this.tableFactory = tableFactory;
        }
 
        /**
         * {...@inheritdoc}
         */
        public CyNetwork getInstance() {
-               //return new MGraph(help);
-               ArrayGraph net = new ArrayGraph(help,mgr);
-               logger.info("ArrayGraph created: ID = " +  net.getSUID());
-               logger.info("ArrayGraph created: Base Graph ID = " +  
net.getBaseNetwork().getSUID());
+               ArrayGraph net = new ArrayGraph(help,mgr,tableFactory);
+               logger.info("CyNetwork created: ID = " +  net.getSUID());
+               logger.info("CyNetwork created: Base Graph ID = " +  
net.getBaseNetwork().getSUID());
                return net.getBaseNetwork(); 
        }
 }

Modified: 
core3/model-impl/trunk/src/main/resources/META-INF/spring/bundle-context.xml
===================================================================
--- 
core3/model-impl/trunk/src/main/resources/META-INF/spring/bundle-context.xml    
    2010-09-05 23:44:18 UTC (rev 21705)
+++ 
core3/model-impl/trunk/src/main/resources/META-INF/spring/bundle-context.xml    
    2010-09-05 23:44:35 UTC (rev 21706)
@@ -18,13 +18,14 @@
        <bean id="cyTableManager" 
class="org.cytoscape.model.internal.CyTableManagerImpl">
        </bean>
 
-       <bean id="cyNetworkFactory" 
class="org.cytoscape.model.internal.CyNetworkFactoryImpl">
+       <bean id="cyDataTableFactory" 
class="org.cytoscape.model.internal.CyDataTableFactoryImpl">
                <constructor-arg ref="cyEventHelperServiceRef" />
-               <constructor-arg ref="cyTableManager" />
        </bean>
 
-       <bean id="cyDataTableFactory" 
class="org.cytoscape.model.internal.CyDataTableFactoryImpl">
+       <bean id="cyNetworkFactory" 
class="org.cytoscape.model.internal.CyNetworkFactoryImpl">
                <constructor-arg ref="cyEventHelperServiceRef" />
+               <constructor-arg ref="cyTableManager" />
+               <constructor-arg ref="cyDataTableFactory" />
        </bean>
 
        <bean id="cyRootNetworkFactory" 
class="org.cytoscape.model.internal.CyRootNetworkFactoryImpl"/>

Modified: 
core3/model-impl/trunk/src/test/java/org/cytoscape/model/CyDataTableTest.java
===================================================================
--- 
core3/model-impl/trunk/src/test/java/org/cytoscape/model/CyDataTableTest.java   
    2010-09-05 23:44:18 UTC (rev 21705)
+++ 
core3/model-impl/trunk/src/test/java/org/cytoscape/model/CyDataTableTest.java   
    2010-09-05 23:44:35 UTC (rev 21706)
@@ -51,8 +51,8 @@
        public void setUp() {
 
                eventHelper = new DummyCyEventHelper();
-               mgr = new CyDataTableImpl(null, "homer", true, eventHelper);
-               attrs = mgr.getRow(1);
+               mgr = new CyDataTableImpl("homer", "SUID", Long.class, true, 
eventHelper);
+               attrs = mgr.getRow(1l);
        }
 
        /**

Modified: 
core3/model-impl/trunk/src/test/java/org/cytoscape/model/TestCyNetworkFactory.java
===================================================================
--- 
core3/model-impl/trunk/src/test/java/org/cytoscape/model/TestCyNetworkFactory.java
  2010-09-05 23:44:18 UTC (rev 21705)
+++ 
core3/model-impl/trunk/src/test/java/org/cytoscape/model/TestCyNetworkFactory.java
  2010-09-05 23:44:35 UTC (rev 21706)
@@ -37,32 +37,34 @@
 package org.cytoscape.model;
 
 
-//import org.cytoscape.model.internal.CyNetworkImpl;
-//import org.cytoscape.model.internal.MGraph;
 import org.cytoscape.model.internal.ArrayGraph;
 import org.cytoscape.model.internal.CyTableManagerImpl;
+import org.cytoscape.model.internal.CyDataTableFactoryImpl;
 import org.cytoscape.event.DummyCyEventHelper;
 
 import org.cytoscape.model.subnetwork.CyRootNetwork;
 import org.junit.Test;
+import static org.junit.Assert.*;
 
 public class TestCyNetworkFactory {
 
        public TestCyNetworkFactory() {};
 
        public static CyNetwork getInstance() {
-               //return new CyNetworkImpl(new DummyCyEventHelper());
-               //return new MGraph(new DummyCyEventHelper());
-               return new ArrayGraph(new DummyCyEventHelper(), new 
CyTableManagerImpl());
+               DummyCyEventHelper deh = new DummyCyEventHelper();
+               return new ArrayGraph(deh, new CyTableManagerImpl(),new 
CyDataTableFactoryImpl(deh));
        }
 
-       public static CyRootNetwork getRootInstance() {
-               return new ArrayGraph(new DummyCyEventHelper(), new 
CyTableManagerImpl());
+       public static CyRootNetwork getRootInstance() { 
+               // This only works because we know that ArrayGraph (returned 
from 
+               // getInstance) is also a root network!
+               return (CyRootNetwork)getInstance();
        }
        
        @Test
        public void testFactory() throws Exception {
-               
+               CyNetwork n = getInstance();
+               assertNotNull(n);
        }
 }
 

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