com.github.phantomthief.failover.impl.PartitionFailoverBuilder Maven / Gradle / Ivy
package com.github.phantomthief.failover.impl;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Predicate;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import com.github.phantomthief.util.ThrowableFunction;
import com.github.phantomthief.util.ThrowablePredicate;
@SuppressWarnings({"checkstyle:VisibilityModifier", "checkstyle:HiddenField"})
public class PartitionFailoverBuilder {
private WeightFailoverBuilder weightFailoverBuilder = new WeightFailoverBuilder<>();
int corePartitionSize;
long maxExternalPoolIdleMillis;
public static PartitionFailoverBuilder newBuilder() {
return new PartitionFailoverBuilder<>();
}
@Nonnull
public PartitionFailover build(Collection original) {
checkNotNull(original);
ensure(original.size());
WeightFailover weightFailover = weightFailoverBuilder.build(original);
return new PartitionFailover<>(this, weightFailover);
}
@Nonnull
public PartitionFailover build(Collection original, int initWeight) {
checkNotNull(original);
ensure(original.size());
WeightFailover weightFailover = weightFailoverBuilder.build(original, initWeight);
return new PartitionFailover<>(this, weightFailover);
}
@Nonnull
public PartitionFailover build(Map original) {
checkNotNull(original);
ensure(original.size());
WeightFailover weightFailover = weightFailoverBuilder.build(original);
return new PartitionFailover<>(this, weightFailover);
}
private void ensure(int allResourceCount) {
checkArgument(corePartitionSize >= 0, "corePartitionSize should not be negative");
checkArgument(corePartitionSize <= allResourceCount, "corePartitionSize should less or equal than size of original");
}
@CheckReturnValue
@Nonnull
public PartitionFailoverBuilder corePartitionSize(int corePartitionSize) {
this.corePartitionSize = corePartitionSize;
return this;
}
@CheckReturnValue
@Nonnull
public PartitionFailoverBuilder reuseRecentResource(long maxExternalPoolIdleMillis) {
this.maxExternalPoolIdleMillis = maxExternalPoolIdleMillis;
return this;
}
//-------------------------methods delegate to weightFailoverBuilder below---------------------
@CheckReturnValue
@Nonnull
public PartitionFailoverBuilder name(String value) {
weightFailoverBuilder.name(value);
return this;
}
@CheckReturnValue
@Nonnull
public PartitionFailoverBuilder autoAddOnMissing(int weight) {
weightFailoverBuilder.autoAddOnMissing(weight);
return this;
}
@CheckReturnValue
@Nonnull
public PartitionFailoverBuilder onMinWeight(Consumer listener) {
weightFailoverBuilder.onMinWeight(listener);
return this;
}
@CheckReturnValue
@Nonnull
public PartitionFailoverBuilder onRecovered(Consumer listener) {
weightFailoverBuilder.onRecovered(listener);
return this;
}
@CheckReturnValue
@Nonnull
public PartitionFailoverBuilder minWeight(int value) {
weightFailoverBuilder.minWeight(value);
return this;
}
@CheckReturnValue
@Nonnull
public PartitionFailoverBuilder failReduceRate(double rate) {
weightFailoverBuilder.failReduceRate(rate);
return this;
}
@CheckReturnValue
@Nonnull
public PartitionFailoverBuilder failReduce(int weight) {
weightFailoverBuilder.failReduce(weight);
return this;
}
@CheckReturnValue
@Nonnull
public PartitionFailoverBuilder successIncreaseRate(double rate) {
weightFailoverBuilder.successIncreaseRate(rate);
return this;
}
@CheckReturnValue
@Nonnull
public PartitionFailoverBuilder successIncrease(int weight) {
weightFailoverBuilder.successIncrease(weight);
return this;
}
@CheckReturnValue
@Nonnull
public PartitionFailoverBuilder checkDuration(long time, TimeUnit unit) {
weightFailoverBuilder.checkDuration(time, unit);
return this;
}
@CheckReturnValue
@Nonnull
public PartitionFailoverBuilder filter(@Nonnull Predicate filter) {
weightFailoverBuilder.filter(filter);
return this;
}
@CheckReturnValue
@Nonnull
public PartitionFailoverBuilder
checker(@Nonnull ThrowableFunction super T, Double, Throwable> failChecker) {
weightFailoverBuilder.checker(failChecker);
return this;
}
@CheckReturnValue
@Nonnull
public PartitionFailoverBuilder checker(
@Nonnull ThrowablePredicate super T, Throwable> failChecker,
@Nonnegative double recoveredInitRate) {
weightFailoverBuilder.checker(failChecker, recoveredInitRate);
return this;
}
}