Java UUID Create generateUUID(Object o)

Here you can find the source of generateUUID(Object o)

Description

generate UUID

License

Open Source License

Declaration

static public String generateUUID(Object o) 

Method Source Code


//package com.java2s;
/*/*from   w  ww.  jav a2  s . c  om*/
 * Copyright (C) 2003-2011 eXo Platform SAS.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    private static String hexServerIP_ = null;
    private static final SecureRandom seeder_ = new SecureRandom();

    static public String generateUUID(Object o) {
        StringBuffer tmpBuffer = new StringBuffer(16);
        if (hexServerIP_ == null) {
            InetAddress localInetAddress = null;
            try {
                // get the inet address
                localInetAddress = InetAddress.getLocalHost();
            } catch (java.net.UnknownHostException uhe) {
                // System .err .println(
                // "ContentSetUtil: Could not get the local IP address using InetAddress.getLocalHost()!"
                // );
                // todo: find better way to get around this...
                uhe.printStackTrace();
                return null;
            }
            byte serverIP[] = localInetAddress.getAddress();
            hexServerIP_ = hexFormat(getInt(serverIP), 8);
        }
        String hashcode = hexFormat(System.identityHashCode(o), 8);
        tmpBuffer.append(hexServerIP_);
        tmpBuffer.append(hashcode);

        long timeNow = System.currentTimeMillis();
        int timeLow = (int) timeNow & 0xFFFFFFFF;
        int node = seeder_.nextInt();

        StringBuffer guid = new StringBuffer(32);
        guid.append(hexFormat(timeLow, 8));
        guid.append(tmpBuffer.toString());
        guid.append(hexFormat(node, 8));
        return guid.toString();
    }

    private static String hexFormat(int i, int j) {
        String s = Integer.toHexString(i);
        return padHex(s, j) + s;
    }

    private static int getInt(byte bytes[]) {
        int i = 0;
        int j = 24;
        for (int k = 0; j >= 0; k++) {
            int l = bytes[k] & 0xff;
            i += l << j;
            j -= 8;
        }
        return i;
    }

    private static String padHex(String s, int i) {
        StringBuffer tmpBuffer = new StringBuffer();
        if (s.length() < i) {
            for (int j = 0; j < i - s.length(); j++) {
                tmpBuffer.append('0');
            }
        }
        return tmpBuffer.toString();
    }
}

Related

  1. generateUUID()
  2. generateUUID()
  3. generateUUID()
  4. generateUuid(boolean dash)
  5. generateUUID(int numchars)
  6. generateUUID(String prefix)
  7. generateUuid(String seed)
  8. generateUUIDFileName(String fileName)
  9. generateUUIDFrom(String uuidString)