]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/PendingTaskSupport.java
Multiple readers in db client
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / PendingTaskSupport.java
diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/PendingTaskSupport.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/PendingTaskSupport.java
new file mode 100644 (file)
index 0000000..f63bad5
--- /dev/null
@@ -0,0 +1,45 @@
+package org.simantics.db.impl.query;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+import org.simantics.db.impl.query.QueryProcessor.SessionTask;
+
+/*
+ * Support class for queuing pending tasks to be executed when result gets ready
+ */
+public class PendingTaskSupport {
+
+    private ArrayList<SessionTask> pendingTasks;
+    private IPending pending;
+    
+    public PendingTaskSupport(IPending pending) {
+        this.pending = pending;
+    }
+    
+    /*
+     * We assume here that the associated IPending performs this atomically
+     * The caller is responsible for execution of the returned task after the critical section
+     */
+    public boolean executeWhenResultIsAvailable(SessionTask task) {
+        if(pending.isPending()) {
+            if(pendingTasks == null)
+                pendingTasks = new ArrayList<SessionTask>();
+            pendingTasks.add(task);
+            return false;
+        } else {
+            return true;
+        }
+    }
+
+    /*
+     * We assume here that the associated IPending performs this atomically after changing the pending result
+     * The caller is responsible for execution of the returned task after the critical section
+     */
+    public Collection<SessionTask> executePending() {
+        ArrayList<SessionTask> ret = pendingTasks;
+        pendingTasks = null;
+        return ret;
+    }
+
+}