]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/procedure/ResultCallWrappedQueryProcedure4.java
Merge "Use Consumer interface instead of deprecated Callback interface"
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / procedure / ResultCallWrappedQueryProcedure4.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 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  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.impl.procedure;
13
14 import java.util.ArrayList;
15 import java.util.concurrent.atomic.AtomicBoolean;
16
17 import org.simantics.db.AsyncReadGraph;
18 import org.simantics.db.common.utils.Logger;
19 import org.simantics.db.procedure.AsyncMultiProcedure;
20
21 public class ResultCallWrappedQueryProcedure4<Result> implements AsyncMultiProcedure<Result> {
22
23     final private ArrayList<Result> result;
24     private Throwable exception = null;
25     final private AsyncMultiProcedure<Result> procedure;
26     final private AtomicBoolean latch;
27     
28     public ResultCallWrappedQueryProcedure4(AsyncMultiProcedure<Result> procedure) {
29         this.procedure = procedure;
30         latch = new AtomicBoolean(false);
31         result = new ArrayList<Result>();
32     }
33     
34     @Override
35     public void execute(AsyncReadGraph graph, Result result) {
36         try {
37             synchronized(this.result) {
38                 this.result.add(result);
39             }
40                 procedure.execute(graph, result);
41         } catch (Throwable t) {
42                 Logger.defaultLogError("AsyncMultiProcedure.execute failed for " + procedure, t);
43         }
44     }
45     
46     @Override
47     public void finished(AsyncReadGraph graph) {
48         if(latch.compareAndSet(false, true)) {
49                 try {
50                 procedure.finished(graph);
51                 } catch (Throwable t) {
52                         Logger.defaultLogError("AsyncMultiProcedure.exception failed for " + procedure, t);
53                 } finally {
54                 }
55         } else {
56                 Logger.defaultLogError("Finished or exception was called many times (this time is finished)");
57         }
58     }
59
60     @Override
61     public void exception(AsyncReadGraph graph, Throwable t) {
62         if(latch.compareAndSet(false, true)) {
63                 try {
64                 this.exception = t;
65                 procedure.exception(graph, t);
66                 } catch (Throwable throwable) {
67                         Logger.defaultLogError("AsyncMultiProcedure.exception failed for " + procedure, throwable);
68                 } finally {
69                 }
70         } else {
71                 Logger.defaultLogError("Finished or exception was called many times (this time is exception)");
72         }
73     }
74     
75     public ArrayList<Result> getResult() {
76         return result;
77     }
78     
79     public Throwable getException() {
80         return exception;
81     }
82
83     @Override
84     public String toString() {
85         return "ResultCallWrappedQueryProcedure4[" + procedure + "]"; 
86     }
87     
88 }