<commit_before>package com.hida.model;

import static com.hida.model.IdGenerator.Rng;
import static com.hida.model.IdGenerator.Logger;
import java.util.Set;
import java.util.TreeSet;

/**
 * An Id Generator that creates Pids primarily based on tokenType and
 * rootLength.
 *
 * @author lruffin
 */
public class AutoIdGenerator extends IdGenerator {

    /**
     * Designates what characters are contained in the id's root. There are 7
     * types of token maps, each describing a range of possible characters in
     * the id. This range is further affected by the variable SansVowel.
     *
     * <pre>
     * DIGIT: Digit values only.
     * LOWER_ALPHABET: Lowercase letters only.
     * UPPER_ALPHABET: Uppercase letters only.
     * MIXED_ALPHABET: Lowercase and Uppercase letters only.
     * LOWER_ALPHABET_EXTENDED: Digit values and Lowercase letters only.
     * UPPER_ALPHABET_EXTENDED: Digits and Uppercase letters only
     * MIXED_ALPHABET_EXTENDED: All characters specified by previous tokens
     * </pre>
     *
     */
    private TokenType TokenType;

    /**
     * Designates the length of the id's root.
     */
    private int RootLength;

    /**
     * Default constructor. Aside from TokenType, there are no restrictions
     * placed on the parameters and can be used however one sees fit.
     *
     * @param prefix A sequence of characters that appear in the beginning of
     * PIDs
     * @param sansVowel Dictates whether or not vowels are allowed
     * @param tokenType An enum used to configure PIDS
     * @param rootLength Designates the length of the id's root
     */
    public AutoIdGenerator(String prefix, boolean sansVowel, TokenType tokenType, int rootLength) {
        super(prefix, sansVowel);
        this.TokenType = tokenType;
        this.RootLength = rootLength;
    }

    /**
     * Creates Pids without regard to a natural order.
     *
     * @param amount The number of PIDs to be created
     * @return A set of Pids
     */
    @Override
    public Set<Pid> randomMint(long amount) {
        // checks to see if its possible to produce or add requested amount of
        long total = calculatePermutations();
        if (total < amount) {
            throw new NotEnoughPermutationsException(total, amount);
        }

        // generate ids
        String map = TokenType.getCharacters();
        Set<Pid> tempIdList = new TreeSet<>();

        for (int i = 0; i < amount; i++) {
            int[] tempIdBaseMap = new int[RootLength];
            for (int j = 0; j < RootLength; j++) {
                tempIdBaseMap[j] = Rng.nextInt(map.length());
            }
            Pid currentId = new AutoId(Prefix, tempIdBaseMap, map);
            Logger.trace("Generated Auto Random ID: " + currentId);

            while (tempIdList.contains(currentId)) {
                currentId.incrementId();
            }
            tempIdList.add(currentId);
        }
        return tempIdList;
    }

    /**
     * Creates Pids in ascending order
     *
     * @param amount The number of PIDs to be created
     * @return A set of Pids
     */
    @Override
    public Set<Pid> sequentialMint(long amount) {
        // checks to see if its possible to produce or add requested amount of
        long total = calculatePermutations();
        if (total < amount) {
            throw new NotEnoughPermutationsException(total, amount);
        }

        // generate ids
        String map = TokenType.getCharacters();
        Set<Pid> idSet = new TreeSet<>();

        int[] previousIdBaseMap = new int[RootLength];
        AutoId currentId = new AutoId(Prefix, previousIdBaseMap, map);
        for (int i = 0; i < amount; i++) {
            AutoId nextId = new AutoId(currentId);
            idSet.add(currentId);
            Logger.trace("Generated Auto Sequential ID: " + currentId);
            nextId.incrementId();
            currentId = new AutoId(nextId);
        }

        return idSet;
    }

    /**
     * This method calculates and returns the total possible number of
     * permutations using the values given in the constructor.
     *
     * @return number of permutations
     */
    @Override
    public long calculatePermutations() {
        // get the base of each character
        int base = 0;
        switch (TokenType) {
            case DIGIT:
                base = 10;
                break;
            case LOWER_ALPHABET:
            case UPPER_ALPHABET:
                base = (SansVowel) ? 20 : 26;
                break;
            case MIXED_ALPHABET:
                base = (SansVowel) ? 40 : 52;
                break;
            case LOWER_ALPHABET_EXTENDED:
            case UPPER_ALPHABET_EXTENDED:
                base = (SansVowel) ? 30 : 36;
                break;
            case MIXED_ALPHABET_EXTENDED:
                base = (SansVowel) ? 50 : 62;
                break;
        }

        // raise it to the power of how ever long the rootLength is
        return ((long) Math.pow(base, RootLength));
    }

