]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/request/ObjectsWithTypeAsync.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / request / ObjectsWithTypeAsync.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.request;\r
13 \r
14 import java.util.Collection;\r
15 import java.util.Set;\r
16 import java.util.concurrent.ConcurrentLinkedQueue;\r
17 \r
18 import org.simantics.db.AsyncReadGraph;\r
19 import org.simantics.db.Resource;\r
20 import org.simantics.db.common.ProcedureBarrier;\r
21 import org.simantics.db.common.utils.Logger;\r
22 import org.simantics.db.procedure.AsyncMultiProcedure;\r
23 import org.simantics.db.procedure.AsyncProcedure;\r
24 \r
25 final public class ObjectsWithTypeAsync extends ResourceAsyncRead3<Collection<Resource>> {\r
26 \r
27     public ObjectsWithTypeAsync(Resource subject, Resource subrelationOf, Resource instanceOf) {\r
28         super(subject, subrelationOf, instanceOf);\r
29     }\r
30 \r
31     @Override\r
32     public void perform(AsyncReadGraph graph, final AsyncProcedure<Collection<Resource>> procedure) {\r
33 \r
34         final Collection<Resource> result = new ConcurrentLinkedQueue<Resource>();\r
35         final ProcedureBarrier<Collection<Resource>> ready = new ProcedureBarrier<Collection<Resource>>(1);\r
36         \r
37         graph.forEachObject(resource, resource2, new AsyncMultiProcedure<Resource>() {\r
38 \r
39                         @Override\r
40                         public void execute(AsyncReadGraph graph, final Resource object) {\r
41                                 \r
42                                 ready.incrementAndGet();\r
43                                 \r
44                                 graph.forTypes(object, new AsyncProcedure<Set<Resource>>() {\r
45                                         \r
46                                         @Override\r
47                                         public void execute(AsyncReadGraph graph, Set<Resource> types) {\r
48 \r
49                                                 if(types.contains(resource3)) {\r
50                                                         result.add(object);\r
51                                                 }\r
52                                                 \r
53                                                 ready.dec(graph, procedure, result);\r
54                                                 \r
55                                         }\r
56                                         \r
57                                         @Override\r
58                                         public void exception(AsyncReadGraph graph, Throwable throwable) {\r
59                                                 \r
60                                                 throwable.printStackTrace();\r
61                                         Logger.defaultLogError(throwable);\r
62                                                 \r
63                                                 ready.except(throwable);\r
64                                                 ready.dec(graph, procedure, result);\r
65                                                 \r
66                                         }\r
67                                         \r
68                                 });\r
69                                 \r
70                         }\r
71 \r
72                         @Override\r
73                         public void finished(AsyncReadGraph graph) {\r
74                                 \r
75                                 ready.dec(graph, procedure, result);\r
76                                 \r
77                         }\r
78 \r
79                         @Override\r
80                         public void exception(AsyncReadGraph graph, Throwable throwable) {\r
81                                 \r
82                                 throwable.printStackTrace();\r
83                         Logger.defaultLogError(throwable);\r
84                                 \r
85                                 ready.except(throwable);\r
86                                 ready.dec(graph, procedure, result);\r
87                                 \r
88                         }\r
89                 \r
90         });\r
91         \r
92     }\r
93     \r
94 //    @Override\r
95 //    public Collection<Resource> perform(ReadGraph graph) throws DatabaseException {\r
96 //        \r
97 //      Set<Resource> result = new HashSet<Resource>();\r
98 //      for(Resource object : graph.getObjects(resource, resource2)) {\r
99 //              if(graph.isInstanceOf(object, resource3)) result.add(object);\r
100 //      }\r
101 //      return result;\r
102 //        \r
103 //    }\r
104 \r
105 }\r