-package org.simantics.graphviz.continuation;\r
-\r
-import java.util.ArrayList;\r
-import java.util.concurrent.CancellationException;\r
-import java.util.concurrent.CountDownLatch;\r
-\r
-public abstract class Computation<T> {\r
- T result;\r
- Exception exception;\r
- ArrayList<Continuation<T>> continuations = new ArrayList<Continuation<T>>(); \r
- CountDownLatch doneGate = new CountDownLatch(1); \r
-\r
- public void addContinuation(Continuation<T> continuation) {\r
- synchronized(doneGate) {\r
- if(doneGate.getCount() == 0) {\r
- if(exception == null)\r
- continuation.succeeded(result);\r
- else\r
- continuation.failed(exception);\r
- }\r
- else\r
- continuations.add(continuation);\r
- }\r
- }\r
-\r
- public boolean isDone() {\r
- return doneGate.getCount() == 0;\r
- }\r
- \r
- protected void failWith(Exception exception) {\r
- this.exception = exception;\r
- ArrayList<Continuation<T>> cs;\r
- synchronized (doneGate) {\r
- cs = this.continuations;\r
- this.continuations = null;\r
- doneGate.countDown();\r
- }\r
- if(cs != null)\r
- for(Continuation<T> continuation : cs)\r
- continuation.failed(exception);\r
- }\r
- \r
- protected void doneWith(T result) {\r
- this.result = result;\r
- ArrayList<Continuation<T>> cs;\r
- synchronized (doneGate) {\r
- cs = this.continuations;\r
- this.continuations = null;\r
- doneGate.countDown();\r
- }\r
- if(cs != null)\r
- for(Continuation<T> continuation : cs)\r
- continuation.succeeded(result);\r
- }\r
- \r
- public void cancel() {\r
- failWith(new CancellationException());\r
- }\r
- \r
- public T get() throws Exception {\r
- doneGate.await();\r
- if(exception != null)\r
- throw exception;\r
- return result;\r
- }\r
-}\r
+package org.simantics.graphviz.continuation;
+
+import java.util.ArrayList;
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.CountDownLatch;
+
+public abstract class Computation<T> {
+ T result;
+ Exception exception;
+ ArrayList<Continuation<T>> continuations = new ArrayList<Continuation<T>>();
+ CountDownLatch doneGate = new CountDownLatch(1);
+
+ public void addContinuation(Continuation<T> continuation) {
+ synchronized(doneGate) {
+ if(doneGate.getCount() == 0) {
+ if(exception == null)
+ continuation.succeeded(result);
+ else
+ continuation.failed(exception);
+ }
+ else
+ continuations.add(continuation);
+ }
+ }
+
+ public boolean isDone() {
+ return doneGate.getCount() == 0;
+ }
+
+ protected void failWith(Exception exception) {
+ this.exception = exception;
+ ArrayList<Continuation<T>> cs;
+ synchronized (doneGate) {
+ cs = this.continuations;
+ this.continuations = null;
+ doneGate.countDown();
+ }
+ if(cs != null)
+ for(Continuation<T> continuation : cs)
+ continuation.failed(exception);
+ }
+
+ protected void doneWith(T result) {
+ this.result = result;
+ ArrayList<Continuation<T>> cs;
+ synchronized (doneGate) {
+ cs = this.continuations;
+ this.continuations = null;
+ doneGate.countDown();
+ }
+ if(cs != null)
+ for(Continuation<T> continuation : cs)
+ continuation.succeeded(result);
+ }
+
+ public void cancel() {
+ failWith(new CancellationException());
+ }
+
+ public T get() throws Exception {
+ doneGate.await();
+ if(exception != null)
+ throw exception;
+ return result;
+ }
+}