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.procedure.AsyncListener;
\r
20 abstract public class SingleSetAsyncListener<T> implements AsyncListener<Collection<T>>{
\r
22 final private boolean changesOnly;
\r
23 private Collection<T> previous = null;
\r
25 public void add(AsyncReadGraph graph, T item) {}
\r
26 public void remove(AsyncReadGraph graph, T item) {}
\r
28 public SingleSetAsyncListener() {
\r
32 public SingleSetAsyncListener(boolean changesOnly) {
\r
33 this.changesOnly = changesOnly;
\r
37 public void execute(AsyncReadGraph graph, final Collection<T> result) {
\r
39 if(previous == null) {
\r
42 // for(T t : result)
\r
45 first(graph, result);
\r
50 HashSet<T> added = new HashSet<T>(result);
\r
51 added.removeAll(previous);
\r
52 HashSet<T> removed = new HashSet<T>(previous);
\r
53 removed.removeAll(result);
\r
55 for(T t : added) add(graph, t);
\r
56 for(T t : removed) remove(graph, t);
\r
64 public void first(AsyncReadGraph graph, final Collection<T> result) {
\r
65 for(T t : result) add(graph, t);
\r