--- /dev/null
+/*******************************************************************************\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.HashSet;\r
+import java.util.Set;\r
+\r
+import org.simantics.db.procedure.Listener;\r
+\r
+abstract public class StagedSingleSetListener<T> implements Listener<Set<T>>{\r
+ \r
+ private Set<T> previous = null;\r
+ \r
+ public void start(Set<T> items) {}\r
+ public void add(T item) {}\r
+ public void remove(T item) {}\r
+ \r
+ @Override\r
+ public synchronized void execute(final Set<T> result) {\r
+ \r
+ if(previous == null) {\r
+\r
+ start(result);\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(t);\r
+ for(T t : removed) remove(t);\r
+\r
+ }\r
+\r
+ previous = result;\r
+ \r
+ }\r
+ \r
+}\r