]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/request/ObjectsWithTypeAsync.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/request/ObjectsWithTypeAsync.java
new file mode 100644 (file)
index 0000000..4287506
--- /dev/null
@@ -0,0 +1,105 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.db.common.request;\r
+\r
+import java.util.Collection;\r
+import java.util.Set;\r
+import java.util.concurrent.ConcurrentLinkedQueue;\r
+\r
+import org.simantics.db.AsyncReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.ProcedureBarrier;\r
+import org.simantics.db.common.utils.Logger;\r
+import org.simantics.db.procedure.AsyncMultiProcedure;\r
+import org.simantics.db.procedure.AsyncProcedure;\r
+\r
+final public class ObjectsWithTypeAsync extends ResourceAsyncRead3<Collection<Resource>> {\r
+\r
+    public ObjectsWithTypeAsync(Resource subject, Resource subrelationOf, Resource instanceOf) {\r
+        super(subject, subrelationOf, instanceOf);\r
+    }\r
+\r
+    @Override\r
+    public void perform(AsyncReadGraph graph, final AsyncProcedure<Collection<Resource>> procedure) {\r
+\r
+       final Collection<Resource> result = new ConcurrentLinkedQueue<Resource>();\r
+       final ProcedureBarrier<Collection<Resource>> ready = new ProcedureBarrier<Collection<Resource>>(1);\r
+       \r
+       graph.forEachObject(resource, resource2, new AsyncMultiProcedure<Resource>() {\r
+\r
+                       @Override\r
+                       public void execute(AsyncReadGraph graph, final Resource object) {\r
+                               \r
+                               ready.incrementAndGet();\r
+                               \r
+                               graph.forTypes(object, new AsyncProcedure<Set<Resource>>() {\r
+                                       \r
+                                       @Override\r
+                                       public void execute(AsyncReadGraph graph, Set<Resource> types) {\r
+\r
+                                               if(types.contains(resource3)) {\r
+                                                       result.add(object);\r
+                                               }\r
+                                               \r
+                                               ready.dec(graph, procedure, result);\r
+                                               \r
+                                       }\r
+                                       \r
+                                       @Override\r
+                                       public void exception(AsyncReadGraph graph, Throwable throwable) {\r
+                                               \r
+                                               throwable.printStackTrace();\r
+                                       Logger.defaultLogError(throwable);\r
+                                               \r
+                                               ready.except(throwable);\r
+                                               ready.dec(graph, procedure, result);\r
+                                               \r
+                                       }\r
+                                       \r
+                               });\r
+                               \r
+                       }\r
+\r
+                       @Override\r
+                       public void finished(AsyncReadGraph graph) {\r
+                               \r
+                               ready.dec(graph, procedure, result);\r
+                               \r
+                       }\r
+\r
+                       @Override\r
+                       public void exception(AsyncReadGraph graph, Throwable throwable) {\r
+                               \r
+                               throwable.printStackTrace();\r
+                       Logger.defaultLogError(throwable);\r
+                               \r
+                               ready.except(throwable);\r
+                               ready.dec(graph, procedure, result);\r
+                               \r
+                       }\r
+               \r
+       });\r
+       \r
+    }\r
+    \r
+//    @Override\r
+//    public Collection<Resource> perform(ReadGraph graph) throws DatabaseException {\r
+//        \r
+//     Set<Resource> result = new HashSet<Resource>();\r
+//     for(Resource object : graph.getObjects(resource, resource2)) {\r
+//             if(graph.isInstanceOf(object, resource3)) result.add(object);\r
+//     }\r
+//     return result;\r
+//        \r
+//    }\r
+\r
+}\r