]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/procedure/CallWrappedSingleQueryProcedure4.java
Use Consumer interface instead of deprecated Callback interface
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / procedure / CallWrappedSingleQueryProcedure4.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 CallWrappedSingleQueryProcedure4<Result> implements AsyncProcedure<Result> {
21
22         final private Object key;
23     final private AsyncProcedure<Result> procedure;
24     final private AtomicBoolean latch = new AtomicBoolean(false);
25     
26     public CallWrappedSingleQueryProcedure4(AsyncProcedure<Result> procedure, Object key) {
27         this.procedure = procedure;
28         this.key = key;
29     }
30     
31     @Override
32     public void execute(AsyncReadGraph graph, Result result) {
33         if(latch.compareAndSet(false, true)) {
34                 try {
35                         procedure.execute(graph, result);
36                 } catch (Throwable t) {
37                         Logger.defaultLogError("AsyncProcedure.exception failed for " + procedure, t);
38                 } finally {
39                 }
40         } else {
41                 Logger.defaultLogError("Execute or exception was called many times (this time is execute)");
42         }
43     }
44
45     @Override
46     public void exception(AsyncReadGraph graph, Throwable t) {
47         if(latch.compareAndSet(false, true)) {
48                 try {
49                         procedure.exception(graph, t);
50                 } catch (Throwable throwable) {
51                         Logger.defaultLogError("AsyncProcedure.exception failed for " + procedure, throwable);
52                 } finally {
53                 }
54         } else {
55                 Logger.defaultLogError("Execute or exception was called many times (this time is exception)");
56         }
57     }
58         
59 }