]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/ProcedureBarrier.java
Improved subscription CSV export wizard page initial selection handling
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / ProcedureBarrier.java
1 package org.simantics.db.common;
2
3 import java.util.concurrent.atomic.AtomicInteger;
4
5 import org.simantics.db.AsyncReadGraph;
6 import org.simantics.db.procedure.AsyncProcedure;
7
8 final public class ProcedureBarrier<T> extends AtomicInteger {
9
10         private static final long serialVersionUID = -2699547741948233067L;
11         
12         private volatile Throwable throwable;
13         
14         public ProcedureBarrier(int initial) {
15                 super(initial);
16         }
17         
18         final synchronized public void except(Throwable throwable) {
19                 this.throwable = throwable;
20         }
21         
22         final public Throwable getThrowable() {
23                 return throwable;
24         }
25         
26         final public void dec(AsyncReadGraph graph, AsyncProcedure<T> procedure, Throwable throwable) {
27                 synchronized(this) {
28                         this.throwable = throwable;
29                 }
30                 if(decrementAndGet() == 0) {
31                         procedure.exception(graph, throwable);
32                 }
33         }
34
35         final public void dec(AsyncReadGraph graph, AsyncProcedure<T> procedure, T result) {
36                 if(decrementAndGet() == 0) {
37                         if(throwable != null) procedure.exception(graph, throwable);
38                         else procedure.execute(graph, result);
39                 }
40         }
41         
42 }