com.github.phantomthief.failover.Failover Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of simple-failover Show documentation
Show all versions of simple-failover Show documentation
A simple failover library for Java
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);
}
}