All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.tinygroup.commons.tools.UUID Maven / Gradle / Ivy

There is a newer version: 2.2.3
Show newest version
/**
 *  Copyright (c) 1997-2013, www.tinygroup.org ([email protected]).
 *
 *  Licensed under the GPL, Version 3.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.gnu.org/licenses/gpl.html
 *
 *  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.
 */
package org.tinygroup.commons.tools;

import org.tinygroup.commons.io.ByteArrayOutputStream;

import java.io.DataOutputStream;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.security.SecureRandom;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 生成唯一ID。
 * 

* 唯一ID由以下元素构成:machineId-jvmId-timestamp-counter。 *

*

* 默认情况下,UUID由数字和大写字母构成。如果在构造函数时,指定noCase=false * ,那么所生成的ID将包含小写字母,这样ID的长度会较短。 *

* * @author Michael Zhou */ public class UUID { private boolean noCase; private String instanceId; private AtomicInteger counter; public UUID() { this(true); } public UUID(boolean noCase) { // 1. Machine ID - 根据IP/MAC区分 byte[] machineId = getLocalHostAddress(); // 2. JVM ID - 根据启动时间区分 + 随机数 byte[] jvmId = getRandomizedTime(); this.instanceId = StringUtil.bytesToString(machineId, noCase) + "-" + StringUtil.bytesToString(jvmId, noCase); // counter this.counter = new AtomicInteger(); this.noCase = noCase; } /** 取得local host的地址,如果有可能,取得物理MAC地址。 */ private static byte[] getLocalHostAddress() { Method getHardwareAddress; try { getHardwareAddress = NetworkInterface.class.getMethod("getHardwareAddress"); } catch (Exception e) { getHardwareAddress = null; } byte[] addr; try { InetAddress localHost = InetAddress.getLocalHost(); if (getHardwareAddress != null) { addr = (byte[]) getHardwareAddress.invoke(NetworkInterface.getByInetAddress(localHost)); // maybe null } else { addr = localHost.getAddress(); } } catch (Exception e) { addr = null; } if (addr == null) { addr = new byte[] { 127, 0, 0, 1 }; } return addr; } /** 取得当前时间,加上随机数。 */ private byte[] getRandomizedTime() { long jvmId = System.currentTimeMillis(); long random = new SecureRandom().nextLong(); // 取得上述ID的bytes,并转化成字符串 ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); try { dos.writeLong(jvmId); dos.writeLong(random); } catch (Exception e) { } return baos.toByteArray().toByteArray(); } public String nextID() { // MACHINE_ID + JVM_ID + 当前时间 + counter return instanceId + "-" + StringUtil.longToString(System.currentTimeMillis(), noCase) + "-" + StringUtil.longToString(counter.getAndIncrement(), noCase); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy