01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.aspectwerkz.proxy;
05:
06: /**
07: * NOTE:
08: * <p/>
09: * This code is based on code from the [Plasmid Replication Engine] project.
10: * <br/>
11: * Licensed under [Mozilla Public License 1.0 (MPL)].
12: * <p/>
13: * Original JavaDoc:
14: * <p/>
15: * Our distributed objects are generally named most efficiently (and cleanly)
16: * by their UUID's. This class provides some static helpers for using UUID's.
17: * If it was efficient to do in Java, I would make the uuid an normal class
18: * and use instances of it. However, in current JVM's, we would end up using an
19: * Object to represent a long, which is pretty expensive. Maybe someday. ###
20: * <p/>
21: * UUID format: currently using currentTimeMillis() for the low bits. This uses
22: * about 40 bits for the next 1000 years, leaving 24 bits for debugging
23: * and consistency data. I'm using 8 of those for a magic asci 'U' byte.
24: * <p/>
25: * Future: use one instance of Uuid per type of object for better performance
26: * and more detailed info (instance could be matched to its uuid's via a map or
27: * array). This all static version bites.###
28: */
29: public final class Uuid {
30:
31: public static final long UUID_NONE = 0;
32: public static final long UUID_WILD = -1;
33: public static final long UUID_MAGICMASK = 0xff << 56;
34: public static final long UUID_MAGIC = 'U' << 56;
35:
36: protected static long lastTime;
37:
38: /**
39: * Generate and return a new Universally Unique ID.
40: * Happens to be monotonically increasing.
41: */
42: public synchronized static long newUuid() {
43: long time = System.currentTimeMillis();
44:
45: if (time <= lastTime) {
46: time = lastTime + 1;
47: }
48: lastTime = time;
49: return UUID_MAGIC | time;
50: }
51:
52: /**
53: * Returns true if uuid could have been generated by Uuid.
54: */
55: public static boolean isValid(final long uuid) {
56: return (uuid & UUID_MAGICMASK) == UUID_MAGIC
57: && (uuid & ~UUID_MAGICMASK) != 0;
58: }
59: }
|