All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yahoo.jdisc.handler.FutureConjunction Maven / Gradle / Ivy

There is a newer version: 8.441.21
Show newest version
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.handler;

import com.yahoo.concurrent.CompletableFutures;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * 

This class implements a Future<Boolean> that is a conjunction of zero or more other Future<Boolean>s, * i.e. it evaluates to true if, and only if, all its operands evaluate to true. To use this class, * simply create an instance of it and add operands to it using the {@link #addOperand(CompletableFuture)} method.

* * @author Simon Thoresen Hult */ final class FutureConjunction implements Future { private final List> operands = new LinkedList<>(); /** *

Adds a {@link CompletableFuture} to this conjunction. This can be called at any time, even after having called * {@link #get()} previously.

* * @param operand The operand to add to this conjunction. */ public void addOperand(CompletableFuture operand) { operands.add(operand); } public void addListener(Runnable listener, Executor executor) { CompletableFutures.allOf(operands) .whenCompleteAsync((__, ___) -> listener.run(), executor); } CompletableFuture completableFuture() { return CompletableFutures.allOf(operands) .thenApply(ops -> ops.stream().allMatch(bool -> bool)); } @Override public boolean cancel(boolean mayInterruptIfRunning) { boolean ret = true; for (Future op : operands) { if (!op.cancel(mayInterruptIfRunning)) { ret = false; } } return ret; } @Override public boolean isCancelled() { for (Future op : operands) { if (!op.isCancelled()) { return false; } } return true; } @Override public boolean isDone() { for (Future op : operands) { if (!op.isDone()) { return false; } } return true; } @Override public Boolean get() throws InterruptedException, ExecutionException { boolean ret = Boolean.TRUE; for (Future op : operands) { if (!op.get()) { ret = Boolean.FALSE; } } return ret; } @Override public 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 op : operands) { if (!op.get(nanos, TimeUnit.NANOSECONDS)) { ret = Boolean.FALSE; } long now = System.nanoTime(); nanos -= now - lastTime; lastTime = now; } return ret; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy