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