package pr2.vererbung.password.rules;

/**
 * Helper class.
 */
final class Helper {

    private Helper() {
        // no instances
    }

    /**
     * Calculates the Shannon Entropy over a character sequence.
     *
     * @param word the characters.
     * @return the Shannon Entropy.
     */
    public static double calcShannonEntropy(String word) {
        int[] count = new int[Character.MAX_VALUE];

        for (char c : word.toCharArray()) {
            count[c]++;
        }

        int sum = 0;

        for (int e : count) {
            if (e != 0) {
                sum += e;
            }
        }

        double entropy = 0.0;

        for (int e : count) {
            if (e != 0) {
                double p = (double) e / (double) sum;
                entropy += (Math.log(p) / Math.log(2)) * p;
            }
        }

        return -entropy;
    }
}
