]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/single/SingleSetAsyncListener.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / procedure / single / SingleSetAsyncListener.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.common.procedure.single;
13
14 import java.util.Collection;
15 import java.util.HashSet;
16
17 import org.simantics.db.AsyncReadGraph;
18 import org.simantics.db.procedure.AsyncListener;
19
20 abstract public class SingleSetAsyncListener<T> implements AsyncListener<Collection<T>>{
21     
22         final private boolean changesOnly;
23         private Collection<T> previous = null;
24     
25     public void add(AsyncReadGraph graph, T item) {}
26     public void remove(AsyncReadGraph graph, T item) {}
27     
28     public SingleSetAsyncListener() {
29         this(false);
30     }
31     
32     public SingleSetAsyncListener(boolean changesOnly) {
33         this.changesOnly = changesOnly;
34     }
35
36     @Override
37     public void execute(AsyncReadGraph graph, final Collection<T> result) {
38         
39         if(previous == null) {
40
41                 if(!changesOnly) {
42 //                      for(T t : result)
43 //                              add(graph, t);
44 //              } else {
45                         first(graph, result);
46                 }
47
48         } else {
49
50             HashSet<T> added = new HashSet<T>(result);
51             added.removeAll(previous);
52             HashSet<T> removed = new HashSet<T>(previous);
53             removed.removeAll(result);
54
55             for(T t : added) add(graph, t);
56             for(T t : removed) remove(graph, t);
57
58         }
59
60         previous = result;
61         
62     }
63     
64     public void first(AsyncReadGraph graph, final Collection<T> result) {
65         for(T t : result) add(graph, t);
66     }
67         
68 }