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