]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/request/DelayedWriteResultRequest.java
94bd4c04ba1cb3cdc9cf8607ba9d7bd55e832040
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / request / DelayedWriteResultRequest.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.common.request;
13
14 import org.simantics.db.AsyncRequestProcessor;
15 import org.simantics.db.RequestProcessor;
16 import org.simantics.db.VirtualGraph;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.procedure.Procedure;
19 import org.simantics.db.request.DelayedWriteResult;
20 import org.simantics.db.request.UndoTraits;
21 import org.simantics.db.request.WriteInterface;
22 import org.simantics.db.request.WriteTraits;
23
24
25 /**
26  * Usage samples:
27  * 
28  * <p>
29  * Synchronous read without checked exceptions:
30  * </p>
31  * <pre>
32  * Resource r = new SimpleReadGraphRequest&lt;Resource&gt;(session) {
33  *     {@code @Override}
34  *     public Resource run(Graph g) {
35  *         Resource r = doSomethingThatMayFail();
36  *         return r;
37  *     }
38  * }.sync();
39  * </pre>
40  * 
41  * <p>
42  * Synchronous read with a selected checked exception that may be thrown:
43  * </p>
44  * <pre>
45  * Resource r = new SimpleReadGraphRequest&lt;Resource&gt;(session) {
46  *     {@code @Override}
47  *     public Resource run(Graph g) throws CheckedException {
48  *         Resource r = doSomethingThatMayFailWithCheckedException();
49  *         return r;
50  *     }
51  * }.sync(CheckedException.class);
52  * </pre>
53  * 
54  * <p>
55  * Asynchronous read with a completed() method for examining the result:
56  * </p>
57  * <pre>
58  * Resource r = new SimpleReadGraphRequest&lt;Resource&gt;(session) {
59  *     {@code @Override}
60  *     public Resource run(Graph g) {
61  *         Resource r = doSomethingThatMayFail();
62  *         return r;
63  *     }
64  *     public void completed(Resource result) {
65  *         // quickly dispatch the result somewhere, do nothing serious in this code.
66  *     }
67  * }.async();
68  * </pre>
69  * 
70  * @param <T> the result type
71  */
72 public abstract class DelayedWriteResultRequest<T> implements DelayedWriteResult<T>, WriteTraits, WriteInterface<T> {
73
74     private final VirtualGraph provider;
75
76     public DelayedWriteResultRequest() {
77         this.provider = null;
78     }
79
80     public DelayedWriteResultRequest(VirtualGraph provider) {
81         this.provider = provider;
82     }
83     
84     @Override
85     final public VirtualGraph getProvider() {
86         return provider;
87     }
88     
89     @Override
90     public UndoTraits getUndoTraits() {
91         return null;
92     }
93
94     @Override
95     public void request(AsyncRequestProcessor processor, final Procedure<T> procedure) {
96         processor.asyncRequest(this, procedure);
97     }
98     
99     @Override
100     public T request(RequestProcessor processor) throws DatabaseException {
101         return processor.syncRequest(this);
102     }
103     
104 }