]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/single/SingleSetSyncListener.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / procedure / single / SingleSetSyncListener.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.ReadGraph;
19 import org.simantics.db.common.request.ReadRequest;
20 import org.simantics.db.common.utils.Logger;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.procedure.AsyncListener;
23 import org.simantics.db.procedure.AsyncProcedure;
24
25 abstract public class SingleSetSyncListener<T> implements AsyncListener<Collection<T>>{
26     
27     private Collection<T> previous = null;
28     
29     /**
30      * @param graph
31      * @return <code>true</code> to continue, <code>false</code> to end processing here
32      * @throws DatabaseException
33      */
34     public boolean start(ReadGraph graph) throws DatabaseException { return true; }
35     public void add(ReadGraph graph, T item) throws DatabaseException {}
36     public void remove(ReadGraph graph,T item) throws DatabaseException {}
37     public void finished(ReadGraph graph) throws DatabaseException {}
38
39     public void exception(ReadGraph graph, Throwable t) throws DatabaseException {
40                 Logger.defaultLogError(t);
41     }
42     
43     @Override
44     final public void execute(AsyncReadGraph graph, final Collection<T> result) {
45         
46         if(result == null) {
47                 Logger.defaultLogError("SingleSetSyncListener does not accept null result.");
48                 return;
49         }
50         
51         graph.asyncRequest(new ReadRequest() {
52
53                         @Override
54                         public void run(ReadGraph graph) throws DatabaseException {
55
56                                 if (!start(graph))
57                                         return;
58
59                                 if(previous == null) {
60
61                                         for(T t : result) add(graph, t);
62
63                                 } else {
64
65                                         HashSet<T> added = new HashSet<T>(result);
66                                         added.removeAll(previous);
67                                         HashSet<T> removed = new HashSet<T>(previous);
68                                         removed.removeAll(result);
69
70                                         for(T t : added) add(graph, t);
71                                         for(T t : removed) remove(graph, t);
72
73                                 }
74
75                                 finished(graph);
76
77                                 previous = result;
78                                 
79                         }
80                 
81         }, new AsyncProcedure<Object>() {
82
83                         @Override
84                         public void exception(AsyncReadGraph graph, Throwable t) {
85                         Logger.defaultLogError(t);
86                         }
87
88             @Override
89             public void execute(AsyncReadGraph graph, Object result) {
90             }
91                 
92         });
93         
94     }
95     
96     @Override
97     final public void exception(AsyncReadGraph graph, final Throwable throwable) {
98         graph.asyncRequest(new ReadRequest() {
99
100             @Override
101             public void run(ReadGraph graph) throws DatabaseException {
102                 exception((ReadGraph)graph, throwable);
103             }
104
105         });
106     }
107         
108 }