Merge from mainline.
[official-gcc.git] / libjava / classpath / java / rmi / server / UID.java
blob359630422119efab7d610cd653defb86979616df
1 /* UID.java -- The unique object Id
2 Copyright (c) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.rmi.server;
41 import java.io.DataInput;
42 import java.io.DataOutput;
43 import java.io.IOException;
44 import java.io.Serializable;
45 import java.net.InetAddress;
47 /**
48 * Represents the unique identifier over time for the host which has generated
49 * it. It contains time (when created), counter (the number of the UID
50 * creation order) and virtual machine id components. The UID can also be
51 * constructed specifying a "well known" identifier in the for of short:
52 * this identifier defines the UID uniqueness alone.
54 * @author Audrius Meskauskas (audriusa@bioinformatics.org)
56 public final class UID
57 implements Serializable
59 /**
60 * Use the serial version uid for interoperability.
62 private static final long serialVersionUID = 1086053664494604050L;
64 /**
65 * The UID counter (the ordinary number in the sequence of number of UID's,
66 * created during the recent millisecond). In the next millisecond, it
67 * starts from the minimal value again. In the unlikely case of creating
68 * more than 65536 uids per millisecond the process pauses till the next
69 * ms.
71 private static short uidCounter = Short.MIN_VALUE;
73 /**
74 * The time, when the last UID has been created.
76 private static long last;
78 /**
79 * This constant tries to be the unique identifier of the virtual machine.
81 private static final int machineId = getMachineId();
83 /**
84 * The UID number in the UID creation sequence.
86 private short count;
88 /**
89 * Always gets the uniqueNr value.
91 private int unique;
93 /**
94 * The time stamp, when the UID was created.
96 private long time;
98 /**
99 * Create the new UID that would have the described features of the
100 * uniqueness.
102 public UID()
104 time = System.currentTimeMillis();
105 unique = machineId;
106 if (time > last)
108 last = time;
109 count = uidCounter = Short.MIN_VALUE;
111 else
113 synchronized (UID.class)
115 if (uidCounter == Short.MAX_VALUE)
117 // Make a 2 ms pause if the counter has reached the maximal
118 // value. This should seldom happen.
121 Thread.sleep(2);
123 catch (InterruptedException e)
126 uidCounter = Short.MIN_VALUE;
127 time = last = System.currentTimeMillis();
130 count = uidCounter++;
136 * Create the new UID with the well known id (number). All UIDs, creates
137 * with the this constructor having the same parameter are equal to each
138 * other (regardless to the host and time where they were created.
140 * @param wellKnownId the well known UID.
142 public UID(short wellKnownId)
144 unique = wellKnownId;
148 * Get the hashCode of this UID.
150 public int hashCode()
152 return (int) (unique ^ time ^ count);
156 * Compare this UID with another UID for equality (not equal to other types of
157 * objects).
159 public boolean equals(Object other)
161 if (other instanceof UID)
163 UID ui = (UID) other;
164 return unique == ui.unique && time == ui.time && count == ui.count;
166 else
167 return false;
170 public static UID read(DataInput in) throws IOException
172 UID uid = new UID();
173 uid.unique = in.readInt();
174 uid.time = in.readLong();
175 uid.count = in.readShort();
176 return (uid);
179 public void write(DataOutput out) throws IOException
181 out.writeInt(unique);
182 out.writeLong(time);
183 out.writeShort(count);
187 * Do our best to get the Id of this virtual machine.
189 static int getMachineId()
191 int hostIpHash;
195 // Try to get the host IP.
196 String host = InetAddress.getLocalHost().toString();
197 // This hash is content - based, not the address based.
198 hostIpHash = host.hashCode();
200 catch (Exception e)
202 // Failed due some reason.
203 hostIpHash = 0;
206 // Should be the unque address if hashcodes are addresses.
207 // Additionally, add the time when the RMI system was probably started
208 // (this class was first instantiated).
209 return new Object().hashCode() ^ (int) System.currentTimeMillis()
210 ^ hostIpHash;
214 * Get the string representation of this UID.
216 * @return a string, uniquely identifying this id.
218 public String toString()
220 int max = Character.MAX_RADIX;
221 // Translate into object count, counting from 0.
222 long lc = (count + Short.MIN_VALUE) & 0xFFFF;
223 return Long.toString(time, max) + ":"
224 + Long.toString(unique, max) + ":"
225 + Long.toString(lc, max);