ID generator : UUID GUID « Development Class « Java

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 Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Development Class » UUID GUIDScreenshots 
ID generator
  
/*
    GNU LESSER GENERAL PUBLIC LICENSE
    Copyright (C) 2006 The Lobo Project

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    Contact info: lobochief@users.sourceforge.net
*/
/*
 * Created on Jun 7, 2005
 */

import java.util.*;
import java.util.logging.*;
import java.security.*;
import java.math.*;
import java.net.InetAddress;

/**
 @author J. H. S.
 */
public class ID {
  private static final Random RANDOM1;
  private static final Random RANDOM2;
  private static final Random RANDOM3;
  private static final long globalProcessID;
    private static final Logger logger = Logger.getLogger(ID.class.getName());

    static {
    long time = System.currentTimeMillis();
    long nanoTime = System.nanoTime();
    long freeMemory = Runtime.getRuntime().freeMemory();
    long addressHashCode;
    try {
      InetAddress inetAddress;
      inetAddress = InetAddress.getLocalHost();
      addressHashCode = inetAddress.getHostName().hashCode() ^ inetAddress.getHostAddress().hashCode();
    catch(Exception err) {
      logger.log(Level.WARNING, "Unable to get local host information.", err);
      addressHashCode = ID.class.hashCode();
    }   
    globalProcessID = time ^ nanoTime ^ freeMemory ^ addressHashCode;   
    RANDOM1 = new Random(time);
    RANDOM2 = new Random(nanoTime);
    RANDOM3 = new Random(addressHashCode ^ freeMemory);
  }
  
  private ID() {
  }
  
  public static long generateLong() {
    return Math.abs(RANDOM1.nextLong() ^ RANDOM2.nextLong() ^ RANDOM3.nextLong());
  }
  
  public static int generateInt() {
    return (intgenerateLong();
  }
  
  public static byte[] getMD5Bytes(String content) {
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            return digest.digest(content.getBytes("UTF-8"));
        catch (NoSuchAlgorithmException e) {
          throw new IllegalStateException(e);
    catch(java.io.UnsupportedEncodingException uee) {
      throw new IllegalStateException(uee);
    }
  }

  public static String getHexString(byte[] bytes) {
    // This method cannot change even if it's wrong.
    BigInteger bigInteger = BigInteger.ZERO;
    int shift = 0;
    for(int i = bytes.length; --i >= 0;) {
      BigInteger contrib = BigInteger.valueOf(bytes[i0xFF);
      contrib = contrib.shiftLeft(shift);
      bigInteger = bigInteger.add(contrib);
      shift += 8;
    }
    return bigInteger.toString(16).toUpperCase();
  }
  
  /**
   * Gets a process ID that is nearly guaranteed to be globally unique.
   */
  public static long getGlobalProcessID() {
    return globalProcessID;
  }
  
  public static int random(int min, int max) {
    if(max <= min) {
      return min;
    }
    return Math.abs(RANDOM1.nextInt()) (max - min+ min;
  }
}

   
    
  
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. Simple Id Generator
17. UUID generation
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.