1 package org.sapia.ubik.rmi.server; 2 3 import java.io.IOException ; 4 import java.io.ObjectInput ; 5 import java.io.ObjectOutput ; 6 import java.net.InetAddress ; 7 8 9 19 public class VmId implements java.io.Externalizable { 20 public static final long serialVersionUID = 1L; 21 private static VmId _instance; 22 private static Object _unique = new Object (); 23 24 static { 25 try { 26 _instance = new VmId(InetAddress.getLocalHost().getHostAddress().hashCode() ^ 27 VmId.class.hashCode() ^ UIDGenerator.createdUID()); 28 } catch (Throwable t) { 29 t.printStackTrace(); 30 new IllegalStateException ("could not initialize VM identifier"); 31 } 32 } 33 34 private long _id; 35 private int _hashCode; 36 37 38 public VmId() { 39 } 40 41 VmId(long id) { 42 _id = id; 43 _hashCode = (int) (_id ^ (_id >>> 32)) ^ _unique.hashCode(); 44 } 45 46 49 public static VmId getInstance() { 50 return _instance; 51 } 52 53 public boolean equals(VmId other) { 54 return other._id == _id && _hashCode == other.hashCode(); 55 } 56 57 60 public boolean equals(Object other) { 61 try { 62 return equals((VmId) other); 63 } catch (ClassCastException e) { 64 return false; 65 } 66 } 67 68 71 public int hashCode() { 72 return _hashCode; 73 } 74 75 78 public void readExternal(ObjectInput in) 79 throws IOException , ClassNotFoundException { 80 _id = in.readLong(); 81 _hashCode = in.readInt(); 82 } 83 84 87 public void writeExternal(ObjectOutput out) throws IOException { 88 out.writeLong(_id); 89 out.writeInt(_hashCode); 90 } 91 92 public String toString() { 93 return "[ id=" + _id + " ]"; 94 } 95 } 96 | Popular Tags |