com.github.phantomthief.pool.KeyAffinity Maven / Gradle / Ivy
package com.github.phantomthief.pool;
import static com.google.common.base.Preconditions.checkNotNull;
import javax.annotation.Nonnull;
import com.github.phantomthief.pool.impl.KeyAffinityBuilder;
import com.github.phantomthief.util.ThrowableConsumer;
import com.github.phantomthief.util.ThrowableFunction;
/**
* @author w.vela
* Created on 2018-02-03.
*/
public interface KeyAffinity extends AutoCloseable, Iterable {
@Nonnull
static KeyAffinityBuilder newBuilder() {
return new KeyAffinityBuilder<>();
}
@Nonnull
V select(K key);
void finishCall(K key);
default T supply(K key, @Nonnull ThrowableFunction func)
throws X {
checkNotNull(func);
V one = select(key);
try {
return func.apply(one);
} finally {
finishCall(key);
}
}
default void run(K key, @Nonnull ThrowableConsumer func) throws X {
supply(key, it -> {
func.accept(it);
return null;
});
}
}