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