Author: ruschein
Date: 2010-12-02 14:31:30 -0800 (Thu, 02 Dec 2010)
New Revision: 23076
Modified:
core3/model-api/trunk/src/main/java/org/cytoscape/model/CyTable.java
core3/model-impl/trunk/impl/src/main/java/org/cytoscape/model/internal/CyTableImpl.java
Log:
Added new getMatchingRows() method to CyTable.
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
2010-12-02 21:02:23 UTC (rev 23075)
+++ core3/model-api/trunk/src/main/java/org/cytoscape/model/CyTable.java
2010-12-02 22:31:30 UTC (rev 23076)
@@ -30,6 +30,7 @@
import java.util.List;
import java.util.Map;
+import java.util.Set;
/**
@@ -141,4 +142,11 @@
* @return if available, a message describing an internal error,
otherwise null
*/
String getLastInternalError();
+
+ /** Returns all the rows of a specified column that contain a certain
value for that column.
+ * @param columnName the column for which we want the rows
+ * @param value the value for which we want the rows that
contain it
+ * @return the rows, if any that contain the value "value" for the
column "columnName"
+ */
+ Set<CyRow> getMatchingRows(String columnName, Object value);
}
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
2010-12-02 21:02:23 UTC (rev 23075)
+++
core3/model-impl/trunk/impl/src/main/java/org/cytoscape/model/internal/CyTableImpl.java
2010-12-02 22:31:30 UTC (rev 23076)
@@ -69,6 +69,7 @@
private final Set<String> currentlyActiveAttributes;
private final Map<String, Map<Object, Object>> attributes;
+ private final Map<String, Map<Object, Set<Object>>> reverse;
private final Map<Object, CyRow> rows;
private final Map<String, Class<?>> types;
@@ -118,6 +119,7 @@
currentlyActiveAttributes = new HashSet<String>();
attributes = new HashMap<String, Map<Object, Object>>();
+ reverse = new HashMap<String, Map<Object, Set<Object>>>();
rows = new HashMap<Object, CyRow>();
types = new HashMap<String, Class<?>>();
listElementTypes = new HashMap<String, Class<?>>();
@@ -196,9 +198,10 @@
* @param columnName
* DOCUMENT ME!
*/
- public void deleteColumn(String columnName) {
+ synchronized public void deleteColumn(String columnName) {
if (attributes.containsKey(columnName)) {
attributes.remove(columnName);
+ reverse.remove(columnName);
types.remove(columnName);
listElementTypes.remove(columnName);
@@ -236,6 +239,7 @@
types.put(columnName, type);
attributes.put(columnName, new HashMap<Object, Object>());
+ reverse.put(columnName, new HashMap<Object, Set<Object>>());
eventHelper.fireAsynchronousEvent(new ColumnCreatedEvent(this,
columnName));
}
@@ -255,6 +259,7 @@
types.put(columnName, List.class);
listElementTypes.put(columnName, listElementType);
attributes.put(columnName, new HashMap<Object, Object>());
+ reverse.put(columnName, new HashMap<Object, Set<Object>>());
eventHelper.fireAsynchronousEvent(new ColumnCreatedEvent(this,
columnName));
}
@@ -302,14 +307,14 @@
*
* @return DOCUMENT ME!
*/
- public CyRow getRow(final Object suid) {
- checkKey(suid);
- CyRow row = rows.get(suid);
+ public CyRow getRow(final Object key) {
+ checkKey(key);
+ CyRow row = rows.get(key);
if (row != null)
return row;
- row = new InternalRow(suid, this);
- rows.put(suid, row);
+ row = new InternalRow(key, this);
+ rows.put(key, row);
return row;
}
@@ -332,6 +337,20 @@
return new ArrayList<CyRow>(rows.values());
}
+ public Set<CyRow> getMatchingRows(final String columnName, final Object
value) {
+ final Map<Object, Set<Object>> valueToKeysMap =
reverse.get(columnName);
+
+ final Set<Object> keys = valueToKeysMap.get(value);
+ if (keys == null)
+ return new HashSet<CyRow>();
+
+ final Set<CyRow> matchingRows = new HashSet<CyRow>(keys.size());
+ for (final Object key : keys)
+ matchingRows.add(rows.get(key));
+
+ return matchingRows;
+ }
+
private void setX(final Object key, final String columnName, final
Object value) {
++counter;
if (columnName == null)
@@ -371,15 +390,28 @@
getRow(key)).handleRowSet(columnName, eqnValue);
} else {
// TODO this is an implicit addRow - not sure
if we want to refactor this or not
- vls.put(key, columnType.cast(value));
+ final Object newValue = columnType.cast(value);
+ vls.put(key, newValue);
+ addToReverseMap(columnName, key, newValue);
eventHelper.getMicroListener(RowSetMicroListener.class,
-
getRow(key)).handleRowSet(columnName, value);
+
getRow(key)).handleRowSet(columnName, newValue);
}
} else
throw new IllegalArgumentException("value is not of
type: "
+ types.get(columnName));
}
+ private void addToReverseMap(final String columnName, final Object key,
final Object value) {
+ final Map<Object, Set<Object>> valueTokeysMap =
reverse.get(columnName);
+ Set<Object> keys = valueTokeysMap.get(value);
+ if (keys == null) {
+ keys = new HashSet<Object>();
+ valueTokeysMap.put(value, keys);
+ }
+
+ keys.add(key);
+ }
+
private static boolean scalarEquationIsCompatible(final Object
equationCandidate,
final Class
targetType)
{
@@ -439,10 +471,9 @@
}
private void unSetX(Object suid, String columnName) {
-
if (!types.containsKey(columnName) ||
!attributes.containsKey(columnName))
throw new IllegalArgumentException("attribute: '" +
columnName
- + "' does not yet exist!");
+ + "' does not yet
exist!");
Map<Object, Object> vls = attributes.get(columnName);
@@ -450,6 +481,14 @@
eventHelper.getMicroListener(RowSetMicroListener.class,
getRow(suid)).handleRowSet(columnName,null);
}
+ private void removeFromReverseMap(final String columnName, final Object
key, final Object value) {
+ final Map<Object, Set<Object>> valueTokeysMap =
reverse.get(columnName);
+ Set<Object> keys = valueTokeysMap.get(value);
+ keys.remove(key);
+ if (keys.isEmpty())
+ valueTokeysMap.remove(value);
+ }
+
private Object getRawX(Object suid, String columnName) {
Map<Object, Object> vls = attributes.get(columnName);
--
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.