]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/BlockingAsyncMultiProcedure.java
Merge "Made spatial picking optimization optional for now"
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / BlockingAsyncMultiProcedure.java
1 /*******************************************************************************
2  * Copyright (c) 2018 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  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.impl;
13
14 import org.simantics.db.AsyncReadGraph;
15 import org.simantics.db.common.utils.Logger;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.impl.graph.ReadGraphImpl;
18 import org.simantics.db.procedure.AsyncMultiProcedure;
19
20 public class BlockingAsyncMultiProcedure<Result> implements AsyncMultiProcedure<Result> {
21
22     private static final Object NO_RESULT = new Object();
23
24     private final Object key;
25     private final ReadGraphImpl graph;
26     private final AsyncMultiProcedure<Result> procedure;
27
28     private Object result = NO_RESULT;
29     private Throwable exception = null;
30
31     public BlockingAsyncMultiProcedure(ReadGraphImpl graph, AsyncMultiProcedure<Result> procedure, Object key) {
32         this.procedure = procedure;
33         this.key = key;
34         this.graph = graph;
35         this.graph.asyncBarrier.inc();
36     }
37
38     @Override
39     public void execute(AsyncReadGraph graph, Result result) {
40         this.result = result;
41         try {
42             if(procedure != null) procedure.execute(graph, result);
43         } catch (Throwable throwable) {
44             Logger.defaultLogError("AsyncProcedure.execute threw for " + procedure, throwable);
45         }
46     }
47
48     @Override
49     public void finished(AsyncReadGraph graph) {
50         this.graph.asyncBarrier.dec();
51         try {
52             if(procedure != null) procedure.finished(graph);
53         } catch (Throwable throwable) {
54             Logger.defaultLogError("AsyncProcedure.finish threw for " + procedure, throwable);
55         }
56     }
57
58     @Override
59     public void exception(AsyncReadGraph graph, Throwable t) {
60         this.exception = t;
61         try {
62             if (procedure != null) procedure.exception(graph, t);
63         } catch (Throwable throwable) {
64             Logger.defaultLogError("AsyncProcedure.exception threw for " + procedure, throwable);
65         } finally {
66             this.graph.asyncBarrier.dec();
67         }
68     }
69
70     @SuppressWarnings("unchecked")
71     public Result get() throws DatabaseException {
72         graph.asyncBarrier.waitBarrier(key, graph);
73         if (exception != null) {
74             if (exception instanceof DatabaseException) throw (DatabaseException) exception;
75             throw new DatabaseException(exception);
76         } else {
77             return (Result) result;
78         }
79     }
80
81     @SuppressWarnings("unchecked")
82     public Result getResult() {
83         return (Result) result;
84     }
85
86     public Throwable getException() {
87         return exception;
88     }
89
90     @Override
91     public String toString() {
92         return "." + procedure; 
93     }
94
95 }