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