]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/single/SingleSetListener.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / procedure / single / SingleSetListener.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.Disposable;
18 import org.simantics.db.procedure.Listener;
19 import org.simantics.db.procedure.SetListener;
20
21 abstract public class SingleSetListener<T> implements Listener<Collection<T>>, SetListener<T>, Disposable {
22         
23     private boolean disposed = false;
24         
25     private Collection<T> previous = null;
26     
27     public void add(T result) {};
28     public void remove(T result) {};
29     
30     @Override
31     public void execute(Collection<T> result) {
32         
33         if(result == null) result = new HashSet<T>();
34         
35         if(previous == null) {
36
37             for(T t : result) add(t);
38
39         } else {
40
41             HashSet<T> added = new HashSet<T>(result);
42             added.removeAll(previous);
43             HashSet<T> removed = new HashSet<T>(previous);
44             removed.removeAll(result);
45
46             for(T t : added) add(t);
47             for(T t : removed) remove(t);
48
49         }
50
51         previous = result;
52         
53     }
54         
55     public void dispose() {
56         disposed = true;
57     }
58
59     @Override
60     public boolean isDisposed() {
61         return disposed;
62     }
63     
64 }