package com.bocloud.ims.common.utils;

/**
 * Map工具类
 *
 * @author sxp
 * @since 2022/11/24
 */
public class MapUtils {
    /**
     * The largest power of two that can be represented as an {@code int}.
     */
    public static final int MAX_POWER_OF_TWO = 1 << (Integer.SIZE - 2);

    /**
     * 根据期望大小获取Map的容量大小
     * <p>
     * Returns a capacity that is sufficient to keep the map from being resized as
     * long as it grows no larger than expectedSize and the load factor is >= its
     * default (0.75).
     * </p>
     *
     * @author Shawpin Shi
     * @since 2018/3/19
     */
    public static int capacity(int expectedSize) {
        if (expectedSize < 3) {
            if (expectedSize < 0) {
                throw new IllegalArgumentException("expectedSize cannot be negative but was: " + expectedSize);
            }
            return expectedSize + 1;
        }

        if (expectedSize < MAX_POWER_OF_TWO) {
            // This is the calculation used in JDK8 to resize when a putAll
            // happens; it seems to be the most conservative calculation we
            // can make.  0.75 is the default load factor.
            return (int) ((float) expectedSize / 0.75F + 1.0F);
        }

        return Integer.MAX_VALUE;
    }

}
