home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / rmi / server / UID.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  3.9 KB  |  147 lines

  1. /*
  2.  * @(#)UID.java    1.7 98/03/18
  3.  *
  4.  * Copyright 1996, 1997 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package java.rmi.server;
  15.  
  16. import java.io.*;
  17.  
  18. /**
  19.  * Abstraction for creating identifiers that are unique with respect
  20.  * to the the host on which it is generated.
  21.  */
  22. public final class UID implements java.io.Serializable {
  23.     private int unique;
  24.     private long time;
  25.     private short count;
  26.  
  27.     /* indicate compatibility with JDK 1.1.x version of class */
  28.     private static final long serialVersionUID = 1086053664494604050L;
  29.  
  30.     /**
  31.      * In the absence of an actual pid, just do something somewhat
  32.      * random.
  33.      */
  34.     private static int getHostUniqueNum() {
  35.     return (new Object()).hashCode();
  36.     }
  37.     
  38.     private static int hostUnique = getHostUniqueNum();
  39.     private static long lastTime = System.currentTimeMillis();
  40.     private static short lastCount = Short.MIN_VALUE;
  41.     private static Object mutex = new Object();
  42.     private static long  ONE_SECOND = 1000; // in milliseconds
  43.     
  44.     /**
  45.      * Create a pure identifier that is unique with respect to the
  46.      * host on which it is generated.  This UID is unique under the
  47.      * following conditions: a) the machine takes more than one second
  48.      * to reboot, and b) the machine's clock is never set backward.
  49.      * In order to construct a UID that is globally unique, simply
  50.      * pair a UID with an InetAddress.
  51.      */
  52.     public UID() {
  53.     
  54.     synchronized (mutex) {
  55.         if (lastCount == Short.MAX_VALUE) {
  56.         boolean done = false;
  57.         while (!done) {
  58.             time = System.currentTimeMillis();
  59.             if (time < lastTime+ONE_SECOND) {
  60.             // pause for a second to wait for time to change
  61.             try {
  62.                 Thread.currentThread().sleep(ONE_SECOND);
  63.             } catch (java.lang.InterruptedException e) {
  64.             }    // ignore exception
  65.             continue;
  66.             } else {
  67.             lastTime = time;
  68.             lastCount = Short.MIN_VALUE;
  69.             done = true;
  70.             }
  71.         }
  72.         } else {
  73.         time = lastTime;
  74.         }
  75.         unique = hostUnique;
  76.         count = lastCount++;
  77.     }
  78.     }
  79.  
  80.     /**
  81.      * Create a "well-known" ID.  There are 2^16 -1 such possible
  82.      * well-known ids.  An id generated via this constructor will not
  83.      * clash with any id generated via the default UID
  84.      * constructor which will generates a genuinely unique identifier
  85.      * with respect to this host.
  86.      */
  87.     public UID(short num) 
  88.     {
  89.     unique = 0;
  90.     time = 0;
  91.     count = num;
  92.     }
  93.  
  94.     private UID(int unique, long time, short count) 
  95.     {
  96.     this.unique = unique;
  97.     this.time = time;
  98.     this.count = count;
  99.     }
  100.  
  101.     public int hashCode() {
  102.     return (int)time + (int)count;
  103.     }
  104.  
  105.     public boolean equals(Object obj) {
  106.     if ((obj != null) && (obj instanceof UID)) {
  107.         UID uid = (UID)obj;
  108.         return (unique == uid.unique &&
  109.             count == uid.count &&
  110.             time == uid.time);
  111.     } else {
  112.         return false;
  113.     }
  114.     }
  115.     
  116.     public String toString() {
  117.     return Integer.toString(unique,16) + ":" +
  118.         Long.toString(time,16) + ":" +
  119.         Integer.toString(count,16);
  120.     }
  121.     
  122.     /**
  123.      * Write uid to output stream.
  124.      */
  125.     public void write(DataOutput out) throws java.io.IOException
  126.     {
  127.     out.writeInt(unique);
  128.     out.writeLong(time);
  129.     out.writeShort(count);
  130.     }
  131.     
  132.     /**
  133.      * Get the uid from the input stream.
  134.      * @param in the input stream
  135.      * @exception IOException If uid could not be read
  136.      * (due to stream failure or malformed uid)
  137.      */
  138.     public static UID read(DataInput in)
  139.     throws java.io.IOException
  140.     {
  141.     int unique = in.readInt();
  142.     long time = in.readLong();
  143.     short count = in.readShort();
  144.     return new UID(unique, time, count);
  145.     }
  146. }
  147.