]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/request/WriteOnlyRequest.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / request / WriteOnlyRequest.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.WriteOnly;
21
22
23 /**
24  * TODO: fix this javadoc to document the correct class!!
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 WriteOnlyRequest implements WriteOnly, WriteInterface<Object> {
73
74     private final VirtualGraph provider;
75
76     public WriteOnlyRequest() {
77         this.provider = null;
78     }
79
80     public WriteOnlyRequest(VirtualGraph provider) {
81         this.provider = provider;
82     }
83
84     @Override
85     final public VirtualGraph getProvider() {
86         return provider;
87     }
88
89     @Override
90     public void request(AsyncRequestProcessor processor, final Procedure<Object> procedure) {
91         processor.asyncRequest(this, parameter -> {
92                 if(parameter != null) procedure.exception(parameter);
93                 else procedure.execute(null);
94                 });
95     }
96     
97     @Override
98     public Object request(RequestProcessor processor) throws DatabaseException {
99         processor.syncRequest(this);
100         return null;
101     }
102     
103 }