    /**
     * Increments a value of a PID. If the maximum limit is reached the values
     * will wrap around.<commit_msg>Correct AutoIdGenerator's calculatePermutations<commit_after>package com.hida.model;

import static com.hida.model.IdGenerator.Rng;
import static com.hida.model.IdGenerator.Logger;
import java.util.Set;
import java.util.TreeSet;

/**
 * An Id Generator that creates Pids primarily based on tokenType and
 * rootLength.
 *
 * @author lruffin
 */
public class AutoIdGenerator extends IdGenerator {

    /**
     * Designates what characters are contained in the id's root. There are 7
     * types of token maps, each describing a range of possible characters in
     * the id. This range is further affected by the variable SansVowel.
     *
     * <pre>
     * DIGIT: Digit values only.
     * LOWER_ALPHABET: Lowercase letters only.
     * UPPER_ALPHABET: Uppercase letters only.
     * MIXED_ALPHABET: Lowercase and Uppercase letters only.
     * LOWER_ALPHABET_EXTENDED: Digit values and Lowercase letters only.
     * UPPER_ALPHABET_EXTENDED: Digits and Uppercase letters only
     * MIXED_ALPHABET_EXTENDED: All characters specified by previous tokens
     * </pre>
     *
     */
    private TokenType TokenType;

    /**
     * Designates the length of the id's root.
     */
    private int RootLength;

    /**
     * Default constructor. Aside from TokenType, there are no restrictions
     * placed on the parameters and can be used however one sees fit.
     *
     * @param prefix A sequence of characters that appear in the beginning of
     * PIDs
     * @param sansVowel Dictates whether or not vowels are allowed
     * @param tokenType An enum used to configure PIDS
     * @param rootLength Designates the length of the id's root
     */
    public AutoIdGenerator(String prefix, boolean sansVowel, TokenType tokenType, int rootLength) {
        super(prefix, sansVowel);
        this.TokenType = tokenType;
        this.RootLength = rootLength;
    }

    /**
     * Creates Pids without regard to a natural order.
     *
     * @param amount The number of PIDs to be created
     * @return A set of Pids
     */
    @Override
    public Set<Pid> randomMint(long amount) {
        // checks to see if its possible to produce or add requested amount of
        long total = calculatePermutations();
        if (total < amount) {
            throw new NotEnoughPermutationsException(total, amount);
        }

        // generate ids
        String map = TokenType.getCharacters();
        Set<Pid> tempIdList = new TreeSet<>();

        for (int i = 0; i < amount; i++) {
            int[] tempIdBaseMap = new int[RootLength];
            for (int j = 0; j < RootLength; j++) {
                tempIdBaseMap[j] = Rng.nextInt(map.length());
            }
            Pid currentId = new AutoId(Prefix, tempIdBaseMap, map);
            Logger.trace("Generated Auto Random ID: " + currentId);

            while (tempIdList.contains(currentId)) {
                currentId.incrementId();
            }
            tempIdList.add(currentId);
        }
        return tempIdList;
    }

    /**
     * Creates Pids in ascending order
     *
     * @param amount The number of PIDs to be created
     * @return A set of Pids
     */
    @Override
    public Set<Pid> sequentialMint(long amount) {
        // checks to see if its possible to produce or add requested amount of
        long total = calculatePermutations();
        if (total < amount) {
            throw new NotEnoughPermutationsException(total, amount);
        }

        // generate ids
        String map = TokenType.getCharacters();
        Set<Pid> idSet = new TreeSet<>();

        int[] previousIdBaseMap = new int[RootLength];
        AutoId currentId = new AutoId(Prefix, previousIdBaseMap, map);
        for (int i = 0; i < amount; i++) {
            AutoId nextId = new AutoId(currentId);
            idSet.add(currentId);
            Logger.trace("Generated Auto Sequential ID: " + currentId);
            nextId.incrementId();
            currentId = new AutoId(nextId);
        }

        return idSet;
    }

    /**
     * This method calculates and returns the total possible number of
     * permutations using the values given in the constructor.
     *
     * @return number of permutations
     */
    @Override
    public long calculatePermutations() {
        // get the base of each character
        int base = TokenType.getCharacters().length();

        // raise it to the power of how ever long the rootLength is
        return ((long) Math.pow(base, RootLength));
    }

    /**
     * Increments a value of a PID. If the maximum limit is reached the values
     * will wrap around.
{'fraction_non_alphanumeric': 0.08130331997122006, 'fraction_numerical': 0.0027752081406105457, 'mean_word_length': 2.1417500807232805, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4820178', 'n_tokens_mistral': 2782, 'n_tokens_neox': 2683, 'n_words': 934}