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

com.github.phantomthief.concurrent.TryWaitResult Maven / Gradle / Ivy

The newest version!
package com.github.phantomthief.concurrent;

import static com.github.phantomthief.util.MoreSuppliers.lazy;
import static com.google.common.base.MoreObjects.toStringHelper;
import static java.util.stream.Collectors.toMap;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.CancellationException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeoutException;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.annotation.Nonnull;

/**
 * @author w.vela
 * Created on 2018-06-25.
 */
class TryWaitResult {

    private final Map, V> success;
    private final Map, Throwable> failed;
    private final Map, TimeoutException> timeout;
    private final Map, CancellationException> cancel;
    private final Map, K> futureMap;

    private final Supplier> successMap;
    private final Supplier> failedMap;
    private final Supplier> timeoutMap;
    private final Supplier> cancelMap;

    TryWaitResult(Map, V> success, Map, Throwable> failed,
            Map, TimeoutException> timeout,
            Map, CancellationException> cancel,
            Map, K> futureMap) {
        this.success = success;
        this.failed = failed;
        this.timeout = timeout;
        this.cancel = cancel;
        this.futureMap = futureMap;
        successMap = lazy(() -> transfer(this.success, this.futureMap));
        failedMap = lazy(() -> transfer(this.failed, this.futureMap));
        timeoutMap = lazy(() -> transfer(this.timeout, this.futureMap));
        cancelMap = lazy(() -> transfer(this.cancel, this.futureMap));
    }

    private  Map transfer(Map, T2> sourceMap,
            Map, K> transferMap) {
        Map map = new HashMap<>();
        // not using collect, for value may be null.
        sourceMap.forEach((k, v) -> map.put(transferMap.get(k), v));
        return map;
    }

    @Nonnull
    public Map getSuccess() {
        return successMap.get();
    }

    @Nonnull
    public Map getFailed() {
        return failedMap.get();
    }

    @Nonnull
    public Map getTimeout() {
        return timeoutMap.get();
    }

    @Nonnull
    public Map getCancel() {
        return cancelMap.get();
    }

    @Nonnull
    public Map cancelAllTimeout(boolean mayInterruptIfRunning) {
        return timeout.entrySet().stream()
                .collect(toMap(entry -> futureMap.get(entry.getKey()),
                        it -> it.getKey().cancel(mayInterruptIfRunning)));
    }

    @Override
    public String toString() {
        return toStringHelper(this)
                .add("success", success.size())
                .add("failed", failed.size())
                .add("timeout", timeout.size())
                .add("cancel", cancel.size())
                .toString();
    }

    public String getCombinedExceptionMessage() {
        return Stream.of(getFailed(), getTimeout(), getCancel())
                .map(map -> map.entrySet().stream())
                .flatMap(Function.identity())
                .map(this::exceptionEntryToString)
                .collect(Collectors.joining("\n"));
    }

    private String exceptionEntryToString(Entry entry) {
        return String.format("key:%s, exception:%s, message:%s", entry.getKey(),
                entry.getValue().getClass(), entry.getValue().getMessage());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy