// Path: src/main/java/com/github/phantomthief/failover/util/RandomListUtils.java
// @Nullable
// public static <T> T getRandom(List<T> source) {
//     if (source == null || source.isEmpty()) {
//         return null;
//     }
//     return source.get(ThreadLocalRandom.current().nextInt(source.size()));
// }


import static com.github.phantomthief.failover.util.RandomListUtils.getRandom;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.RandomAccess;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.phantomthief.util.ThrowableConsumer;
import com.github.phantomthief.util.ThrowableFunction;
    }

    public static <T> ConcurrencyAware<T> create() {
        return new ConcurrencyAware<>();
    }

    @Nullable
    private T selectIdlest(@Nonnull Iterable<T> candidates) {
        checkNotNull(candidates);
        if (candidates instanceof List && candidates instanceof RandomAccess) {
            List<T> candidatesCol = (List<T>) candidates;
            T t = selectIdlestFast(candidatesCol);
            if (t != null) {
                return t;
            }
        }

        // find objects with minimum concurrency
        List<T> idlest = new ArrayList<>();
        int minValue = Integer.MAX_VALUE;
        for (T obj : candidates) {
            int c = concurrency.getOrDefault(obj, 0);
            if (c < minValue) {
                minValue = c;
                idlest.clear();
                idlest.add(obj);
            } else if (c == minValue) {
                idlest.add(obj);
            }
        }
// Path: src/main/java/com/github/phantomthief/failover/util/RandomListUtils.java
// @Nullable
// public static <T> T getRandom(List<T> source) {
//     if (source == null || source.isEmpty()) {
//         return null;
//     }
//     return source.get(ThreadLocalRandom.current().nextInt(source.size()));
// }


// Path: src/main/java/com/github/phantomthief/failover/util/ConcurrencyAware.java
import static com.github.phantomthief.failover.util.RandomListUtils.getRandom;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.RandomAccess;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.phantomthief.util.ThrowableConsumer;
import com.github.phantomthief.util.ThrowableFunction;

    }

    public static <T> ConcurrencyAware<T> create() {
        return new ConcurrencyAware<>();
    }

    @Nullable
    private T selectIdlest(@Nonnull Iterable<T> candidates) {
        checkNotNull(candidates);
        if (candidates instanceof List && candidates instanceof RandomAccess) {
            List<T> candidatesCol = (List<T>) candidates;
            T t = selectIdlestFast(candidatesCol);
            if (t != null) {
                return t;
            }
        }

        // find objects with minimum concurrency
        List<T> idlest = new ArrayList<>();
        int minValue = Integer.MAX_VALUE;
        for (T obj : candidates) {
            int c = concurrency.getOrDefault(obj, 0);
            if (c < minValue) {
                minValue = c;
                idlest.clear();
                idlest.add(obj);
            } else if (c == minValue) {
                idlest.add(obj);
            }
        }
        T result = getRandom(idlest);