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