KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
25  * A globally unique identifier (globally across a cluster of virtual
26  * machines).
27  *
28  * <p>The identifier is composed of:
29  * <ol>
30  * <li>The VMID for the virtual machine.</li>
31  * <li>A UID to provide uniqueness over a VMID.</li>
32  * </ol>
33  *
34  * <pre>
35  * [ address ] - [ process id ] - [ time ] - [ counter ] - [ time ] - [ counter ]
36  * |------- UID --------| |------- UID --------|
37  * |---------------------- VMID -----------------------|
38  * </pre>
39  *
40  * @see VMID
41  * @see UID
42  *
43  * @version <tt>$Revision: 1958 $</tt>
44  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
45  */

46 public class GUID
47    implements ID, Comparable JavaDoc
48 {
49    /** The serial version id, @since 1.6 */
50    static final long serialVersionUID = 3289509836244263718L;
51    /** The virtual machine identifier */
52    protected final VMID vmid;
53
54    /** The unique identifier */
55    protected final UID uid;
56
57    /** The hash code of this GUID */
58    protected final int hashCode;
59
60    /**
61     * Construct a new GUID.
62     */

63    public GUID() {
64       this.vmid = VMID.getInstance();
65       this.uid = new UID();
66
67       // generate a hash code for this GUID
68
int code = vmid.hashCode();
69       code ^= uid.hashCode();
70       hashCode = code;
71    }
72
73    /**
74     * Copy a GUID.
75     *
76     * @param guid GUID to copy.
77     */

78    protected GUID(final GUID guid) {
79       this.vmid = guid.vmid;
80       this.uid = guid.uid;
81       this.hashCode = guid.hashCode;
82    }
83
84    /**
85     * Get the VMID portion of this GUID.
86     *
87     * @return The VMID portion of this GUID.
88     */

89    public final VMID getVMID() {
90       return vmid;
91    }
92
93    /**
94     * Get the UID portion of this GUID.
95     *
96     * @return The UID portion of this GUID.
97     */

98    public final UID getUID() {
99       return uid;
100    }
101    
102    /**
103     * Return a string representation of this GUID.
104     *
105     * @return A string representation of this GUID.
106     */

107    public String JavaDoc toString() {
108       return vmid.toString() + "-" + uid.toString();
109    }
110
111    /**
112     * Return the hash code of this GUID.
113     *
114     * @return The hash code of this GUID.
115     */

116    public int hashCode() {
117       return hashCode;
118    }
119
120    /**
121     * Check if the given object is equal to this GUID.
122     *
123     * <p>A GUID is equal to another GUID if the VMID and UID portions are
124     * equal.
125     *
126     * @param obj Object to test equality with.
127     * @return True if object is equal to this GUID.
128     */

129    public boolean equals(final Object JavaDoc obj) {
130       if (obj == this) return true;
131
132       if (obj != null && obj.getClass() == getClass()) {
133          GUID guid = (GUID)obj;
134
135          return
136             guid.vmid.equals(vmid) &&
137             guid.uid.equals(uid);
138       }
139
140       return false;
141    }
142
143    /**
144     * Returns a copy of this GUID.
145     *
146     * @return A copy of this GUID.
147     */

148    public Object JavaDoc clone() {
149       try {
150          return super.clone();
151       }
152       catch (CloneNotSupportedException JavaDoc e) {
153          throw new InternalError JavaDoc();
154       }
155    }
156
157    /**
158     * Returns a GUID as a string.
159     *
160     * @return GUID as a string.
161     */

162    public static String JavaDoc asString() {
163       return new GUID().toString();
164    }
165
166    public int compareTo(Object JavaDoc o)
167    {
168       GUID guid = (GUID)o;
169       return this.toString().compareTo(guid.toString());
170    }
171    
172 }
173
Popular Tags