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<K, V> extends AutoCloseable, Iterable<V> {

    @Nonnull
    static <V> KeyAffinityBuilder<V> newBuilder() {
        return new KeyAffinityBuilder<>();
    }

    @Nonnull
    V select(K key);

    void finishCall(K key);

    default <T, X extends Throwable> T supply(K key, @Nonnull ThrowableFunction<V, T, X> func)
            throws X {
        checkNotNull(func);
        V one = select(key);
        try {
            return func.apply(one);
        } finally {
            finishCall(key);
        }
    }

    default <X extends Throwable> void run(K key, @Nonnull ThrowableConsumer<V, X> func) throws X {
        supply(key, it -> {
            func.accept(it);
            return null;
        });
    }
    
    boolean inited();
}

@@ -429,16 +429,32 @@
 e%3CV%3E %7B%0A%0A
+    @Deprecated%0A
     @Non
@@ -620,24 +620,40 @@
 ll(K key);%0A%0A
+    @Deprecated%0A
     default 
@@ -927,24 +927,40 @@
    %7D%0A    %7D%0A%0A
+    @Deprecated%0A
     default 

mark deprecated methods.