| 1 | package dmg.cells.nucleus; |
| 2 | |
| 3 | import java.io.Serializable; |
| 4 | import java.util.Date; |
| 5 | import java.util.concurrent.atomic.AtomicLong; |
| 6 | |
| 7 | /** |
| 8 | * uoid is the 'Unique Message Identifier'. |
| 9 | * |
| 10 | * |
| 11 | * @author Patrick Fuhrmann |
| 12 | * @version 0.1, 15 Feb 1998 |
| 13 | * |
| 14 | * WARNING : This Class is designed to be immutable. All other class rely on that |
| 15 | * fact and a lot of things may fail at runtime if this design item is changed. |
| 16 | */ |
| 17 | |
| 18 | public class UOID implements Serializable, Cloneable { |
| 19 | |
| 20 | static final long serialVersionUID = -5940693996555861085L; |
| 21 | |
| 22 | private static final AtomicLong __counter = new AtomicLong(100); |
| 23 | |
| 24 | private final long _counter; |
| 25 | private final long _time; |
| 26 | |
| 27 | /** |
| 28 | * The constructor creates an instance of an uoid which is assumed to be |
| 29 | * different from all other uoid's created before that time and after. This |
| 30 | * behavior is guaranteed for all uoids created inside of one virtual |
| 31 | * machine and very likely for all others. |
| 32 | */ |
| 33 | public UOID() { |
| 34 | _time = System.currentTimeMillis(); |
| 35 | _counter = __counter.incrementAndGet(); |
| 36 | } |
| 37 | |
| 38 | @Override |
| 39 | public Object clone() { |
| 40 | // it's safe to do so, UOID is immutable |
| 41 | return this; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * creates a hashcode which is more optimal then the object hashCode. |
| 46 | */ |
| 47 | @Override |
| 48 | public int hashCode() { |
| 49 | // System.out.println( " hashCode called " ) ; |
| 50 | return (int) (_counter & 0xffffffff); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * compares two uoids and overwrites Object.equals. |
| 55 | */ |
| 56 | @Override |
| 57 | public boolean equals(Object x) { |
| 58 | if( x == this ) return true; |
| 59 | if (!(x instanceof UOID)) return false; |
| 60 | UOID u = (UOID) x; |
| 61 | return (u._counter == _counter) && (u._time == _time); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public String toString() { |
| 66 | return "<" + _time + ":" + _counter + ">"; |
| 67 | } |
| 68 | |
| 69 | public static void main(String[] args) { |
| 70 | if (args.length == 0) { |
| 71 | UOID a = new UOID(); |
| 72 | System.out.println(" UOID : " + a); |
| 73 | } else { |
| 74 | Date date = new Date(Long.parseLong(args[0])); |
| 75 | System.out.println(date.toString()); |
| 76 | } |
| 77 | |
| 78 | } |
| 79 | } |