KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > rmi > server > OID


1 package org.sapia.ubik.rmi.server;
2
3 import java.io.Externalizable JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.ObjectInput JavaDoc;
6 import java.io.ObjectOutput JavaDoc;
7
8
9 /**
10  * This class models a unique remote object identifier. An instance of this
11  * class uniquely identifies a remote object, even among multiple VMs.
12  *
13  * @author Yanick Duchesne
14  * <dl>
15  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
16  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
17  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
18  * </dl>
19  */

20 public class OID implements Externalizable JavaDoc, Comparable JavaDoc {
21   static final long serialVersionUID = 1L;
22   private static final Object JavaDoc _unique = new Object JavaDoc();
23   private long _id;
24   private int _hashCode;
25   private String JavaDoc _codebase = System.getProperty("java.rmi.server.codebase");
26
27   /** Do not call; used for externalization only. */
28   public OID() {
29   }
30
31   /**
32    * Creates an instance of this class with the given identifier string.
33    */

34   public OID(long id) {
35     _id = id;
36     _hashCode = (int) (id ^ (id >>> 32)) ^ _unique.hashCode();
37   }
38
39   /**
40    * Creates a hashcode with this instance's internal ID string.
41    *
42    * @return a hashcode
43    */

44   public int hashCode() {
45     return _hashCode;
46   }
47
48   /**
49    * @return <code>true</code> if the object passed in is another object identifier
50    * representing the same remote reference has this.
51    */

52   public boolean equals(Object JavaDoc o) {
53     try {
54       return (((OID) o)._id == _id) && (_hashCode == o.hashCode());
55     } catch (ClassCastException JavaDoc e) {
56       return false;
57     }
58   }
59
60   /**
61    * @see java.io.Externalizable#readExternal(ObjectInput)
62    */

63   public void readExternal(ObjectInput JavaDoc in)
64     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
65     _id = in.readLong();
66     _hashCode = in.readInt();
67     _codebase =(String JavaDoc)in.readObject();
68   }
69
70   /**
71    * @see java.io.Externalizable#writeExternal(ObjectOutput)
72    */

73   public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
74     out.writeLong(_id);
75     out.writeInt(_hashCode);
76     out.writeObject(_codebase);
77   }
78   
79   /**
80    * @return the remote codebase of the object to which this instance corresponds,
81    * or <code>null</code> if no such codebase has been set.
82    */

83   public String JavaDoc getCodebase(){
84     return _codebase;
85   }
86
87   public String JavaDoc toString() {
88     return new StringBuffer JavaDoc().append("[id=").append(_id).append(", hashCode=")
89                              .append(_hashCode).append("]").toString();
90   }
91
92   public int compareTo(Object JavaDoc other) {
93     try {
94       long diff = _id - ((OID) other)._id;
95
96       if (diff < 0) {
97         return -1;
98       } else if (diff == 0) {
99         return _hashCode - other.hashCode();
100       } else {
101         return 1;
102       }
103     } catch (ClassCastException JavaDoc e) {
104       return -1;
105     }
106   }
107 }
108
Popular Tags