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