001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * @author Mikhail A. Markov
021: * @version $Revision: 1.7.4.3 $
022: */package java.rmi.server;
023:
024: import java.io.DataInput;
025: import java.io.DataOutput;
026: import java.io.IOException;
027: import java.io.Serializable;
028:
029: /**
030: * @com.intel.drl.spec_ref
031: *
032: * @author Mikhail A. Markov
033: * @version $Revision: 1.7.4.3 $
034: */
035: public final class UID implements Serializable {
036:
037: private static final long serialVersionUID = 1086053664494604050L;
038: private int unique;
039: private long time;
040: private short count;
041:
042: // Sequentially increasing counter for initializing count field.
043: private static short countCounter = Short.MIN_VALUE;
044:
045: // Time when last UID was created.
046: private static long lastCreationTime = System.currentTimeMillis();
047:
048: // Lock object for synchronization.
049: private static class Lock {
050: }
051:
052: private static final Object lock = new Lock();
053:
054: // unique identifier for this VM.
055: private static final int vmUnique = lock.hashCode();
056:
057: /**
058: * @com.intel.drl.spec_ref
059: */
060: public UID(short num) {
061: unique = 0;
062: time = 0;
063: count = num;
064: }
065:
066: /**
067: * @com.intel.drl.spec_ref
068: */
069: public UID() {
070: synchronized (lock) {
071: if (countCounter == Short.MAX_VALUE) {
072: long curTime = System.currentTimeMillis();
073:
074: while (curTime - lastCreationTime < 1000) {
075: try {
076: // sleep for a while
077: Thread.sleep(1000);
078: } catch (InterruptedException ie) {
079: }
080: curTime = System.currentTimeMillis();
081: }
082: lastCreationTime = curTime;
083: countCounter = Short.MIN_VALUE;
084: }
085: unique = vmUnique;
086: time = lastCreationTime;
087: count = countCounter++;
088: }
089: }
090:
091: /**
092: * @com.intel.drl.spec_ref
093: */
094: public String toString() {
095: return "UID[" + Integer.toString(unique, 16) + ":" //$NON-NLS-1$ //$NON-NLS-2$
096: + Long.toString(time, 16) + ":" //$NON-NLS-1$
097: + Integer.toString(count, 16) + "]"; //$NON-NLS-1$
098: }
099:
100: /**
101: * @com.intel.drl.spec_ref
102: */
103: public boolean equals(Object obj) {
104: if (obj != null && (obj instanceof UID)) {
105: UID uid = (UID) obj;
106: return (unique == uid.unique) && (time == uid.time)
107: && (count == uid.count);
108: }
109: return false;
110: }
111:
112: /**
113: * @com.intel.drl.spec_ref
114: */
115: public void write(DataOutput out) throws IOException {
116: out.writeInt(unique);
117: out.writeLong(time);
118: out.writeShort(count);
119: }
120:
121: /**
122: * @com.intel.drl.spec_ref
123: */
124: public int hashCode() {
125: return (int) (time ^ count);
126: }
127:
128: /**
129: * @com.intel.drl.spec_ref
130: */
131: public static UID read(DataInput in) throws IOException {
132: UID uid = new UID((short) -1);
133: uid.unique = in.readInt();
134: uid.time = in.readLong();
135: uid.count = in.readShort();
136: return uid;
137: }
138: }
|