FileDocCategorySizeDatePackage
UuidUtil.javaAPI DocGlassfish v2 API7027Fri May 04 22:35:32 BST 2007com.sun.enterprise.util.uuid

UuidUtil

public class UuidUtil extends Object
Class UuidUtil

Fields Summary
static final String
_inetAddr
private static SecureRandom
_seeder
Constructors Summary
Methods Summary
private static java.lang.StringaddRandomTo(java.lang.String hexString)

        long hexAsLong = convertToLong(hexString);
        int nextRandom = getNextInt();
        long resultInt = hexAsLong + nextRandom;
        String result = Long.toHexString(resultInt);
        // START PWC 6425338 
        // Always return a length of 7
        int len = result.length();
        if (len < 7) {
            result = padto7(result);
        } else {
            result = result.substring(len - 7, len);
        }
        //  END PWC 6425338 
        return result;
    
private static longconvertToLong(java.lang.String hexString)

        long result = 0;
        try
        {
            result = (Long.valueOf(hexString, 16)).longValue();
        } catch (NumberFormatException ex) {
        }
        return result;
    
private static java.lang.StringformatHexString(int inputInt)

        String result = null;
        String s = Integer.toHexString(inputInt);
        /* PWC 6425338 
        if(s.length() < 8)
        {
            result = s;
        } else {
            result = s.substring(0, 7);
        }
        */
        // START PWC 6425338 
        // Always return a length of 7
        int len = s.length();
        if (len < 7) {
            result = padto7(s);
        } else {
            result = s.substring(len - 7, len);
        }
        //  END PWC 6425338 
        return result;
    
public static java.lang.StringgenerateUuid()

        return generateUuid(new Object());
    
public static java.lang.StringgenerateUuid(java.lang.Object obj)

        
        //low order time bits
        long presentTime = System.currentTimeMillis();
        int presentTimeLow = (int) presentTime & 0xFFFFFFFF;
        String presentTimeStringLow = formatHexString(presentTimeLow);
        
        StringBuffer sb = new StringBuffer(50);
        sb.append(presentTimeStringLow);
        //sb.append(":");
        sb.append(getIdentityHashCode(obj));
        //sb.append(":");
        //sb.append(_inetAddr);
        sb.append(addRandomTo(_inetAddr));
        //sb.append(":");
        sb.append(getNextRandomString());
        return sb.toString();
    
public static java.lang.StringgenerateUuidMM()


    //first method (from MarketMaker Guid)
        
        return new StringBuffer(new UID().toString()).reverse().append(':").append(_inetAddr).toString();
    
private static java.lang.StringgetIdentityHashCode(java.lang.Object obj)
Method getIdentityHashCode

return
audience

        
        String result = null;
        try {
            int hc = System.identityHashCode(obj);
            return formatHexString(hc);
                
        } catch (Exception ex) {
            //must return a value
            //return null;
            return "8AF5182";
        }
    
private static synchronized intgetNextInt()

        return _seeder.nextInt();
    
private static java.lang.StringgetNextRandomString()

        int nextInt = getNextInt();
        return formatHexString(nextInt);
    
private static java.lang.StringinitInetAddr()
Method initInetAddr

return
audience


        try {
            byte[] bytes = InetAddress.getLocalHost().getAddress();
            StringBuffer b = new StringBuffer();
            String s = null;

            for (int i = 0; i < bytes.length; i++) {
                s = Integer.toHexString(bytes[i]);

                if (bytes[i] < 0) {
                    b.append(s.substring(s.length() - 2));
                } else {
                    b.append(s);
                }
            }

            return b.toString();
        } catch (Exception ex) {
            //must return a value
            return "a48eb993";
            //return null;
        }
    
public static voidmain(java.lang.String[] args)
Method main

param
args
audience


              
         
        System.out.println(UuidUtil.generateUuidMM());
        System.out.println(UuidUtil.generateUuid());
        System.out.println(UuidUtil.generateUuid(new Object()));
    
private static java.lang.Stringpadto7(java.lang.String s)


       int i = 0;
       char[] chars = new char[7];
       int len = s.length();
       while (i < len) {
           chars[i] = s.charAt(i);
           i++;
       }
       while (i < 7) {
           chars[i++] = '0";
       }
       return new String(chars);