OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 132 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 132 Rev 177
Line 12... Line 12...
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.utils.cc;
14
 package org.openconcerto.utils.cc;
15
 
15
 
16
import org.openconcerto.utils.Value;
16
import org.openconcerto.utils.Value;
-
 
17
import org.openconcerto.utils.cc.ExceptionFuture.OneExceptionFuture;
17
 
18
 
18
import java.util.concurrent.Callable;
-
 
19
import java.util.concurrent.ExecutionException;
19
import java.util.concurrent.ExecutionException;
20
import java.util.concurrent.FutureTask;
-
 
21
import java.util.concurrent.TimeUnit;
20
import java.util.concurrent.TimeUnit;
22
import java.util.concurrent.TimeoutException;
21
import java.util.concurrent.TimeoutException;
23
import java.util.concurrent.atomic.AtomicReference;
22
import java.util.concurrent.atomic.AtomicReference;
24
 
23
 
25
public class TransformerFuture<E, T, X extends Exception> implements ITransformerFuture<E, T, X> {
24
public class TransformerFuture<E, T, X extends Exception> implements ITransformerFuture<E, T, X> {
26
 
25
 
27
    private final AtomicReference<Value<E>> input;
26
    private final AtomicReference<Value<E>> input;
28
    private final FutureTask<T> f;
27
    private final OneExceptionFuture<T, X> f;
29
 
28
 
30
    public TransformerFuture(final ITransformerExn<? super E, ? extends T, ? extends X> cl) {
29
    public TransformerFuture(final ITransformerExn<? super E, ? extends T, ? extends X> cl) {
31
        super();
30
        super();
32
        this.f = new FutureTask<T>(new Callable<T>() {
31
        this.f = new OneExceptionFuture<T, X>(new IExnFactory<T, X>() {
33
            @Override
32
            @Override
34
            public T call() throws X {
33
            public T createChecked() throws X {
35
                return cl.transformChecked(TransformerFuture.this.input.get().getValue());
34
                return cl.transformChecked(TransformerFuture.this.input.get().getValue());
36
            }
35
            }
37
        });
36
        });
38
        this.input = new AtomicReference<Value<E>>(Value.<E> getNone());
37
        this.input = new AtomicReference<Value<E>>(Value.<E> getNone());
39
    }
38
    }
Line 68... Line 67...
68
        if (!this.input.compareAndSet(Value.<E> getNone(), Value.getSome(input)))
67
        if (!this.input.compareAndSet(Value.<E> getNone(), Value.getSome(input)))
69
            throw new IllegalStateException("Already run");
68
            throw new IllegalStateException("Already run");
70
        this.f.run();
69
        this.f.run();
71
        assert this.f.isDone();
70
        assert this.f.isDone();
72
        try {
71
        try {
73
            return this.f.get();
72
            return this.f.getWithOriginalException();
74
        } catch (InterruptedException e) {
73
        } catch (InterruptedException e) {
75
            // shouldn't happen since f is done
74
            // shouldn't happen since f is done
76
            throw new IllegalStateException(e);
75
            throw new IllegalStateException(e);
77
        } catch (ExecutionException e) {
-
 
78
            final Throwable cause = e.getCause();
-
 
79
            if (cause instanceof Error) {
-
 
80
                throw (Error) cause;
-
 
81
            } else if (cause instanceof RuntimeException) {
-
 
82
                throw (RuntimeException) cause;
-
 
83
            } else {
-
 
84
                // our Callable throws X
-
 
85
                @SuppressWarnings("unchecked")
-
 
86
                final X casted = (X) cause;
-
 
87
                throw casted;
-
 
88
            }
-
 
89
        }
76
        }
90
    }
77
    }
91
}
78
}