Generates a UUID : UUID « Development « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Development » UUID 
6.54.7.Generates a UUIDPrevious/Next
/**************************************************************************************
 * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved.                 *
 * http://aspectwerkz.codehaus.org                                                    *
 * ---------------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the LGPL license      *
 * a copy of which has been included with this distribution in the license.txt file.  *
 **************************************************************************************/


import java.net.InetAddress;
import java.security.SecureRandom;

/**
 * Generates a UUID. <p/>A Universally Unique Identifier (UUID) is a 128 bit number generated according to an algorithm
 * that is garanteed to be unique in time A space from all other UUIDs.
 *
 @author <a href="mailto:[email protected]">Jonas BonŽr </a>
 */
public class UuidGenerator {
    /**
     * Random seeder.
     */
    private static SecureRandom s_seeder = null;

    /**
     * Mid value, needed for calculation.
     */
    private static String s_midValue = null;

    /**
     * Defines if the generator is initialized or not.
     */
    private static boolean s_initialized = false;

    /**
     * Private constructor to prevent subclassing
     */
    private UuidGenerator() {
    }

    /**
     * Returns a unique uuid.
     *
     @param obj the calling object (this)
     @return a unique uuid
     */
    public static String generate(Object obj) {
        if (!s_initialized) {
            initialize(obj);
        }
        long timeNow = System.currentTimeMillis();

        // get int value as unsigned
        int timeLow = (inttimeNow & 0xFFFFFFFF;
        int node = s_seeder.nextInt();
        return (hexFormat(timeLow, 8+ s_midValue + hexFormat(node, 8));
    }

    /**
     * Initializes the factory.
     *
     @param obj
     */
    private synchronized static void initialize(final Object obj) {
        try {
            InetAddress inet = InetAddress.getLocalHost();
            byte[] bytes = inet.getAddress();
            String hexInetAddress = hexFormat(getInt(bytes)8);
            String thisHashCode = hexFormat(System.identityHashCode(obj)8);
            s_midValue = hexInetAddress + thisHashCode;
            s_seeder = new SecureRandom();
            s_seeder.nextInt();
        catch (java.net.UnknownHostException e) {
            throw new Error("can not initialize the UuidGenerator generator");
        }
        s_initialized = true;
    }

    /**
     * Utility method.
     *
     @param abyte
     @return
     */
    private static int getInt(final byte[] abyte) {
        int i = 0;
        int j = 24;
        for (int k = 0; j >= 0; k++) {
            int l = abyte[k0xff;
            i += (l << j);
            j -= 8;
        }
        return i;
    }

    /**
     * Utility method.
     *
     @param i
     @param j
     @return
     */
    private static String hexFormat(final int i, final int j) {
        String s = Integer.toHexString(i);
        return padHex(s, j+ s;
    }

    /**
     * Utility method.
     *
     @param str
     @param i
     @return
     */
    private static String padHex(final String str, final int i) {
        StringBuffer buf = new StringBuffer();
        if (str.length() < i) {
            for (int j = 0; j < (i - str.length()); j++) {
                buf.append('0');
            }
        }
        return buf.toString();
    }
}
6.54.UUID
6.54.1.Get a unique identifier Using java.rmi.dgc.VMID
6.54.2.Using java.util.UUID
6.54.3.Using java.util.concurrent.AtomicLong: A numerical id, start at zero and increment by one.
6.54.4.Create your own basic UUID
6.54.5.Random GUID
6.54.6.Session ID generator
6.54.7.Generates a UUID
6.54.8.ID generator
6.54.9.Generator for Globally unique Strings
6.54.10.Generates random UUIDs
6.54.11.UUID generator
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.