1 3 package org.jgroups.blocks; 4 5 6 7 import org.jgroups.Address; 8 9 import java.io.Externalizable ; 10 import java.io.IOException ; 11 import java.io.ObjectInput ; 12 import java.io.ObjectOutput ; 13 14 15 16 17 36 public class Xid implements Externalizable { 37 protected Address creator=null; 38 protected long id=0; 39 protected int mode=DIRTY_READS; 40 protected static transient long next_id=0; 41 public static final String XID="xid"; 42 43 48 public static final int DIRTY_READS = 1; 49 50 54 public static final int READ_COMMITTED = 2; 55 56 60 public static final int REPEATABLE_READ = 3; 61 62 66 public static final int SERIALIZABLE = 4; 67 68 69 public Xid() { 70 ; } 72 73 private Xid(Address creator, long id) { 74 this.creator=creator; this.id=id; 75 } 76 77 private Xid(Address creator, long id, int mode) { 78 this.creator=creator; this.id=id; this.mode=mode; 79 } 80 81 public Address getCreator() {return creator;} 82 public long getId() {return id;} 83 public long getMode() {return mode;} 84 85 86 public static Xid create(Address creator) throws Exception { 87 if(creator == null) 88 throw new Exception ("Xid.create(): creator == null"); 89 synchronized(Xid.class) { 90 return new Xid(creator, ++next_id); 91 } 92 } 93 94 public static Xid create(Address creator, int mode) throws Exception { 95 if(creator == null) 96 throw new Exception ("Xid.create(): creator == null"); 97 synchronized(Xid.class) { 98 return new Xid(creator, ++next_id, mode); 99 } 100 } 101 102 public static String modeToString(int m) { 103 switch(m) { 104 case DIRTY_READS: return "DIRTY_READS"; 105 case READ_COMMITTED: return "READ_COMMITTED"; 106 case REPEATABLE_READ: return "REPEATABLE_READ"; 107 case SERIALIZABLE: return "SERIALIZABLE"; 108 default: return "<unknown>"; 109 } 110 } 111 112 public boolean equals(Object other) { 113 return compareTo(other) == 0; 114 } 115 116 public int hashCode() { 117 return creator.hashCode() + (int)id; 118 } 119 120 public int compareTo(Object o) { 121 Xid other; 122 int comp; 123 if(o == null || !(o instanceof Xid)) 124 throw new ClassCastException ("Xid.compareTo(): comparison between different classes"); 125 other=(Xid)o; 126 comp=creator.compareTo(other.getCreator()); 127 if(comp != 0) return comp; 128 if(id < other.getId()) return -1; 129 if(id > other.getId()) return 1; 130 return 0; 131 } 132 133 public String toString() { 134 StringBuffer sb=new StringBuffer (); 135 sb.append('<').append(creator).append(">:").append(id); 136 return sb.toString(); 137 } 138 139 140 public void writeExternal(ObjectOutput out) throws IOException { 141 out.writeObject(creator); 142 out.writeLong(id); 143 out.writeInt(mode); 144 } 145 146 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException { 147 creator=(Address)in.readObject(); 148 id=in.readLong(); 149 mode=in.readInt(); 150 } 151 152 153 154 } 155 | Popular Tags |