aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_core/src/main/java/com/yahoo/jdisc/handler/FutureConjunction.java
blob: 4380ad0cbd24afda96104c71431711dabae94510 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.handler;

import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.JdkFutureAdapters;
import com.google.common.util.concurrent.ListenableFuture;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.*;

/**
 * <p>This class implements a Future&lt;Boolean&gt; that is conjunction of zero or more other Future&lt;Boolean&gt;s,
 * i.e. it evaluates to <tt>true</tt> if, and only if, all its operands evaluate to <tt>true</tt>. To use this class,
 * simply create an instance of it and add operands to it using the {@link #addOperand(ListenableFuture)} method.</p>
 * TODO: consider rewriting usage of FutureConjunction to use CompletableFuture instead.
 *
 * @author Simon Thoresen Hult
 */
public final class FutureConjunction implements ListenableFuture<Boolean> {

    private final List<ListenableFuture<Boolean>> operands = new LinkedList<>();

    /**
     * <p>Adds a ListenableFuture&lt;Boolean&gt; to this conjunction. This can be called at any time, even after having called
     * {@link #get()} previously.</p>
     *
     * @param operand The operand to add to this conjunction.
     */
    public void addOperand(ListenableFuture<Boolean> operand) {
        operands.add(operand);
    }

    @Override
    public void addListener(Runnable listener, Executor executor) {
        Futures.allAsList(operands).addListener(listener, executor);
    }

    @Override
    public final boolean cancel(boolean mayInterruptIfRunning) {
        boolean ret = true;
        for (Future<Boolean> op : operands) {
            if (!op.cancel(mayInterruptIfRunning)) {
                ret = false;
            }
        }
        return ret;
    }

    @Override
    public final boolean isCancelled() {
        for (Future<Boolean> op : operands) {
            if (!op.isCancelled()) {
                return false;
            }
        }
        return true;
    }

    @Override
    public final boolean isDone() {
        for (Future<Boolean> op : operands) {
            if (!op.isDone()) {
                return false;
            }
        }
        return true;
    }

    @Override
    public final Boolean get() throws InterruptedException, ExecutionException {
        Boolean ret = Boolean.TRUE;
        for (Future<Boolean> op : operands) {
            if (!op.get()) {
                ret = Boolean.FALSE;
            }
        }
        return ret;
    }

    @Override
    public final Boolean get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException,
                                                                 TimeoutException {
        Boolean ret = Boolean.TRUE;
        long nanos = unit.toNanos(timeout);
        long lastTime = System.nanoTime();
        for (Future<Boolean> op : operands) {
            if (!op.get(nanos, TimeUnit.NANOSECONDS)) {
                ret = Boolean.FALSE;
            }
            long now = System.nanoTime();
            nanos -= now - lastTime;
            lastTime = now;
        }
        return ret;
    }

}