]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/single/SingleSetAsyncListener.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / procedure / single / SingleSetAsyncListener.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.AsyncReadGraph;\r
18 import org.simantics.db.procedure.AsyncListener;\r
19 \r
20 abstract public class SingleSetAsyncListener<T> implements AsyncListener<Collection<T>>{\r
21     \r
22         final private boolean changesOnly;\r
23         private Collection<T> previous = null;\r
24     \r
25     public void add(AsyncReadGraph graph, T item) {}\r
26     public void remove(AsyncReadGraph graph, T item) {}\r
27     \r
28     public SingleSetAsyncListener() {\r
29         this(false);\r
30     }\r
31     \r
32     public SingleSetAsyncListener(boolean changesOnly) {\r
33         this.changesOnly = changesOnly;\r
34     }\r
35 \r
36     @Override\r
37     public void execute(AsyncReadGraph graph, final Collection<T> result) {\r
38         \r
39         if(previous == null) {\r
40 \r
41                 if(!changesOnly) {\r
42 //                      for(T t : result)\r
43 //                              add(graph, t);\r
44 //              } else {\r
45                         first(graph, result);\r
46                 }\r
47 \r
48         } else {\r
49 \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
54 \r
55             for(T t : added) add(graph, t);\r
56             for(T t : removed) remove(graph, t);\r
57 \r
58         }\r
59 \r
60         previous = result;\r
61         \r
62     }\r
63     \r
64     public void first(AsyncReadGraph graph, final Collection<T> result) {\r
65         for(T t : result) add(graph, t);\r
66     }\r
67         \r
68 }\r