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

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

There is a newer version: 0.1.55
Show 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.concurrent.CancellationException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeoutException;
import java.util.function.Supplier;

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();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy