KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > id > UID


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util.id;
23
24 import EDU.oswego.cs.dl.util.concurrent.SynchronizedLong;
25
26 /**
27  * A unique identifier (uniqueness only guarantied inside of the virtual
28  * machine in which it was created).
29  *
30  * <p>The identifier is composed of:
31  * <ol>
32  * <li>A long generated from the current system time (in milliseconds).</li>
33  * <li>A long generated from a counter (which is the number of UID objects
34  * that have been created durring the life of the executing virtual
35  * machine).</li>
36  * </ol>
37  *
38  * <pre>
39  * [ time ] - [ counter ]
40  * </pre>
41  *
42  * <p>Numbers are converted to radix(Character.MAX_RADIX) when converting
43  * to strings.
44  *
45  * <p>This <i>should</i> provide adequate uniqueness for most purposes.
46  *
47  * @version <tt>$Revision: 1958 $</tt>
48  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
49  */

50 public class UID
51    implements ID
52 {
53    private static final long serialVersionUID = -8093336932569424512L;
54
55    /** A counter for generating identity values */
56    protected static final SynchronizedLong COUNTER = new SynchronizedLong(0);
57
58    /** The time portion of the UID */
59    protected final long time;
60
61    /** The identity portion of the UID */
62    protected final long id;
63
64    /**
65     * Construct a new UID.
66     */

67    public UID() {
68       time = System.currentTimeMillis();
69       id = COUNTER.increment();
70    }
71
72    /**
73     * Copy a UID.
74     */

75    protected UID(final UID uid) {
76       time = uid.time;
77       id = uid.id;
78    }
79
80    /**
81     * Get the time portion of this UID.
82     *
83     * @return The time portion of this UID.
84     */

85    public final long getTime() {
86       return time;
87    }
88
89    /**
90     * Get the identity portion of this UID.
91     *
92     * @return The identity portion of this UID.
93     */

94    public final long getID() {
95       return id;
96    }
97
98    /**
99     * Return a string representation of this UID.
100     *
101     * @return A string representation of this UID.
102     */

103    public String JavaDoc toString() {
104       return
105          Long.toString(time, Character.MAX_RADIX) +
106          "-" +
107          Long.toString(id, Character.MAX_RADIX);
108    }
109
110    /**
111     * Return the hash code of this UID.
112     *
113     * @return The hash code of this UID.
114     */

115    public int hashCode() {
116       return (int)id;
117    }
118
119    /**
120     * Checks if the given object is equal to this UID.
121     *
122     * @param obj Object to test equality with.
123     * @return True if object is equal to this UID.
124     */

125    public boolean equals(final Object JavaDoc 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    /**
140     * Returns a copy of this UID.
141     *
142     * @return A copy of this UID.
143     */

144    public Object JavaDoc clone() {
145       try {
146          return super.clone();
147       }
148       catch (CloneNotSupportedException JavaDoc e) {
149          throw new InternalError JavaDoc();
150       }
151    }
152
153    /**
154     * Returns a UID as a string.
155     *
156     * @return UID as a string.
157     */

158    public static String JavaDoc asString() {
159       return new UID().toString();
160    }
161 }
162
Popular Tags