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