Thursday, March 31, 2011

Generating Unique Number in Java


import java.util.Random;

public class UniqueIDGenerator {

    private static final long serialVersionUID = 1L;

    /**
     *
     * Random-number generator.
     */

    private static transient Random r = new Random();

    // use constanst to prevent warnings about magic numbers:

    private static final long HEX_3FFFFFFFFFFFFFFF = 0x3FFFFFFFFFFFFFFFL;

    private static final long HEX_8000000000000000 = 0x8000000000000000L;

    private static final long HEX_FFFFFFFFFFFF0FFF = 0xFFFFFFFFFFFF0FFFL;

    private static final long HEX_4000 = 0x4000;

    private static final long HEX_FFFFFFFF00000000 = 0xFFFFFFFF00000000L;

    private static final long HEX_FFFFFFFF = 0xFFFFFFFFL;

    private static final long HEX_FFFF0000 = 0xFFFF0000L;

    private static final long HEX_0000000000000000FFFF = 0x0000000000000000FFFFL;

    private static final long HEX_FFFF000000000000 = 0xFFFF000000000000L;

    private static final long HEX_FFFF = 0xFFFF;

    private static final long HEX_FFFFFFFFFFFF = 0xFFFFFFFFFFFFL;

    private static final int DEC_4 = 4;

    private static final int DEC_8 = 8;

    private static final int DEC_12 = 12;

    private static final int DEC_16 = 16;

    private static final int DEC_32 = 32;

    private static final int DEC_48 = 48;

    /**
     *
     * @return a string with a universally unique id of format:
     *
     *         xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx .
     */

    public static String generateUUID() {

        // calculate lsb and msb

        long leastSigBits = r.nextLong();

        long mostSigBits = r.nextLong();

        leastSigBits &= HEX_3FFFFFFFFFFFFFFF;

        leastSigBits |= HEX_8000000000000000; // set top two bits to variant 2

        mostSigBits &= HEX_FFFFFFFFFFFF0FFF;

        mostSigBits |= HEX_4000; // Version 4;

        // turn leastSigBits and mostSigBits into string of format:

        // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

        String id =

        padHex(((mostSigBits & HEX_FFFFFFFF00000000) >> DEC_32) & HEX_FFFFFFFF,
                DEC_8)

                + "-"

                + padHex(((mostSigBits & HEX_FFFF0000) >> DEC_16), DEC_4)

                + "-"

                + padHex((mostSigBits & HEX_0000000000000000FFFF), DEC_4)

                + "-"

                + padHex(
                        (((leastSigBits & HEX_FFFF000000000000) >> DEC_48) & HEX_FFFF),
                        DEC_4)

                + "-"

                + padHex(leastSigBits & HEX_FFFFFFFFFFFF, DEC_12);

        return id;

    }

    /**
     *
     * Returns a hex String from l, padded to n spaces.
     *
     * @param l
     *            .
     *
     * @param n
     *            .
     *
     * @return String
     */

    private static String padHex(long l, int n) {

        String s = Long.toHexString(l);

        while (s.length() < n) {

            s = "0" + s;

        }

        return s;

    }
    public static void main(String[] args) {
        System.out.println(generateUUID());
    }

}




No comments:

Post a Comment

Java 1.7 New Features Over 1.6

Automatic Resource Management Description: A proposal to support scoping of resource usage in a block with automatic resource cleanup. T...