1 22 package org.jboss.util.id; 23 24 import EDU.oswego.cs.dl.util.concurrent.SynchronizedLong; 25 26 50 public class UID 51 implements ID 52 { 53 private static final long serialVersionUID = -8093336932569424512L; 54 55 56 protected static final SynchronizedLong COUNTER = new SynchronizedLong(0); 57 58 59 protected final long time; 60 61 62 protected final long id; 63 64 67 public UID() { 68 time = System.currentTimeMillis(); 69 id = COUNTER.increment(); 70 } 71 72 75 protected UID(final UID uid) { 76 time = uid.time; 77 id = uid.id; 78 } 79 80 85 public final long getTime() { 86 return time; 87 } 88 89 94 public final long getID() { 95 return id; 96 } 97 98 103 public String toString() { 104 return 105 Long.toString(time, Character.MAX_RADIX) + 106 "-" + 107 Long.toString(id, Character.MAX_RADIX); 108 } 109 110 115 public int hashCode() { 116 return (int)id; 117 } 118 119 125 public boolean equals(final Object obj) { 126 if (obj == this) return true; 127 128 if (obj != null && obj.getClass() == getClass()) { 129 UID uid = (UID)obj; 130 131 return 132 uid.time == time && 133 uid.id == id; 134 } 135 136 return false; 137 } 138 139 144 public Object clone() { 145 try { 146 return super.clone(); 147 } 148 catch (CloneNotSupportedException e) { 149 throw new InternalError (); 150 } 151 } 152 153 158 public static String asString() { 159 return new UID().toString(); 160 } 161 } 162 | Popular Tags |