1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.db.common.procedure.single;
\r
14 import java.util.Collection;
\r
15 import java.util.HashSet;
\r
17 import org.simantics.db.AsyncReadGraph;
\r
18 import org.simantics.db.ReadGraph;
\r
19 import org.simantics.db.common.request.ReadRequest;
\r
20 import org.simantics.db.common.utils.Logger;
\r
21 import org.simantics.db.exception.DatabaseException;
\r
22 import org.simantics.db.procedure.AsyncListener;
\r
23 import org.simantics.db.procedure.AsyncProcedure;
\r
25 abstract public class SingleSetSyncListener<T> implements AsyncListener<Collection<T>>{
\r
27 private Collection<T> previous = null;
\r
31 * @return <code>true</code> to continue, <code>false</code> to end processing here
\r
32 * @throws DatabaseException
\r
34 public boolean start(ReadGraph graph) throws DatabaseException { return true; }
\r
35 public void add(ReadGraph graph, T item) throws DatabaseException {}
\r
36 public void remove(ReadGraph graph,T item) throws DatabaseException {}
\r
37 public void finished(ReadGraph graph) throws DatabaseException {}
\r
39 public void exception(ReadGraph graph, Throwable t) throws DatabaseException {
\r
40 Logger.defaultLogError(t);
\r
44 final public void execute(AsyncReadGraph graph, final Collection<T> result) {
\r
46 if(result == null) {
\r
47 Logger.defaultLogError("SingleSetSyncListener does not accept null result.");
\r
51 graph.asyncRequest(new ReadRequest() {
\r
54 public void run(ReadGraph graph) throws DatabaseException {
\r
59 if(previous == null) {
\r
61 for(T t : result) add(graph, t);
\r
65 HashSet<T> added = new HashSet<T>(result);
\r
66 added.removeAll(previous);
\r
67 HashSet<T> removed = new HashSet<T>(previous);
\r
68 removed.removeAll(result);
\r
70 for(T t : added) add(graph, t);
\r
71 for(T t : removed) remove(graph, t);
\r
81 }, new AsyncProcedure<Object>() {
\r
84 public void exception(AsyncReadGraph graph, Throwable t) {
\r
85 Logger.defaultLogError(t);
\r
89 public void execute(AsyncReadGraph graph, Object result) {
\r
97 final public void exception(AsyncReadGraph graph, final Throwable throwable) {
\r
98 graph.asyncRequest(new ReadRequest() {
\r
101 public void run(ReadGraph graph) throws DatabaseException {
\r
102 exception((ReadGraph)graph, throwable);
\r