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