]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/single/StagedSingleSetSyncListener.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / procedure / single / StagedSingleSetSyncListener.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.ReadGraph;\r
18 import org.simantics.db.common.utils.Logger;\r
19 import org.simantics.db.exception.DatabaseException;\r
20 import org.simantics.db.procedure.SyncListener;\r
21 \r
22 abstract public class StagedSingleSetSyncListener<T> implements SyncListener<Collection<T>>{\r
23     \r
24     private Collection<T> previous = null;\r
25     \r
26     public void start(ReadGraph graph, Collection<T> items) throws DatabaseException {}\r
27     public void add(ReadGraph graph, T item) throws DatabaseException {}\r
28     public void remove(ReadGraph graph, T item) throws DatabaseException {}\r
29     \r
30     @Override\r
31     public synchronized void execute(ReadGraph graph, final Collection<T> result) throws DatabaseException {\r
32         \r
33         if(previous == null) {\r
34 \r
35             start(graph, result);\r
36 \r
37         } else {\r
38 \r
39             HashSet<T> added = new HashSet<T>(result);\r
40             added.removeAll(previous);\r
41             HashSet<T> removed = new HashSet<T>(previous);\r
42             removed.removeAll(result);\r
43 \r
44             for(T t : added) add(graph, t);\r
45             for(T t : removed) remove(graph, t);\r
46 \r
47         }\r
48 \r
49         previous = result;\r
50         \r
51     }\r
52     \r
53     @Override\r
54     public void exception(ReadGraph graph, Throwable throwable) throws DatabaseException {\r
55                 Logger.defaultLogError(throwable);\r
56     }\r
57     \r
58 }\r