[Mulgara-svn] r2058 - in trunk/src/jar: query/java/org/jrdf/graph/mem query/java/org/jrdf/util util/java/org/mulgara/util
pag at mulgara.org
pag at mulgara.org
Wed Sep 28 23:44:31 UTC 2011
Author: pag
Date: 2011-09-28 23:44:31 +0000 (Wed, 28 Sep 2011)
New Revision: 2058
Removed:
trunk/src/jar/query/java/org/jrdf/util/UIDGenerator.java
trunk/src/jar/query/java/org/jrdf/util/UIDGeneratorUnitTest.java
Modified:
trunk/src/jar/query/java/org/jrdf/graph/mem/GraphElementFactoryImpl.java
trunk/src/jar/util/java/org/mulgara/util/UIDGenerator.java
Log:
Consolidated UIDGenerators
Modified: trunk/src/jar/query/java/org/jrdf/graph/mem/GraphElementFactoryImpl.java
===================================================================
--- trunk/src/jar/query/java/org/jrdf/graph/mem/GraphElementFactoryImpl.java 2011-09-28 19:16:18 UTC (rev 2057)
+++ trunk/src/jar/query/java/org/jrdf/graph/mem/GraphElementFactoryImpl.java 2011-09-28 23:44:31 UTC (rev 2058)
@@ -59,7 +59,7 @@
package org.jrdf.graph.mem;
import org.jrdf.graph.*;
-import org.jrdf.util.UIDGenerator;
+import org.mulgara.util.UIDGenerator;
import java.net.URI;
import java.util.HashMap;
Deleted: trunk/src/jar/query/java/org/jrdf/util/UIDGenerator.java
===================================================================
--- trunk/src/jar/query/java/org/jrdf/util/UIDGenerator.java 2011-09-28 19:16:18 UTC (rev 2057)
+++ trunk/src/jar/query/java/org/jrdf/util/UIDGenerator.java 2011-09-28 23:44:31 UTC (rev 2058)
@@ -1,303 +0,0 @@
-/*
- * $Header$
- * $Revision: 624 $
- * $Date: 2006-06-24 21:02:12 +1000 (Sat, 24 Jun 2006) $
- *
- * ====================================================================
- *
- * The Apache Software License, Version 1.1
- *
- * Copyright (c) 2003, 2004 The JRDF Project. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- *
- * 3. The end-user documentation included with the redistribution, if
- * any, must include the following acknowlegement:
- * "This product includes software developed by the
- * the JRDF Project (http://jrdf.sf.net/)."
- * Alternately, this acknowlegement may appear in the software itself,
- * if and wherever such third-party acknowlegements normally appear.
- *
- * 4. The names "The JRDF Project" and "JRDF" must not be used to endorse
- * or promote products derived from this software without prior written
- * permission. For written permission, please contact
- * newmana at users.sourceforge.net.
- *
- * 5. Products derived from this software may not be called "JRDF"
- * nor may "JRDF" appear in their names without prior written
- * permission of the JRDF Project.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
- * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
- * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- * ====================================================================
- *
- * This software consists of voluntary contributions made by many
- * individuals on behalf of the JRDF Project. For more
- * information on JRDF, please see <http://jrdf.sourceforge.net/>.
- */
-
-package org.jrdf.util;
-
-//Java 2 standard packages
-import java.net.InetAddress;
-import java.net.UnknownHostException;
-import java.nio.ByteBuffer;
-import java.rmi.dgc.VMID;
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
-import java.security.SecureRandom;
-
-/**
- * Utility class that generates an Unique identifier.
- *
- * @author <a href="mailto:robert.turner at tucanatech.com">Robert Turner</a>
- */
-public class UIDGenerator {
-
- /** Computers IP address. */
- private static String ipAddress;
-
- /** Unique Java Virtual Machine Identifier. */
- private static String vmID;
-
- /** time the method was called (used to prevent duplicates). */
- private static long callTime;
-
- /** count of UIDs generated by this JVM (to avoid per millisecond duplicates). */
- private static long uidCounter;
- private static final int SINGLE_DIGIT = 0x10;
- private static final int INT_OFFSET = 0xFF;
-
- private UIDGenerator() {
- }
-
- /**
- * Generates an Unique Identifier using the current time and the machines
- * IP address.
- *
- * @throws Exception
- * @return String
- */
- public static synchronized String generateUID() throws Exception {
- String uniqueID = getUniqueID(getSeed());
- return uniqueID;
- }
-
- /**
- * Returns a MD5 sum of the seed.
- *
- * @param seed char[]
- * @throws Exception
- * @return String
- */
- private static synchronized String getUniqueID(char[] seed) throws
- Exception {
-
- String uid = null;
-
- //digest the seed and convert to hex
- byte[] digested = digest(seed);
- StringBuffer buffer = new StringBuffer();
- int currentInt = 0;
-
- //convert each byte to an int (as hex)
- for (int i = 0; i < digested.length; ++i) {
-
- //conver to int
- currentInt = digested[i] & INT_OFFSET;
-
- //is the int smaller than 16? (single digit hex)
- if (SINGLE_DIGIT > currentInt) {
-
- buffer.append('0');
- }
-
- buffer.append(Integer.toHexString(currentInt));
- }
-
- uid = buffer.toString();
-
- //validate
- if (null == uid) {
-
- throw new Exception("Failed to generate UID.");
- }
-
- return uid;
- }
-
- /**
- * Returns an MD5 sum for the char [] .
- *
- * @param chars []
- * @throws Exception
- * @return byte[]
- */
- private static byte[] digest(char[] chars) throws Exception {
-
- //validate
- if (null == chars) {
-
- throw new IllegalArgumentException("Cannot get MD5 sum for null char [].");
- }
-
- try {
-
- MessageDigest digest = MessageDigest.getInstance("MD5");
-
- //add the chars to the buffer
- int bufferSize = chars.length * 2;
- ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
- for (int i = 0; i < chars.length; i++) {
-
- buffer.putChar(chars[i]);
- }
-
- //digest
- return digest.digest(buffer.array());
- }
- catch (NoSuchAlgorithmException algorithmException) {
-
- throw new Exception("Could not get MD5 algorithm.", algorithmException);
- }
- }
-
- /**
- * Returns an unique seed.
- *
- * @throws Exception
- * @return char []
- */
- private static synchronized char[] getSeed() throws Exception {
-
- StringBuffer seed = new StringBuffer();
-
- //location in universe (cyberspace). A JVM running on a particular machine.
- seed.append(getIP());
- seed.append(getJVMID());
- //time (current millisecond)
- seed.append(getTime());
- //random, allows multiple new UIDS per millisecond. (validated by List)
- seed.append(getRandom());
- //another Class could be generating UIDS the same way within the same JVM
- seed.append(UIDGenerator.class.getName());
- //two random numbers could (possibly) be generated in the same millisecond
- seed.append(getCount());
- //more than one UIDGenerator class may be loaded by different ClassLoaders
- seed.append(getClassLoaderId());
-
- //conver to char []
- char[] chars = new char[seed.length()];
- seed.getChars(0, seed.length(), chars, 0);
-
- return chars;
- }
-
- /**
- * Returns a Random number/String.
- *
- * @return String
- */
- private static synchronized String getRandom() {
-
- long random = new SecureRandom().nextLong();
-
- return "" + random;
- }
-
- /**
- * Returns the current time in milliseconds.
- *
- * @return String
- */
- private static synchronized String getTime() {
-
- callTime = System.currentTimeMillis();
-
- return "" + callTime;
- }
-
- /**
- * Returns an incremented count of UIDs generated by this instance.
- * @return long
- */
- private static synchronized long getCount() {
- return++uidCounter;
- }
-
- /**
- * Returns the IP address for this machine.
- *
- * @throws Exception
- * @return String
- */
- private static synchronized String getIP() throws Exception {
-
- try {
-
- //lazily obtain IP address
- if (null == ipAddress) {
-
- ipAddress = InetAddress.getLocalHost().getHostAddress();
- }
-
- return ipAddress;
- }
- catch (UnknownHostException hostException) {
-
- throw new Exception("Could not determine IP Address.", hostException);
- }
- }
-
- /**
- * Returns an Unique Identifier for this particular Java Virtual Machine.
- *
- * @return String
- */
- private static synchronized String getJVMID() {
-
- //lazily obtain JVM ID
- if (null == vmID) {
-
- vmID = new VMID().toString();
- }
-
- return vmID;
- }
-
- /**
- * If multiple UIDGenerators are loaded by different class loaders, they
- * will be operating independantly of each other (ie. multiple web
- * applications within an application server), It is (remotely) possible
- * that multiple UIDGenerator may duplicate their count and random numbers
- * within the same millisecond.
- *
- * @return int
- */
- private static synchronized int getClassLoaderId() {
- //what ClassLoader within this JVM loaded this UIDGenerator class
- int classLoaderId = System.identityHashCode(UIDGenerator.class.
- getClassLoader());
- return classLoaderId;
- }
-}
Deleted: trunk/src/jar/query/java/org/jrdf/util/UIDGeneratorUnitTest.java
===================================================================
--- trunk/src/jar/query/java/org/jrdf/util/UIDGeneratorUnitTest.java 2011-09-28 19:16:18 UTC (rev 2057)
+++ trunk/src/jar/query/java/org/jrdf/util/UIDGeneratorUnitTest.java 2011-09-28 23:44:31 UTC (rev 2058)
@@ -1,292 +0,0 @@
-/*
- * $Header$
- * $Revision: 624 $
- * $Date: 2006-06-24 21:02:12 +1000 (Sat, 24 Jun 2006) $
- *
- * ====================================================================
- *
- * The Apache Software License, Version 1.1
- *
- * Copyright (c) 2003, 2004 The JRDF Project. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- *
- * 3. The end-user documentation included with the redistribution, if
- * any, must include the following acknowlegement:
- * "This product includes software developed by the
- * the JRDF Project (http://jrdf.sf.net/)."
- * Alternately, this acknowlegement may appear in the software itself,
- * if and wherever such third-party acknowlegements normally appear.
- *
- * 4. The names "The JRDF Project" and "JRDF" must not be used to endorse
- * or promote products derived from this software without prior written
- * permission. For written permission, please contact
- * newmana at users.sourceforge.net.
- *
- * 5. Products derived from this software may not be called "JRDF"
- * nor may "JRDF" appear in their names without prior written
- * permission of the JRDF Project.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
- * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
- * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- * ====================================================================
- *
- * This software consists of voluntary contributions made by many
- * individuals on behalf of the JRDF Project. For more
- * information on JRDF, please see <http://jrdf.sourceforge.net/>.
- */
-
-package org.jrdf.util;
-
-// Third party packages
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-import junit.textui.TestRunner;
-
-import java.lang.reflect.Method;
-import java.net.URL;
-import java.net.URLClassLoader;
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-
-/**
- * Unit test for testing the Unique Identifier Generator (UIDGenerator).
- *
- * @author <a href="mailto:robert.turner at tucanatech.com">Robert Turner </a>
- */
-public class UIDGeneratorUnitTest extends TestCase {
-
- /** Set of UIDs that have been generated. */
- private Set<String> uids;
-
- /** Number of UIDs generated. */
- private static final int NUM_UIDS = 10000;
-
- /** Number of Threads concurrently generating UIDs. */
- private static final int NUM_THREADS = 10;
-
- /** Number of ClassLoaders using UIDGenerator classes to genreateUIDs .*/
- private static final int NUM_CLASSLOADERS = 10;
-
- /** Short name of the UIDGenerator class. */
- private static final String CLASS_NAME = "UIDGenerator";
-
- /** Fully qualified name of the UIDGenerator class. */
- private static final String FULL_CLASS_NAME = "org.jrdf.util." + CLASS_NAME;
-
- /**
- * Constructs a new test with the given name.
- *
- * @param name
- * the name of the test
- */
- public UIDGeneratorUnitTest(String name) {
- super(name);
- }
-
- /**
- * Hook for test runner to obtain a test suite from.
- *
- * @return The test suite
- */
- public static Test suite() {
-
- TestSuite suite = new TestSuite();
- suite.addTest(new UIDGeneratorUnitTest("testUID"));
- suite.addTest(new UIDGeneratorUnitTest("testConcurrency"));
-// suite.addTest(new UIDGeneratorUnitTest("testMultiClassLoader"));
- return suite;
- }
-
- /**
- * Default test runner.
- *
- * @param args The command line arguments
- */
- public static void main(String[] args) {
-
- TestRunner.run(suite());
- }
-
- /**
- * Tests that UID are unique.
- *
- * @throws Exception
- */
- public void testUID() throws Exception {
-
- String currentUID = "";
-
- for (int i = 0; NUM_UIDS > i; i++) {
-
- currentUID = UIDGenerator.generateUID();
-
- //is it unique??
- if (uids.contains(currentUID)) {
-
- fail("UID set already contains UID [" + i + "]: " + currentUID);
- }
-
- uids.add(currentUID);
- }
- }
-
- /**
- * Tests that UID are unique when generated from differnet Threads.
- *
- * @throws Exception
- */
- public void testConcurrency() throws Exception {
-
- //threads that have to complete before the test finished
- List<Thread> threadList = new ArrayList<Thread>();
- Thread currentThread = null;
-
- //start threads that generate UIDs
- for (int i = 0; NUM_THREADS > i; i++) {
- //start a new thread that inserts and checks UIDS
- currentThread = new Thread(new Runnable() {
-
- /** run test */
- public void run() {
- try {
- String currentUID = "";
- int numUIDS = NUM_UIDS / NUM_THREADS;
- for (int i = 0; i < numUIDS; i++) {
- currentUID = UIDGenerator.generateUID();
- synchronized (UIDGeneratorUnitTest.class) {
- //is it unique??
- if (uids.contains(currentUID)) {
- fail("UID set already contains UID [" + i
- + "]: " + currentUID);
- }
- uids.add(currentUID);
- }
- }
- } catch (Exception exception) {
- throw new RuntimeException(
- "Error occurred while testing concurrency.",
- exception);
- }
- }
- });
- //end thread
-
- //keep reference
- threadList.add(currentThread);
-
- //start it
- currentThread.start();
- }
-
- //must wait for all threads to finish
- for (int i = 0; NUM_THREADS > i; i++) {
- threadList.get(i).join();
- }
- }
-
- /**
- * Tests the corner case of having multiple applications loaded by different
- * ClassLoaders.
- *
- * @throws Exception
- */
- @SuppressWarnings("unchecked")
- public void testMultiClassLoader() throws Exception {
-
- //load UIDGenerator from multiple classes
- URLClassLoader currentLoader = null;
- Class<UIDGenerator> currentUIDGeneratorClass = null;
-
- //get an URL to the UIDGenerator class file
- URL[] uidClass = new URL[] {
- ClassLoader.getSystemClassLoader().getResource(CLASS_NAME)};
-
- for (int i = 0; NUM_CLASSLOADERS > i; i++) {
- currentLoader = new URLClassLoader(uidClass);
- currentUIDGeneratorClass = (Class<UIDGenerator>)currentLoader.loadClass(FULL_CLASS_NAME);
- testUIDGeneratorClass(currentUIDGeneratorClass);
- }
-
- }
-
- /**
- * Uses the supplied UIDGenerator class to generate UIDs.
- *
- * @param uidGenerator
- * @throws Exception
- */
- private void testUIDGeneratorClass(Class<UIDGenerator> uidGenerator) throws Exception {
- //get the UIDGenerator's generateUID method
- Method generateUID = uidGenerator.getMethod("generateUID", (Class[])null);
- String currentUID = "";
- for (int i = 0; NUM_UIDS > i; i++) {
- currentUID = (String) generateUID.invoke(null, (Object[])null);
- //is it unique??
- if (uids.contains(currentUID)) {
- fail("UID set already contains UID [" + i + "]: " + currentUID);
- }
- uids.add(currentUID);
- }
- }
-
- //set up and tear down
-
- /**
- * Initialise members.
- *
- * @throws Exception if something goes wrong
- */
- public void setUp() throws Exception {
-
- try {
-
- uids = new HashSet<String>();
-
- super.setUp();
- } catch (Exception exception) {
-
- //try to tear down first
- tearDown();
-
- //then throw
- throw exception;
- }
- }
-
- /**
- * The teardown method for JUnit.
- *
- * @throws Exception
- */
- public void tearDown() throws Exception {
-
- uids.clear();
-
- //allow super to close down
- super.tearDown();
- }
-}
Modified: trunk/src/jar/util/java/org/mulgara/util/UIDGenerator.java
===================================================================
--- trunk/src/jar/util/java/org/mulgara/util/UIDGenerator.java 2011-09-28 19:16:18 UTC (rev 2057)
+++ trunk/src/jar/util/java/org/mulgara/util/UIDGenerator.java 2011-09-28 23:44:31 UTC (rev 2058)
@@ -1,70 +1,94 @@
/*
- * The contents of this file are subject to the Mozilla Public License
- * Version 1.1 (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.mozilla.org/MPL/
+ * $Date: 2006-06-24 21:02:12 +1000 (Sat, 24 Jun 2006) $
*
- * Software distributed under the License is distributed on an "AS IS"
- * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
- * the License for the specific language governing rights and limitations
- * under the License.
+ * ====================================================================
*
- * The Original Code is the Kowari Metadata Store.
+ * The Apache Software License, Version 1.1
*
- * The Initial Developer of the Original Code is Plugged In Software Pty
- * Ltd (http://www.pisoftware.com, mailto:info at pisoftware.com). Portions
- * created by Plugged In Software Pty Ltd are Copyright (C) 2001,2002
- * Plugged In Software Pty Ltd. All Rights Reserved.
+ * Copyright (c) 2003, 2004 The JRDF Project. All rights reserved.
*
- * Contributor(s): N/A.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
*
- * [NOTE: The text of this Exhibit A may differ slightly from the text
- * of the notices in the Source Code files of the Original Code. You
- * should use the text of this Exhibit A rather than the text found in the
- * Original Code Source Code for Your Modifications.]
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
*
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution, if
+ * any, must include the following acknowlegement:
+ * "This product includes software developed by the
+ * the JRDF Project (http://jrdf.sf.net/)."
+ * Alternately, this acknowlegement may appear in the software itself,
+ * if and wherever such third-party acknowlegements normally appear.
+ *
+ * 4. The names "The JRDF Project" and "JRDF" must not be used to endorse
+ * or promote products derived from this software without prior written
+ * permission. For written permission, please contact
+ * newmana at users.sourceforge.net.
+ *
+ * 5. Products derived from this software may not be called "JRDF"
+ * nor may "JRDF" appear in their names without prior written
+ * permission of the JRDF Project.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the JRDF Project. For more
+ * information on JRDF, please see <http://jrdf.sourceforge.net/>.
*/
package org.mulgara.util;
//Java 2 standard packages
-import java.net.*;
-import java.rmi.dgc.*;
-import java.security.*;
-import java.nio.*;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.nio.ByteBuffer;
+import java.rmi.dgc.VMID;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.security.SecureRandom;
/**
* Utility class that generates an Unique identifier.
*
- * @created 2004-09-29
- *
* @author <a href="mailto:robert.turner at tucanatech.com">Robert Turner</a>
- *
- * @version $Revision: 1.10 $
- *
- * @modified $Date: 2005/02/03 20:07:37 $ by $Author: newmana $
- *
- * @company <a href="http://www.tucanatech.com/">Tucana Technologies</a>
- *
- * @copyright ©2001 <a href="http://www.pisoftware.com/">Plugged In
- * Software Pty Ltd</a>
- *
- * @licence <a href="{@docRoot}/../../LICENCE">Mozilla Public License v1.1</a>
*/
public class UIDGenerator {
- /** Computers IP address */
- private static String ipAddress = null;
+ /** Computers IP address. */
+ private static String ipAddress;
- /** Unique Java Virtual Machine Identifier */
- private static String vmID = null;
+ /** Unique Java Virtual Machine Identifier. */
+ private static String vmID;
- /** time the method was called (used to prevent duplicates) */
- private static long callTime = 0;
+ /** time the method was called (used to prevent duplicates). */
+ private static long callTime;
/** count of UIDs generated by this JVM (to avoid per millisecond duplicates). */
- private static long uidCounter = 0;
+ private static long uidCounter;
+ private static final int SINGLE_DIGIT = 0x10;
+ private static final int INT_OFFSET = 0xFF;
+ private UIDGenerator() {
+ }
+
/**
* Generates an Unique Identifier using the current time and the machines
* IP address.
@@ -72,7 +96,7 @@
* @throws Exception
* @return String
*/
- public synchronized static String generateUID() throws Exception {
+ public static synchronized String generateUID() throws Exception {
String uniqueID = getUniqueID(getSeed());
return uniqueID;
}
@@ -84,7 +108,7 @@
* @throws Exception
* @return String
*/
- private synchronized static String getUniqueID(char[] seed) throws
+ private static synchronized String getUniqueID(char[] seed) throws
Exception {
String uid = null;
@@ -98,10 +122,10 @@
for (int i = 0; i < digested.length; ++i) {
//conver to int
- currentInt = digested[i] & 0xFF;
+ currentInt = digested[i] & INT_OFFSET;
//is the int smaller than 16? (single digit hex)
- if (currentInt < 0x10) {
+ if (SINGLE_DIGIT > currentInt) {
buffer.append('0');
}
@@ -112,7 +136,7 @@
uid = buffer.toString();
//validate
- if (uid == null) {
+ if (null == uid) {
throw new Exception("Failed to generate UID.");
}
@@ -121,7 +145,7 @@
}
/**
- * Returns an MD5 sum for the char []
+ * Returns an MD5 sum for the char [] .
*
* @param chars []
* @throws Exception
@@ -130,7 +154,7 @@
private static byte[] digest(char[] chars) throws Exception {
//validate
- if (chars == null) {
+ if (null == chars) {
throw new IllegalArgumentException("Cannot get MD5 sum for null char [].");
}
@@ -162,7 +186,7 @@
* @throws Exception
* @return char []
*/
- private synchronized static char[] getSeed() throws Exception {
+ private static synchronized char[] getSeed() throws Exception {
StringBuffer seed = new StringBuffer();
@@ -192,7 +216,7 @@
*
* @return String
*/
- private synchronized static String getRandom() {
+ private static synchronized String getRandom() {
long random = new SecureRandom().nextLong();
@@ -204,7 +228,7 @@
*
* @return String
*/
- private synchronized static String getTime() {
+ private static synchronized String getTime() {
callTime = System.currentTimeMillis();
@@ -215,7 +239,7 @@
* Returns an incremented count of UIDs generated by this instance.
* @return long
*/
- private synchronized static long getCount() {
+ private static synchronized long getCount() {
return++uidCounter;
}
@@ -225,12 +249,12 @@
* @throws Exception
* @return String
*/
- private synchronized static String getIP() throws Exception {
+ private static synchronized String getIP() throws Exception {
try {
//lazily obtain IP address
- if (ipAddress == null) {
+ if (null == ipAddress) {
ipAddress = InetAddress.getLocalHost().getHostAddress();
}
@@ -246,13 +270,12 @@
/**
* Returns an Unique Identifier for this particular Java Virtual Machine.
*
- * @throws Exception
* @return String
*/
- private synchronized static String getJVMID() throws Exception {
+ private static synchronized String getJVMID() {
//lazily obtain JVM ID
- if (vmID == null) {
+ if (null == vmID) {
vmID = new VMID().toString();
}
@@ -269,7 +292,7 @@
*
* @return int
*/
- private synchronized static int getClassLoaderId() {
+ private static synchronized int getClassLoaderId() {
//what ClassLoader within this JVM loaded this UIDGenerator class
int classLoaderId = System.identityHashCode(UIDGenerator.class.
getClassLoader());
More information about the Mulgara-svn
mailing list