Generator for Globally unique Strings : 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 
Generator for Globally unique Strings
  

/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.net.InetAddress;
import java.net.ServerSocket;


/**
 * Generator for Globally unique Strings.
 */
public class UuidGenerator {

    private static final String UNIQUE_STUB;
    private static int instanceCount;
    private static String hostName;
    private String seed;
    private long sequence;

    static {
        String stub = "";
        boolean canAccessSystemProps = true;
        try {
            SecurityManager sm = System.getSecurityManager();
            if (sm != null) {
                sm.checkPropertiesAccess();
            }
        catch (SecurityException se) {
            canAccessSystemProps = false;
        }

        if (canAccessSystemProps) {
            try {
                hostName = InetAddress.getLocalHost().getHostName();
                ServerSocket ss = new ServerSocket(0);
                stub = "/" + ss.getLocalPort() "-" + System.currentTimeMillis() "/";
                Thread.sleep(100);
                ss.close();
            catch (Exception ioe) {
                System.out.println("Could not generate unique stub" + ioe);
            }
        else {
            hostName = "localhost";
            stub = "-1-" + System.currentTimeMillis() "-";
        }
        UNIQUE_STUB = generateSanitizedId(stub);
    }

    public UuidGenerator(String prefix) {
        synchronized (UNIQUE_STUB) {
            this.seed = generateSanitizedId(prefix + UNIQUE_STUB + (instanceCount++"-");
        }
    }

    public UuidGenerator() {
        this("ID-" + hostName);
    }

    /**
     * As we have to find the hostname as a side-affect of generating a unique
     * stub, we allow it's easy retrevial here
     
     @return the local host name
     */
    public static String getHostName() {
        return hostName;
    }

    /**
     * Generate a unqiue id
     */
    public synchronized String generateId() {
        return this.seed + (this.sequence++);
    }

    /**
     * Generate a unique ID - that is friendly for a URL or file system
     
     @return a unique id
     */
    public String generateSanitizedId() {
        return generateSanitizedId(generateId());
    }

    /**
     * Ensures that the id is friendly for a URL or file system
     *
     @param id the unique id
     @return the id as file friendly id
     */
    public static String generateSanitizedId(String id) {
        id = id.replace(':''-');
        id = id.replace('_''-');
        id = id.replace('.''-');
        id = id.replace('/''-');
        id = id.replace('\\''-');
        return id;
    }

}

   
    
  
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. ID generator
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.