1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.common.request;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.concurrent.atomic.AtomicInteger;
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;
27 * Asynchronous version of {@link MappedParts}.
29 * @author Tuukka Lehtonen
31 public class AsyncMappedParts extends ResourceAsyncRead<Map<Resource, Resource>> {
33 public AsyncMappedParts(Resource flat) {
38 public void perform(AsyncReadGraph graph, final AsyncProcedure<Map<Resource, Resource>> procedure) {
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>();
45 graph.forEachObject(resource, l0.ConsistsOf, new AsyncMultiProcedureAdapter<Resource>() {
47 public void execute(AsyncReadGraph graph, final Resource part) {
48 done.incrementAndGet();
49 graph.forPossibleObject(part, L0X.Represents, new AsyncProcedureAdapter<Resource>() {
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);
61 public void exception(AsyncReadGraph graph, Throwable t) {
62 procedure.exception(graph, t);
65 public void finished(AsyncReadGraph graph) {
66 if(done.decrementAndGet() == 0) {
67 procedure.execute(graph, result);