1 24 25 package org.objectweb.dream.protocol.causality; 26 27 import java.io.Externalizable ; 28 import java.io.IOException ; 29 import java.io.ObjectInput ; 30 import java.io.ObjectOutput ; 31 32 35 public class Update implements Externalizable 36 { 37 38 39 private transient short l; 40 41 42 private transient short c; 43 44 45 private transient int stamp; 46 47 48 private transient Update next; 49 50 53 public Update() 54 { 55 } 56 57 64 public Update(short l, short c, int stamp) 65 { 66 super(); 67 this.l = l; 68 this.c = c; 69 this.stamp = stamp; 70 } 71 72 80 public Update(short l, short c, int s, Update list) 81 { 82 this.l = l; 83 this.c = c; 84 this.stamp = s; 85 if (list != null) 86 { 87 this.next = list.next; 88 list.next = this; 89 } 90 else 91 { 92 this.next = null; 93 } 94 } 95 96 99 public short getLine() 100 { 101 return l; 102 } 103 104 107 public short getColumn() 108 { 109 return c; 110 } 111 112 115 public int getStamp() 116 { 117 return stamp; 118 } 119 120 123 public Update getNext() 124 { 125 return next; 126 } 127 128 131 public String toString() 132 { 133 StringBuffer sb = new StringBuffer (); 134 sb.append('('); 135 Update u = this; 136 while (u != null) 137 { 138 u.toStringBuffer(sb); 139 u = u.next; 140 } 141 sb.append(')'); 142 return sb.toString(); 143 } 144 145 private StringBuffer toStringBuffer(StringBuffer sb) 146 { 147 sb.append('('); 148 sb.append(l).append(", "); 149 sb.append(c).append(", "); 150 sb.append(stamp).append(')'); 151 return sb; 152 } 153 154 157 public void readExternal(ObjectInput in) throws IOException , 158 ClassNotFoundException 159 { 160 this.l = in.readShort(); 161 this.c = in.readShort(); 162 this.stamp = in.readInt(); 163 164 short l; 165 while ((l = in.readShort()) != -1) 166 { 167 alloc(l, in.readShort(), in.readInt(), this); 168 } 169 } 170 171 174 public void writeExternal(ObjectOutput out) throws IOException 175 { 176 Update update = this; 177 while (update != null) 178 { 179 out.writeShort(update.l); 180 out.writeShort(update.c); 181 out.writeInt(update.stamp); 182 Update nextUpdate = update.next; 183 unalloc(update); 184 update = nextUpdate; 185 } 186 out.writeShort(-1); 187 } 188 189 192 public static Update readUpdate(ObjectInput in) throws IOException , 193 ClassNotFoundException 194 { 195 Update u = alloc((short) 0, (short) 0, 0, null); 196 u.readExternal(in); 197 return u; 198 } 199 200 private static Update[] pool = new Update[30]; 201 202 private static int elementCount; 203 204 static synchronized Update alloc(short l, short c, int s, Update list) 205 { 206 if (elementCount == 0) 207 { 208 return new Update(l, c, s, list); 209 } 210 elementCount -= 1; 211 Update update = pool[elementCount]; 212 213 update.l = l; 214 update.c = c; 215 update.stamp = s; 216 if (list != null) 217 { 218 update.next = list.next; 219 list.next = update; 220 } 221 else 222 { 223 update.next = null; 224 } 225 226 pool[elementCount] = null; 227 return update; 228 229 } 230 231 static synchronized void unalloc(Update update) 232 { 233 if (elementCount == pool.length) 234 { 235 return; 236 } 237 pool[elementCount] = update; 238 update.next = null; 239 elementCount += 1; 240 } 241 } | Popular Tags |