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