]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/request/AsyncMappedParts.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / request / AsyncMappedParts.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.Map;\r
15 import java.util.concurrent.ConcurrentHashMap;\r
16 import java.util.concurrent.atomic.AtomicInteger;\r
17 \r
18 import org.simantics.db.AsyncReadGraph;\r
19 import org.simantics.db.Resource;\r
20 import org.simantics.db.common.procedure.adapter.AsyncMultiProcedureAdapter;\r
21 import org.simantics.db.common.procedure.adapter.AsyncProcedureAdapter;\r
22 import org.simantics.db.procedure.AsyncProcedure;\r
23 import org.simantics.layer0.Layer0;\r
24 import org.simantics.operation.Layer0X;\r
25 \r
26 /**\r
27  * Asynchronous version of {@link MappedParts}.\r
28  * \r
29  * @author Tuukka Lehtonen\r
30  */\r
31 public class AsyncMappedParts extends ResourceAsyncRead<Map<Resource, Resource>> {\r
32 \r
33     public AsyncMappedParts(Resource flat) {\r
34         super(flat);\r
35     }\r
36 \r
37     @Override\r
38     public void perform(AsyncReadGraph graph, final AsyncProcedure<Map<Resource, Resource>> procedure) {\r
39 \r
40         final AtomicInteger done = new AtomicInteger(1);\r
41         final Layer0 l0 = graph.getService(Layer0.class);\r
42         final Layer0X L0X = graph.getService(Layer0X.class);\r
43         final Map<Resource, Resource> result = new ConcurrentHashMap<Resource, Resource>();\r
44 \r
45         graph.forEachObject(resource, l0.ConsistsOf, new AsyncMultiProcedureAdapter<Resource>() {\r
46             @Override\r
47             public void execute(AsyncReadGraph graph, final Resource part) {\r
48                 done.incrementAndGet();\r
49                 graph.forPossibleObject(part, L0X.Represents, new AsyncProcedureAdapter<Resource>() {\r
50                     @Override\r
51                     public void execute(AsyncReadGraph graph, Resource represents) {\r
52                         if (represents != null)\r
53                             result.put(represents, part);\r
54                         if(done.decrementAndGet() == 0) {\r
55                             procedure.execute(graph, result);\r
56                         }\r
57                     }\r
58                 });\r
59             }\r
60             @Override\r
61             public void exception(AsyncReadGraph graph, Throwable t) {\r
62                 procedure.exception(graph, t);\r
63             }\r
64             @Override\r
65             public void finished(AsyncReadGraph graph) {\r
66                 if(done.decrementAndGet() == 0) {\r
67                     procedure.execute(graph, result);\r
68                 }\r
69             }\r
70         });\r
71     }\r
72 \r
73 }\r