/*
Copyright (C) 2000-2010  Ministere de la culture et de la communication (France), AJLSM
See LICENCE file
*/
package fr.gouv.culture.sdx.documentbase;

import fr.gouv.culture.sdx.utils.Utilities;

import java.util.Random;

public class DefaultIDGenerator extends AbstractIDGenerator {

    /**Generates an idea appending the given strings
     *
     * @param prefix    The prefix for the generated id
     * @param suffix    The suffix for the generated id
     * */
    public String generate(String prefix, String suffix) {
        String id = null;

        Random r = new Random();

        int i = r.nextInt(100);
        int j = r.nextInt(100);
        int k = r.nextInt(100);

        Random rr = new Random(r.hashCode());
        long m = Math.abs(rr.hashCode());

        Random rrr = new Random(m);

        int[] nums = new int[100];
        for (int l = 0; l < nums.length; l++) {
            int num = rrr.nextInt(65535);
            nums[l] = num;
        }

        int n = nums[i];
        int o = nums[j];
        int p = nums[k];


        id = Integer.toHexString(n) + Integer.toHexString(o) + Integer.toHexString(p);

        if (Utilities.checkString(prefix))
            id = prefix + id;

        if (Utilities.checkString(suffix))
            id = id + suffix;

        if (!isIdUnique(id))
            return generate(prefix, suffix); //a recursive call will loop until a unique id is generated

        return id;

    }

}
