]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/single/SingleSetSyncListener.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / procedure / single / SingleSetSyncListener.java
diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/single/SingleSetSyncListener.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/single/SingleSetSyncListener.java
new file mode 100644 (file)
index 0000000..8dc0348
--- /dev/null
@@ -0,0 +1,108 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.db.common.procedure.single;\r
+\r
+import java.util.Collection;\r
+import java.util.HashSet;\r
+\r
+import org.simantics.db.AsyncReadGraph;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.common.utils.Logger;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.procedure.AsyncListener;\r
+import org.simantics.db.procedure.AsyncProcedure;\r
+\r
+abstract public class SingleSetSyncListener<T> implements AsyncListener<Collection<T>>{\r
+    \r
+    private Collection<T> previous = null;\r
+    \r
+    /**\r
+     * @param graph\r
+     * @return <code>true</code> to continue, <code>false</code> to end processing here\r
+     * @throws DatabaseException\r
+     */\r
+    public boolean start(ReadGraph graph) throws DatabaseException { return true; }\r
+    public void add(ReadGraph graph, T item) throws DatabaseException {}\r
+    public void remove(ReadGraph graph,T item) throws DatabaseException {}\r
+    public void finished(ReadGraph graph) throws DatabaseException {}\r
+\r
+    public void exception(ReadGraph graph, Throwable t) throws DatabaseException {\r
+               Logger.defaultLogError(t);\r
+    }\r
+    \r
+    @Override\r
+    final public void execute(AsyncReadGraph graph, final Collection<T> result) {\r
+       \r
+       if(result == null) {\r
+               Logger.defaultLogError("SingleSetSyncListener does not accept null result.");\r
+               return;\r
+       }\r
+       \r
+       graph.asyncRequest(new ReadRequest() {\r
+\r
+                       @Override\r
+                       public void run(ReadGraph graph) throws DatabaseException {\r
+\r
+                               if (!start(graph))\r
+                                       return;\r
+\r
+                               if(previous == null) {\r
+\r
+                                       for(T t : result) add(graph, t);\r
+\r
+                               } else {\r
+\r
+                                       HashSet<T> added = new HashSet<T>(result);\r
+                                       added.removeAll(previous);\r
+                                       HashSet<T> removed = new HashSet<T>(previous);\r
+                                       removed.removeAll(result);\r
+\r
+                                       for(T t : added) add(graph, t);\r
+                                       for(T t : removed) remove(graph, t);\r
+\r
+                               }\r
+\r
+                               finished(graph);\r
+\r
+                               previous = result;\r
+                               \r
+                       }\r
+               \r
+       }, new AsyncProcedure<Object>() {\r
+\r
+                       @Override\r
+                       public void exception(AsyncReadGraph graph, Throwable t) {\r
+                       Logger.defaultLogError(t);\r
+                       }\r
+\r
+            @Override\r
+            public void execute(AsyncReadGraph graph, Object result) {\r
+            }\r
+               \r
+       });\r
+        \r
+    }\r
+    \r
+    @Override\r
+    final public void exception(AsyncReadGraph graph, final Throwable throwable) {\r
+        graph.asyncRequest(new ReadRequest() {\r
+\r
+            @Override\r
+            public void run(ReadGraph graph) throws DatabaseException {\r
+                exception((ReadGraph)graph, throwable);\r
+            }\r
+\r
+        });\r
+    }\r
+       \r
+}\r