OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 132 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
132 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.utils.cc;
15
 
16
import org.openconcerto.utils.Value;
177 ilm 17
import org.openconcerto.utils.cc.ExceptionFuture.OneExceptionFuture;
132 ilm 18
 
19
import java.util.concurrent.ExecutionException;
20
import java.util.concurrent.TimeUnit;
21
import java.util.concurrent.TimeoutException;
22
import java.util.concurrent.atomic.AtomicReference;
23
 
24
public class TransformerFuture<E, T, X extends Exception> implements ITransformerFuture<E, T, X> {
25
 
26
    private final AtomicReference<Value<E>> input;
177 ilm 27
    private final OneExceptionFuture<T, X> f;
132 ilm 28
 
29
    public TransformerFuture(final ITransformerExn<? super E, ? extends T, ? extends X> cl) {
30
        super();
177 ilm 31
        this.f = new OneExceptionFuture<T, X>(new IExnFactory<T, X>() {
132 ilm 32
            @Override
177 ilm 33
            public T createChecked() throws X {
132 ilm 34
                return cl.transformChecked(TransformerFuture.this.input.get().getValue());
35
            }
36
        });
37
        this.input = new AtomicReference<Value<E>>(Value.<E> getNone());
38
    }
39
 
40
    @Override
41
    public final boolean cancel(boolean mayInterruptIfRunning) {
42
        return this.f.cancel(mayInterruptIfRunning);
43
    }
44
 
45
    @Override
46
    public final boolean isCancelled() {
47
        return this.f.isCancelled();
48
    }
49
 
50
    @Override
51
    public final boolean isDone() {
52
        return this.f.isDone();
53
    }
54
 
55
    @Override
56
    public final T get() throws InterruptedException, ExecutionException {
57
        return this.f.get();
58
    }
59
 
60
    @Override
61
    public final T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
62
        return this.f.get(timeout, unit);
63
    }
64
 
65
    @Override
66
    public final T transformChecked(E input) throws X {
67
        if (!this.input.compareAndSet(Value.<E> getNone(), Value.getSome(input)))
68
            throw new IllegalStateException("Already run");
69
        this.f.run();
70
        assert this.f.isDone();
71
        try {
177 ilm 72
            return this.f.getWithOriginalException();
132 ilm 73
        } catch (InterruptedException e) {
74
            // shouldn't happen since f is done
75
            throw new IllegalStateException(e);
76
        }
77
    }
78
}