1 20 package org.apache.cactus.internal.util; 21 22 import java.net.InetAddress ; 23 import java.net.UnknownHostException ; 24 25 import junit.framework.TestCase; 26 27 32 public class UniqueGenerator 33 { 34 39 private static byte count = 0; 40 41 44 private static Object lock = new Object (); 45 46 49 private static String ipAddress; 50 static 51 { 52 try 53 { 54 byte ip[] = InetAddress.getLocalHost().getAddress(); 55 ipAddress = toHex(((ip[0] & 0xff) << 24) 56 | ((ip[1] & 0xff) << 16) | ((ip[2] & 0xff) << 8) 57 | (ip[3] & 0xff)); 58 } 59 catch (UnknownHostException e) 60 { 61 ipAddress = ""; 62 } 63 } 64 65 71 public static String generate(TestCase theTestCase) 72 { 73 long time = System.currentTimeMillis(); 74 synchronized (lock) 75 { 76 time += count++; 77 } 78 return generate(theTestCase, time); 79 } 80 81 88 public static String generate(TestCase theTestCase, 89 long theTime) 90 { 91 String id = ipAddress; 92 id += "-" + toHex(theTime); 93 id += "-" + toHex(System.identityHashCode(theTestCase)); 94 id += toHex(theTestCase.getName().hashCode()); 95 return id; 96 } 97 98 104 private static String toHex(long theValue) 105 { 106 return Long.toString(theValue, 16).toUpperCase(); 107 } 108 109 } 110 | Popular Tags |