Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / java / rmi / server / ObjID.java
blob1aaa223602c867be859d7e3064c7ff640b576a83
1 /* ObjID.java -- Unique object id with respect to the given host.
2 Copyright (c) 1996, 1997, 1998, 1999, 2004, 2006
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301 USA.
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
25 combination.
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
40 package java.rmi.server;
42 import java.io.DataInput;
43 import java.io.DataOutput;
44 import java.io.IOException;
45 import java.io.ObjectInput;
46 import java.io.ObjectOutput;
47 import java.io.Serializable;
49 /**
50 * Represents the object identifier, unique for the host that generated it.
51 * The ObjID contains inside the integer object identifier that, if needed,
52 * may indicated that this is a reference to one of the well known objects
53 * on that host (registry, activator or dgc) and the {@link UID} that
54 * ensures uniqueness.
56 public final class ObjID
57 implements Serializable
60 /**
61 * Use serial version uid for interoperability.
63 static final long serialVersionUID = - 6386392263968365220L;
65 /**
66 * The object counter, which value is assigned when creating the ordinary
67 * objects without the known object id. The counter is incremented each time
68 * the new ObjID is constructed.
70 private static long next = 0x8000000000000000L;
72 /**
73 * The object to put the lock on when incrementing {@link #next}
75 private static final Object lock = ObjID.class;
77 /**
78 * Defines the ID of the naming service.
80 public static final int REGISTRY_ID = 0;
82 /**
83 * Defines the ID of the activator.
85 public static final int ACTIVATOR_ID = 1;
87 /**
88 * Defines the ID of the distributed garbage collector.
90 public static final int DGC_ID = 2;
92 /**
93 * The object Id (either well-known value or the value of the incrementing
94 * object counter.
96 long objNum;
98 /**
99 * The object unique identifier, generated individually for each object.
101 UID space;
104 * Create the new object id, unique for this host.
106 public ObjID()
108 synchronized (lock)
110 objNum = next++;
112 space = new UID();
116 * Create the new object id defining the well known remotely accessible
117 * object, present in this host. The well - known objects are:
118 * <ul>
119 * <li>{@link #REGISTRY_ID} - RMI naming service.</li>
120 * <li>{@link #ACTIVATOR_ID} - activator</li>
121 * <li>{@link #DGC_ID} - distributed garbage collector (grants lease
122 * durations to keep the object before it is garbage collected.</li>
123 * </ul>
125 * @param id the well known object id, one of the above.
127 public ObjID(int id)
129 objNum = (long) id;
130 space = new UID((short) 0);
134 * Write object id as long, then the object {@link UID}.
136 public void write(ObjectOutput out) throws IOException
138 DataOutput dout = (DataOutput) out;
139 dout.writeLong(objNum);
140 space.write(dout);
144 * Read object id (as long), then the object {@link UID}.
146 public static ObjID read(ObjectInput in) throws IOException
148 DataInput din = (DataInput) in;
149 ObjID id = new ObjID();
150 id.objNum = din.readLong();
151 id.space = UID.read(din);
152 return (id);
156 * Get the hashcode.
158 public int hashCode()
160 return space == null ? (int) objNum : space.hashCode() ^ (int) objNum;
164 * Compare for equality.
166 public boolean equals(Object obj)
168 if (obj instanceof ObjID)
170 ObjID that = (ObjID) obj;
171 return that.objNum == objNum && eq(that.space, space);
173 else
174 return false;
178 * Compare by .equals if both a and b are not null, compare directly if at
179 * least one of them is null.
181 static final boolean eq(Object a, Object b)
183 if (a == null || b == null)
184 return a == b;
185 else
186 return a.equals(b);
190 * Get the string representation.
192 public String toString()
194 return (objNum + ":" + space);