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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.db.common.request;
\r
14 import java.util.Collection;
\r
15 import java.util.Set;
\r
16 import java.util.concurrent.ConcurrentLinkedQueue;
\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
25 final public class ObjectsWithTypeAsync extends ResourceAsyncRead3<Collection<Resource>> {
\r
27 public ObjectsWithTypeAsync(Resource subject, Resource subrelationOf, Resource instanceOf) {
\r
28 super(subject, subrelationOf, instanceOf);
\r
32 public void perform(AsyncReadGraph graph, final AsyncProcedure<Collection<Resource>> procedure) {
\r
34 final Collection<Resource> result = new ConcurrentLinkedQueue<Resource>();
\r
35 final ProcedureBarrier<Collection<Resource>> ready = new ProcedureBarrier<Collection<Resource>>(1);
\r
37 graph.forEachObject(resource, resource2, new AsyncMultiProcedure<Resource>() {
\r
40 public void execute(AsyncReadGraph graph, final Resource object) {
\r
42 ready.incrementAndGet();
\r
44 graph.forTypes(object, new AsyncProcedure<Set<Resource>>() {
\r
47 public void execute(AsyncReadGraph graph, Set<Resource> types) {
\r
49 if(types.contains(resource3)) {
\r
53 ready.dec(graph, procedure, result);
\r
58 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
60 throwable.printStackTrace();
\r
61 Logger.defaultLogError(throwable);
\r
63 ready.except(throwable);
\r
64 ready.dec(graph, procedure, result);
\r
73 public void finished(AsyncReadGraph graph) {
\r
75 ready.dec(graph, procedure, result);
\r
80 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
82 throwable.printStackTrace();
\r
83 Logger.defaultLogError(throwable);
\r
85 ready.except(throwable);
\r
86 ready.dec(graph, procedure, result);
\r
95 // public Collection<Resource> perform(ReadGraph graph) throws DatabaseException {
\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