1 /*******************************************************************************
2 * Copyright (c) 2018 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 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.impl.procedure;
14 import java.util.ArrayList;
15 import java.util.concurrent.atomic.AtomicBoolean;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.common.utils.Logger;
19 import org.simantics.db.procedure.SyncMultiProcedure;
21 public class ResultCallWrappedSyncQueryProcedure<Result> implements SyncMultiProcedure<Result> {
23 private final ArrayList<Result> result;
24 private Throwable exception = null;
25 private final SyncMultiProcedure<Result> procedure;
26 private final AtomicBoolean latch;
28 public ResultCallWrappedSyncQueryProcedure(SyncMultiProcedure<Result> procedure) {
29 this.procedure = procedure;
30 latch = new AtomicBoolean(false);
31 result = new ArrayList<Result>();
35 public void execute(ReadGraph graph, Result result) {
37 synchronized(this.result) {
38 this.result.add(result);
40 procedure.execute(graph, result);
41 } catch (Throwable t) {
42 Logger.defaultLogError("AsyncMultiProcedure.execute failed for " + procedure, t);
47 public void finished(ReadGraph graph) {
48 if(latch.compareAndSet(false, true)) {
50 procedure.finished(graph);
51 } catch (Throwable t) {
52 Logger.defaultLogError("AsyncMultiProcedure.exception failed for " + procedure, t);
56 Logger.defaultLogError("Finished or exception was called many times (this time is finished)");
61 public void exception(ReadGraph graph, Throwable t) {
62 if(latch.compareAndSet(false, true)) {
65 procedure.exception(graph, t);
66 } catch (Throwable throwable) {
67 Logger.defaultLogError("AsyncMultiProcedure.exception failed for " + procedure, throwable);
71 Logger.defaultLogError("Finished or exception was called many times (this time is exception)");
75 public ArrayList<Result> get() {
79 public Throwable getException() {
84 public String toString() {
85 return "ResultCallWrappedQueryProcedure4[" + procedure + "]";