1 22 package org.jboss.util.platform; 23 24 import java.io.Serializable ; 25 26 import java.util.Random ; 27 28 37 public class PID 38 implements Serializable , Cloneable 39 { 40 41 protected final int id; 42 43 48 protected PID(final int id) { 49 this.id = id; 50 } 51 52 57 public final int getID() { 58 return id; 59 } 60 61 66 public String toString() { 67 return String.valueOf(id); 68 } 69 70 75 public String toString(int radix) { 76 return Integer.toString(id, radix); 77 } 78 79 84 public int hashCode() { 85 return id; 86 } 87 88 94 public boolean equals(final Object obj) { 95 if (obj == this) return true; 96 97 if (obj != null && obj.getClass() == getClass()) { 98 PID pid = (PID)obj; 99 return pid.id == id; 100 } 101 102 return false; 103 } 104 105 110 public Object clone() { 111 try { 112 return super.clone(); 113 } 114 catch (CloneNotSupportedException e) { 115 throw new InternalError (); 116 } 117 } 118 119 120 124 125 private static PID instance = null; 126 127 132 public synchronized static PID getInstance() { 133 if (instance == null) { 134 instance = create(); 135 } 136 return instance; 137 } 138 139 144 private static PID create() { 145 int random = Math.abs(new Random ().nextInt()); 147 return new PID(random); 148 } 149 } 150 | Popular Tags |