]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/procedure/ResultCallWrappedSingleQueryProcedure4.java
Use Consumer interface instead of deprecated Callback interface
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / procedure / ResultCallWrappedSingleQueryProcedure4.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.concurrent.atomic.AtomicBoolean;
15
16 import org.simantics.db.AsyncReadGraph;
17 import org.simantics.db.common.utils.Logger;
18 import org.simantics.db.procedure.AsyncProcedure;
19
20 public class ResultCallWrappedSingleQueryProcedure4<Result> implements AsyncProcedure<Result> {
21
22         final private Object key;
23     private Result result = null;
24     private Throwable exception = null;
25     final private AsyncProcedure<Result> procedure;
26     final private AtomicBoolean latch;
27     
28     public ResultCallWrappedSingleQueryProcedure4(AsyncProcedure<Result> procedure, Object key) {
29         assert(procedure != null);
30         this.key = key;
31         this.procedure = procedure;
32         latch = new AtomicBoolean(false);
33     }
34     
35     @Override
36     public void execute(AsyncReadGraph graph, Result result) {
37         this.result = result;
38         if(latch.compareAndSet(false, true)) {
39                 try {
40                         procedure.execute(graph, result);
41                 } catch (Throwable throwable) {
42                 Logger.defaultLogError("AsyncProcedure.execute threw for " + procedure, throwable);
43                 } finally {
44 //                      System.err.println("ResultCallWrappedSingleQueryProcedure4 dec " + key);
45                 }
46         } else {
47                 Logger.defaultLogError("Procedure was called many times (this time is execute)");
48         }
49     }
50
51     @Override
52     public void exception(AsyncReadGraph graph, Throwable t) {
53         this.exception = t;
54         if(latch.compareAndSet(false, true)) {
55                 try {
56                         procedure.exception(graph, t);
57                 } catch (Throwable throwable) {
58                 Logger.defaultLogError("AsyncProcedure.exception threw for " + procedure, throwable);
59                 } finally {
60                 }
61         } else {
62                 Logger.defaultLogError("Procedure was called many times (this time is exception)");
63         }
64         
65     }
66     
67     public Result getResult() {
68         return result;
69     }
70     
71     public Throwable getException() {
72         return exception;
73     }
74         
75     @Override
76     public String toString() {
77         return "." + procedure; 
78     }
79     
80 }