]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/single/SingleSetListener.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / procedure / single / SingleSetListener.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.db.common.procedure.single;\r
13 \r
14 import java.util.Collection;\r
15 import java.util.HashSet;\r
16 \r
17 import org.simantics.db.Disposable;\r
18 import org.simantics.db.procedure.Listener;\r
19 import org.simantics.db.procedure.SetListener;\r
20 \r
21 abstract public class SingleSetListener<T> implements Listener<Collection<T>>, SetListener<T>, Disposable {\r
22         \r
23     private boolean disposed = false;\r
24         \r
25     private Collection<T> previous = null;\r
26     \r
27     public void add(T result) {};\r
28     public void remove(T result) {};\r
29     \r
30     @Override\r
31     public void execute(Collection<T> result) {\r
32         \r
33         if(result == null) result = new HashSet<T>();\r
34         \r
35         if(previous == null) {\r
36 \r
37             for(T t : result) add(t);\r
38 \r
39         } else {\r
40 \r
41             HashSet<T> added = new HashSet<T>(result);\r
42             added.removeAll(previous);\r
43             HashSet<T> removed = new HashSet<T>(previous);\r
44             removed.removeAll(result);\r
45 \r
46             for(T t : added) add(t);\r
47             for(T t : removed) remove(t);\r
48 \r
49         }\r
50 \r
51         previous = result;\r
52         \r
53     }\r
54         \r
55     public void dispose() {\r
56         disposed = true;\r
57     }\r
58 \r
59     @Override\r
60     public boolean isDisposed() {\r
61         return disposed;\r
62     }\r
63     \r
64 }\r