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

com.github.phantomthief.failover.Failover Maven / Gradle / Ivy

There is a newer version: 0.1.32
Show newest version
package com.github.phantomthief.failover;

import static com.github.phantomthief.failover.util.RandomListUtils.getRandom;
import static java.util.stream.Collectors.toList;

import java.util.Collection;
import java.util.List;
import java.util.Set;

import javax.annotation.Nullable;

import com.github.phantomthief.failover.util.FailoverUtils;
import com.github.phantomthief.util.ThrowableConsumer;
import com.github.phantomthief.util.ThrowableFunction;

/**
 * @author w.vela
 */
public interface Failover extends SimpleFailover {

    List getAll();

    /**
     * better use {@code #getAvailable(int)} or {@code #getOneAvailable()}
     */
    List getAvailable();

    default List getAvailableExclude(Collection exclusions) {
        return getAvailable().stream().filter(e -> !exclusions.contains(e)).collect(toList());
    }

    Set getFailed();

    @Nullable
    @Override
    default T getOneAvailable() {
        return getRandom(getAvailable());
    }

    @Nullable
    @Override
    default T getOneAvailableExclude(Collection exclusions) {
        return getRandom(getAvailableExclude(exclusions));
    }

    default List getAvailable(int n) {
        return getRandom(getAvailable(), n);
    }

    /**
     * @see FailoverUtils#supplyWithRetry
     */
    default  E supplyWithRetry(ThrowableFunction func) throws X {
        return FailoverUtils.supplyWithRetry(getAll().size(), 0, this, func);
    }

    /**
     * @see FailoverUtils#runWithRetry
     */
    default  void runWithRetry(ThrowableConsumer func) throws X {
        FailoverUtils.runWithRetry(getAll().size(), 0, this, func);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy