⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uniqueidgenerator.java

📁 老外的在线考试
💻 JAVA
字号:
/* * Core - Library of useful classes that are used in many CyberDemia projects. * Copyright (C) 2003 CyberDemia Research and Services * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 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 * Library General Public License for more details. *  * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA  02111-1307, USA. * * See the COPYING file located in the top-level-directory of * the archive of this library for complete text of license. */package com.cyberdemia.util;import java.util.Random;import java.net.InetAddress;/** * UniqueIdGenerator is a singleton that can generate "Universal Unique IDs". * It is based on the algorithm described in  * <cite>Marinescu, F. : EJB Design Patterns (2002)</cite>. * It uses the following information to generate the IDs: current time, * computer's IP address and this generator's hash code. * Because it relies on the computer's IP address, it should generate  * unique IDs even when used in a distributed manner across different machines. * It does not rely on database access, so it should be quite efficient. The * generated ID is a 16-character String (shorter than the original algorithm * for faster indexing). *  * @author Alexander Yap * @version $Revision: 1.2 $ at $Date: 2004/02/18 08:35:17 $ by $Author: alexycyap $ * */public class UniqueIdGenerator{	/**	 * Gets the single instance of UniqueIdGenerator.	 * 	 * @return UniqueIdGenerator	 */	public static UniqueIdGenerator getInstance()	{		return s_instance;	}		/**	 * Creates a new unique ID.	 * @return A 16-character String unique ID.	 */	public String createId()	{		return createId(null);	}	/**	 * Creates a new unique ID that incorporates a passed in data.	 * @param data  Additional data to construct the unique ID, or null to use a random value.	 * @return A 16-character String unique ID.	 */	public String createId(Object data)	{		String randomHex = null;		if (data!=null)		{			randomHex = getLastChars( Integer.toHexString(data.hashCode()),4);		}		else		{			randomHex = getLastChars(Integer.toHexString(m_random.nextInt()),4);		}		String nowHex = getLastChars(Integer.toHexString((int)System.currentTimeMillis()),8);		return nowHex + randomHex + m_fixedPart;	}	private UniqueIdGenerator()	{		// Work out the fixed part of the ID		int ipHash = 0;		try		{			ipHash = InetAddress.getLocalHost().getHostAddress().hashCode();		}		catch (Exception ex)		{	// If anything goes wrong with IP address, proceed with a random number			ipHash = m_random.nextInt();		}				int identityHash = System.identityHashCode(this);		// Simply combine these two hash codes into one.		int fixedHash = (ipHash & 0xffff) + (identityHash & 0xffff);		m_fixedPart = getLastChars(Integer.toHexString(fixedHash),4);	}		/**	 * Gets the last n characters of str. Prepend with "0"s is str is	 * shorter than n characters. Truncate the front of str if it is too long.	 * @param str Original String	 * @param n Number of characters in the result String.	 * @return n-character String with the last portion of str.	 */	private static String getLastChars(String str, int n)	{		if (str.length()==n)		{			return str;		}		char[] outChars = new char[n];		for (int i=str.length()-1, x=0; x<n; i--,x++)		{			if (i>=0)			{				outChars[x] = str.charAt(i); 			}			else			{				outChars[x] = '0';			}		}		return new String(outChars);	}	private Random m_random = new Random();	private String m_fixedPart = null;		private static UniqueIdGenerator s_instance = new UniqueIdGenerator();}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -