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