// Path: core/src/main/java/com/github/phantomthief/pool/KeyAffinity.java
// @Deprecated
// public interface KeyAffinity<K, V> extends AutoCloseable, Iterable<V> {
// 
//     boolean inited();
// }
// 
// Path: core/src/main/java/com/github/phantomthief/util/ThrowableConsumer.java
// @FunctionalInterface
// public interface ThrowableConsumer<T, X extends Throwable> {
// 
//     void accept(T t) throws X;
// 
//     default ThrowableConsumer<T, X> andThen(ThrowableConsumer<? super T, X> after) {
//         requireNonNull(after);
//         return t -> {
//             accept(t);
//             after.accept(t);
//         };
//     }
// }


import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterators.transform;
import static com.google.common.util.concurrent.Uninterruptibles.sleepUninterruptibly;
import static java.util.Comparator.comparingInt;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import static java.util.stream.Collectors.toCollection;
import static java.util.stream.Collectors.toList;
import static java.util.stream.IntStream.range;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BooleanSupplier;
import java.util.function.IntPredicate;
import java.util.function.IntSupplier;
import java.util.function.Supplier;
import javax.annotation.Nonnull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.phantomthief.pool.KeyAffinity;
import com.github.phantomthief.util.ThrowableConsumer;
import com.google.common.annotations.VisibleForTesting;
package com.github.phantomthief.pool.impl;






/**
 * @author w.vela
 * Created on 2018-02-08.
 */
class KeyAffinityImpl<K, V> implements KeyAffinity<K, V> {

    private static final Logger logger = LoggerFactory.getLogger(KeyAffinityImpl.class);
    private static long sleepBeforeClose = SECONDS.toMillis(5);

    private final IntSupplier count;
    private final List<ValueRef> all;
// Path: core/src/main/java/com/github/phantomthief/pool/KeyAffinity.java
// @Deprecated
// public interface KeyAffinity<K, V> extends AutoCloseable, Iterable<V> {
// 
//     boolean inited();
// }
// 
// Path: core/src/main/java/com/github/phantomthief/util/ThrowableConsumer.java
// @FunctionalInterface
// public interface ThrowableConsumer<T, X extends Throwable> {
// 
//     void accept(T t) throws X;
// 
//     default ThrowableConsumer<T, X> andThen(ThrowableConsumer<? super T, X> after) {
//         requireNonNull(after);
//         return t -> {
//             accept(t);
//             after.accept(t);
//         };
//     }
// }


// Path: core/src/main/java/com/github/phantomthief/pool/impl/KeyAffinityImpl.java
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Iterators.transform;
import static com.google.common.util.concurrent.Uninterruptibles.sleepUninterruptibly;
import static java.util.Comparator.comparingInt;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import static java.util.stream.Collectors.toCollection;
import static java.util.stream.Collectors.toList;
import static java.util.stream.IntStream.range;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BooleanSupplier;
import java.util.function.IntPredicate;
import java.util.function.IntSupplier;
import java.util.function.Supplier;
import javax.annotation.Nonnull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.phantomthief.pool.KeyAffinity;
import com.github.phantomthief.util.ThrowableConsumer;
import com.google.common.annotations.VisibleForTesting;

package com.github.phantomthief.pool.impl;






/**
 * @author w.vela
 * Created on 2018-02-08.
 */
class KeyAffinityImpl<K, V> implements KeyAffinity<K, V> {

    private static final Logger logger = LoggerFactory.getLogger(KeyAffinityImpl.class);
    private static long sleepBeforeClose = SECONDS.toMillis(5);

    private final IntSupplier count;
    private final List<ValueRef> all;
    private final ThrowableConsumer<V, Exception> deposeFunc;