1 /*
2 * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
101 */
102 private final short count;
103
104 /**
105 * Generates a <code>UID</code> that is unique over time with
106 * respect to the host that it was generated on.
107 */
108 public UID() {
109
110 synchronized (lock) {
111 if (!hostUniqueSet) {
112 hostUnique = (new SecureRandom()).nextInt();
113 hostUniqueSet = true;
114 }
115 unique = hostUnique;
116 if (lastCount == Short.MAX_VALUE) {
117 boolean interrupted = Thread.interrupted();
118 boolean done = false;
119 while (!done) {
120 long now = System.currentTimeMillis();
121 if (now <= lastTime) {
122 // wait for time to change
123 try {
124 Thread.currentThread().sleep(1);
125 } catch (InterruptedException e) {
126 interrupted = true;
127 }
128 } else {
129 lastTime = now;
130 lastCount = Short.MIN_VALUE;
131 done = true;
132 }
133 }
134 if (interrupted) {
135 Thread.currentThread().interrupt();
136 }
137 }
138 time = lastTime;
139 count = lastCount++;
140 }
141 }
142
143 /**
144 * Creates a "well-known" <code>UID</code>.
145 *
146 * There are 2<sup>16</sup> possible such well-known ids.
147 *
148 * <p>A <code>UID</code> created via this constructor will not
149 * clash with any <code>UID</code>s generated via the no-arg
|
1 /*
2 * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
101 */
102 private final short count;
103
104 /**
105 * Generates a <code>UID</code> that is unique over time with
106 * respect to the host that it was generated on.
107 */
108 public UID() {
109
110 synchronized (lock) {
111 if (!hostUniqueSet) {
112 hostUnique = (new SecureRandom()).nextInt();
113 hostUniqueSet = true;
114 }
115 unique = hostUnique;
116 if (lastCount == Short.MAX_VALUE) {
117 boolean interrupted = Thread.interrupted();
118 boolean done = false;
119 while (!done) {
120 long now = System.currentTimeMillis();
121 if (now == lastTime) {
122 // wait for time to change
123 try {
124 Thread.sleep(1);
125 } catch (InterruptedException e) {
126 interrupted = true;
127 }
128 } else {
129 // If system time has gone backwards increase
130 // original by 1ms to maintain uniqueness
131 lastTime = (now < lastTime) ? lastTime+1 : now;
132 lastCount = Short.MIN_VALUE;
133 done = true;
134 }
135 }
136 if (interrupted) {
137 Thread.currentThread().interrupt();
138 }
139 }
140 time = lastTime;
141 count = lastCount++;
142 }
143 }
144
145 /**
146 * Creates a "well-known" <code>UID</code>.
147 *
148 * There are 2<sup>16</sup> possible such well-known ids.
149 *
150 * <p>A <code>UID</code> created via this constructor will not
151 * clash with any <code>UID</code>s generated via the no-arg
|