UUID generation : UUID GUID « Development Class « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Development Class » UUID GUIDScreenshots 
UUID generation
     
//Revised from act.soap.util;

import java.util.*;
import java.security.SecureRandom;

public class UUID {
    private int    d1;
    private short  d2;
    private short  d3;
    private byte[] d4;
    public static final UUID nil = new UUID(0(short0(short0new byte[] {  0000000} );
    static Random random = new Random();
    public static UUID generate() {
        int d1 = random.nextInt() 65536;
        d1 += random.nextInt() 65535 65535;
        int d2 = random.nextInt();
        int d3 = random.nextInt();
        byte[] d4 = new byte[8];
        for (int i = 0; i < 4; i++) {
            int t = random.nextInt();
            d4[i * 2(byte) (t % 256);
            d4[i * 1(byte) (t / 256 256);
        }
        return new UUID(d1, (short) (d2 % 65536)(short) (d3 % 65536), d4);
    }
    public UUID() {
        d1 = 0;
        d2 = 0;
        d3 = 0;
        d4 = new byte[] { 0000000};
    }
    public UUID(int d1, short d2, short d3, byte[] d4) {
        this.d1 = d1;
        this.d2 = d2;
        this.d3 = d3;
        this.d4 = new byte[8];
        for (int i = 0; i < 8; i++)
            this.d4[i= d4[i];
    }
    public String toString() {
        String temp1, temp2, temp3, temp4, temp;
        temp1 = Integer.toHexString(d1);
        while (temp1.length() 8)
            temp1 = new String("0"+ temp1;
        if (d2 < 0)
            temp2 = Integer.toHexString(d2 + 65536);
        else
            temp2 = Integer.toHexString(d2);
        while (temp2.length() 4)
            temp2 = new String("0"+ temp2;
        if (d3 < 0)
            temp3 = Integer.toHexString(d3 + 65536);
        else
            temp3 = Integer.toHexString(d3);
        while (temp3.length() 4)
            temp3 = new String("0"+ temp3;
        temp = temp1 + "-" + temp2 + "-" + temp3 + "-";
        for (int i = 0; i < d4.length; i++) {
            if (d4[i0)
                temp4 = Integer.toHexString(d4[i256);
            else
                temp4 = Integer.toHexString(d4[i]);
            while (temp4.length() 2)
                temp4 = new String("0"+ temp4;
            temp += temp4;
        }
        return temp;
    }
        public static void mainString[] args ) {
        forint i = ; i< 100 ; i++ ) {
            System.out.printlnUUID.generate().toString() );
        }
    }
}

   
    
    
    
    
  
Related examples in the same category
1.Random GUID
2.Algorithm for generating Random GUID
3.Get a unique identifier Using java.rmi.dgc.VMID
4.Using java.util.UUID
5.Create your own basic UUID
6.Session ID generator
7.UUID generator from Sun Microsystems
8.UUID generator from http://www1.ics.uci.edu
9.Using java.util.concurrent.AtomicLong: A numerical id, start at zero and increment by one.
10.A 32 byte GUID generator
11.A unique identifier
12.Generate pseudo-GUID sequences
13.Generates a UUID
14.Generates random UUIDs
15.Generator for Globally unique Strings
16.ID generator
17.Simple Id Generator
18.UUID generator of 32 bytes long values
19.Simple Long Id Generator
20.Long Sequence Generator
21.UUID generator
22.Thread-safe version of the Axiom UUIDGenerator
23.Convert an array of bytes into a List of Strings using UTF-8.
24.Your own UUID
25.Your own UUID 2
26.Create GUIDs according to the principles at http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt.
27.Get Unique Id
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.