View Javadoc

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