home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / ObjID.java < prev    next >
Text File  |  1997-05-20  |  4KB  |  133 lines

  1. /*
  2.  * @(#)ObjID.java    1.6 96/11/18
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  */
  21. package java.rmi.server;
  22.  
  23. import java.io.ObjectInput;
  24. import java.io.ObjectOutput;
  25.  
  26. /**
  27.  * The class ObjID is used to identify remote objects uniquely in a VM
  28.  * over time. Each identifier contains an object number and an address
  29.  * space identifier that is unique with respect to a specific host. An
  30.  * object identifier is assigned to a remote object when it is
  31.  * exported.
  32.  */
  33. public final class ObjID implements java.io.Serializable {
  34.     /** well-known id for the registry */
  35.     public static final int REGISTRY_ID = 0;
  36.     /** well-known id for the distributed garbage collector */
  37.     public static final int DGC_ID = 2;
  38.  
  39.     /** object number */
  40.     private long objNum;
  41.     /** address space identifier (unique to host) */
  42.     private UID space;
  43.  
  44.     private static long nextNum = 0;
  45.     private static UID mySpace = new UID();
  46.  
  47.     /**
  48.      * Generate unique object identifier.
  49.      */
  50.     public ObjID () {
  51.     synchronized (mySpace) {
  52.         space = mySpace;
  53.         objNum = nextNum++;
  54.     }
  55.     }
  56.  
  57.     /**
  58.      * Generate a "well-known" object ID.  An object ID generated via
  59.      * this constructor will not clash with any object IDs generated
  60.      * via the default constructor.
  61.      * @param num a unique well-known object number
  62.      */
  63.     public ObjID (int num) {
  64.     space = new UID((short)0);
  65.     objNum = num;
  66.     }
  67.  
  68.     /**
  69.      * Private constructor for creating an object ID given its contents
  70.      * that is read from a stream.
  71.      */
  72.     private ObjID(long objNum, UID space) 
  73.     {
  74.     this.objNum = objNum;
  75.     this.space = space;
  76.     }
  77.     
  78.     /**
  79.      * Marshal object id to output stream.
  80.      */
  81.     public void write(ObjectOutput out) throws java.io.IOException
  82.     {
  83.     out.writeLong(objNum);
  84.     space.write(out);
  85.     }
  86.     
  87.     /**
  88.      * The read method constructs an object id whose contents is read
  89.      * from the specified input stream.
  90.      */
  91.     public static ObjID read(ObjectInput in)
  92.     throws java.io.IOException
  93.     {
  94.     long num = in.readLong();
  95.     UID space = UID.read(in);
  96.     return new ObjID(num, space);
  97.     }
  98.  
  99.     /**
  100.      * The hashcode is the object number.
  101.      */
  102.     public int hashCode() 
  103.     {
  104.     return (int) objNum;
  105.     }
  106.  
  107.     /**
  108.      * Two object identifiers are considered equal if they have the
  109.      * same contents.
  110.      */
  111.     public boolean equals(Object obj) 
  112.     {
  113.     if ((obj != null) && (obj instanceof ObjID)) {
  114.         ObjID id = (ObjID)obj;
  115.         return (objNum == id.objNum && space.equals(id.space));
  116.     } else {
  117.         return false;
  118.     }
  119.     }
  120.  
  121.     /**
  122.      * Returns a string containing the object id representation. The
  123.      * address space identifier is included in the string
  124.      * representation only if the object id is from a non-local
  125.      * address space.
  126.      */
  127.     public String toString()
  128.     {
  129.     return "[" + (space.equals(mySpace) ? "" : space + ", ") + 
  130.         objNum + "]";
  131.     }
  132. }
  133.