]> gerrit.simantics Code Review - simantics/platform.git/blob
cd6866d6fef29e94c8a50204a5cb5e764e7e8ca0
[simantics/platform.git] /
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.common.procedure.wrapper;
13
14 import org.simantics.db.AsyncReadGraph;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.common.request.ReadRequest;
17 import org.simantics.db.common.utils.Logger;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.db.procedure.AsyncListener;
20 import org.simantics.db.procedure.Procedure;
21 import org.simantics.db.procedure.SyncListener;
22
23 final public class SyncToAsyncListener<T> implements AsyncListener<T> {
24
25         final private SyncListener<T> procedure;
26
27         public SyncToAsyncListener(SyncListener<T> procedure) {
28                 this.procedure = procedure;
29         }
30         
31         @Override
32         final public void execute(AsyncReadGraph graph, final T result) {
33 //          System.out.println("SyncToAsyncListener for " + procedure + " - execute " + result);
34                 graph.asyncRequest(new ReadRequest() {
35
36                         @Override
37                         public void run(ReadGraph graph) throws DatabaseException {
38 //                      System.out.println("SyncToAsyncListener for " + procedure + " - execute " + result);
39                                 procedure.execute(graph, result);
40                         }
41                         
42                         @Override
43                         public String toString() {
44                             return "execute for " + SyncToAsyncListener.this.toString();
45                         }
46                         
47                 }, new Procedure<Object>() {
48
49             @Override
50             public void exception(Throwable t) {
51                         Logger.defaultLogError(t);
52             }
53
54             @Override
55             public void execute(Object result) {
56             }
57                     
58                 });
59         }
60
61         @Override
62         final public void exception(AsyncReadGraph graph, final Throwable t) {
63         graph.asyncRequest(new ReadRequest() {
64
65             @Override
66             public void run(ReadGraph graph) throws DatabaseException {
67                 procedure.exception(graph, t);
68             }
69             
70         });
71         }
72
73         @Override
74         public boolean isDisposed() {
75             return procedure.isDisposed();
76         }
77         
78     @Override
79     public String toString() {
80         return "SyncToAsyncListener -> " + procedure;
81    }
82         
83 }