]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/single/wrapper/SingleOrNullProcedure.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / procedure / single / wrapper / SingleOrNullProcedure.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.procedure.single.wrapper;
13
14 import java.util.concurrent.atomic.AtomicBoolean;
15
16 import org.simantics.db.AsyncReadGraph;
17 import org.simantics.db.common.procedure.adapter.AsyncMultiProcedureAdapter;
18 import org.simantics.db.common.utils.Logger;
19 import org.simantics.db.procedure.AsyncProcedure;
20
21 final public class SingleOrNullProcedure<Result> extends AsyncMultiProcedureAdapter<Result> {
22     
23         private Result result = null;
24         final AsyncProcedure<Result> procedure;
25     final AtomicBoolean found = new AtomicBoolean(false);
26     final AtomicBoolean done = new AtomicBoolean(false);
27     
28     public SingleOrNullProcedure(AsyncProcedure<Result> procedure) {
29         this.procedure = procedure;
30     }
31     
32     @Override
33     public void finished(AsyncReadGraph graph) {
34
35         // Shall fire exactly once!
36         if(done.compareAndSet(false, true)) {
37                 try {
38                         procedure.execute(graph, result);
39                 } catch (Throwable t) {
40                         Logger.defaultLogError(t);
41                 }
42         }
43         
44     }
45
46     @Override
47     public void execute(AsyncReadGraph graph, Result result) {
48         
49                 if(found.compareAndSet(false, true)) {
50                         this.result = result;
51                 } else {
52                         // Shall fire exactly once!
53                         if(done.compareAndSet(false, true)) {
54                                 try {
55                                 procedure.execute(graph, null);
56                                 } catch (Throwable t) {
57                                 Logger.defaultLogError(t);
58                                 }
59                         }
60                 }
61         
62     }
63         
64     
65     @Override
66     public void exception(AsyncReadGraph graph, Throwable t) {
67                 // Shall fire exactly once!
68                 if(done.compareAndSet(false, true)) {
69                         try {
70                                 procedure.exception(graph, t);
71                         } catch (Throwable t2) {
72                         Logger.defaultLogError(t2);
73                         }
74                 }
75     }
76     
77     @Override
78     public String toString() {
79         return "SingleOrNullProcedure -> " + procedure;
80     }
81
82 }