1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2000-2007 Sun Microsystems, Inc. All rights reserved.
5 *
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common Development
8 * and Distribution License ("CDDL") (collectively, the "License"). You may
9 * not use this file except in compliance with the License. You can obtain
10 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
11 * or mq/legal/LICENSE.txt. See the License for the specific language
12 * governing permissions and limitations under the License.
13 *
14 * When distributing the software, include this License Header Notice in each
15 * file and include the License file at mq/legal/LICENSE.txt. Sun designates
16 * this particular file as subject to the "Classpath" exception as provided by
17 * Sun in the GPL Version 2 section of the License file that accompanied this
18 * code. If applicable, add the following below the License Header, with the
19 * fields enclosed by brackets [] replaced by your own identifying information:
20 * "Portions Copyrighted [year] [name of copyright owner]"
21 *
22 * Contributor(s):
23 *
24 * If you wish your version of this file to be governed by only the CDDL or
25 * only the GPL Version 2, indicate your decision by adding "[Contributor]
26 * elects to include this software in this distribution under the [CDDL or GPL
27 * Version 2] license." If you don't indicate a single choice of license, a
28 * recipient has the option to distribute your version of this file under
29 * either the CDDL, the GPL Version 2 or to extend the choice of license to
30 * its licensees as provided above. However, if you add GPL Version 2 code
31 * and therefore, elected the GPL Version 2 license, then the option applies
32 * only if the new code is made subject to such option by the copyright holder.
33 */
34
35 /*
36 * @(#)UID.java 1.10 07/06/07
37 */
38
39 package com.sun.messaging.jmq.util;
40
41 import java.io;
42 import java.util.Random;
43
44 /**
45 * An encapsulation of a JMQ Unique ID. A Unique ID is an ID
46 * with the following characteristics.
47 *
48 * 1. Is unique in this VM
49 * 2. Will stay unique for a very long time (over 34 years)
50 * 3. Will stay unique across VM restarts (due to #2)
51 * 4. Can be made more unique by the caller providing a 16 bit prefix
52 *
53 * For more information see UniqueID.java
54 *
55 */
56 public class UID implements Serializable {
57
58 private static final long serialVersionUID = -583620884703541778L;
59
60 // Default prefix to something random
61 protected static short prefix = (short)((new Random()).nextInt(Short.MAX_VALUE));
62
63 protected long id = 0;
64
65 transient int hashCode = 0;
66 transient String unique_id = null;
67
68 /**
69 * Constructs a new UID. You may want to call
70 * setPrefix() to specify a prefix before creating any UID,
71 * otherwise a random prefix will be used.
72 */
73 public UID() {
74 this.id = UniqueID.generateID(prefix);
75 }
76
77 /**
78 * Constructs a new UID using the argument as the identifier.
79 * This constructor performs no checking as to the uniqueness of
80 * <code>id</code>
81 *
82 * @param id The identifier to initialize this UID from.
83 */
84 public UID(long id) {
85 this.id = id;
86 }
87
88 /**
89 * Set the prefix to us for all UID generated. UIDs are guaranteed
90 * to be unique in this VM no matter what the prefix. The prefix can
91 * be used for additional uniqueness. By default a random prefix is
92 * be used.
93 *
94 * @param p prefix to use
95 */
96 public static void setPrefix(short p) {
97 prefix = p;
98 }
99
100 /**
101 * Get the prefix being used to generate Ids.
102 */
103 public static short getPrefix() {
104 return prefix;
105 }
106
107 /**
108 * Return the identifier as a long
109 */
110 public long longValue() {
111 return id;
112 }
113
114 /**
115 * Return the hash code for the indentifier
116 */
117 public int hashCode() {
118 if (hashCode == 0) {
119 hashCode = UniqueID.hashCode(id);
120 }
121 return hashCode;
122 }
123
124 /**
125 * Equals
126 */
127 public boolean equals(Object obj) {
128 if (! (obj instanceof UID)) {
129 return false;
130 }
131 return (this.id == ((UID)obj).id);
132 }
133
134 /**
135 * Return the age in milliseconds of this identifier
136 */
137 public long age() {
138 return UniqueID.age(id);
139 }
140
141 /**
142 * Return the age in milliseconds of this identifier
143 *
144 * @param currentTime Current time in milliseconds
145 */
146 public long age(long currentTime) {
147 return UniqueID.age(id, currentTime);
148 }
149
150 /**
151 * Return the timestamp in milliseconds of this identifier
152 */
153 public long getTimestamp() {
154 return UniqueID.getTimestamp(id);
155 }
156
157 /**
158 * Return a short string representation of this identifier
159 */
160 public String toString() {
161 return String.valueOf(id);
162 }
163
164 /**
165 * Return a short string that can be used as a key. This is
166 * for backwards compatibility with earlier style identifiers.
167 */
168 public String getUniqueName() {
169 if (unique_id == null) {
170 unique_id = this.toString();
171 }
172 return unique_id;
173 }
174
175 /**
176 * Return a long string representation of this identifier
177 */
178 public String toLongString() {
179 return UniqueID.toLongString(id);
180 }
181
182 /**
183 * Marshals a binary representation of this UID to a
184 * Data OutPut stream. The binary representation is just a long.
185 */
186 public void writeUID(DataOutputStream out)
187 throws IOException {
188 out.writeLong(this.id);
189 }
190
191 /**
192 * Constructs and returns a new UID instance by unmarshalling
193 * a binary representation from an input stream.
194 */
195 public static UID readUID(DataInputStream in) throws IOException {
196 long n = in.readLong();
197 return new UID(n);
198 }
199
200 }