1 22 package org.jboss.tm; 23 24 32 public class LocalId 33 implements java.io.Externalizable 34 { 35 static final long serialVersionUID = 2076780468014328911L; 36 37 41 private long value; 42 43 44 46 47 private static final int TX_GEN_SIZE = 20; 48 49 50 private static final int LONG_SIZE = 64; 51 52 53 private static final int TX_NUM_SIZE = LONG_SIZE - TX_GEN_SIZE; 54 55 56 private static final long TX_GEN_MASK = (1L << TX_GEN_SIZE) - 1; 57 58 59 private static final long TX_NUM_MASK = (1L << TX_NUM_SIZE) - 1; 60 61 71 public static long assemble(int genNumber, long txNumber) 72 { 73 return (((long) genNumber) << TX_NUM_SIZE) | (txNumber & TX_NUM_MASK); 74 } 75 76 83 public static int toGenerationNumber(long localIdValue) 84 { 85 return (int)((localIdValue >> TX_NUM_SIZE) & TX_GEN_MASK); 86 } 87 88 95 public static long toTransactionNumber(long localIdValue) 96 { 97 return localIdValue & TX_NUM_MASK; 98 } 99 100 108 public static String toString(long l) 109 { 110 return "" + toGenerationNumber(l) + ":" + toTransactionNumber(l); 111 } 112 113 121 public static void toByteArray(long localIdValue, byte[] dst, int dstBegin) 122 { 123 dst[dstBegin + 0] = (byte)(0xff & (localIdValue >>> 56)); 124 dst[dstBegin + 1] = (byte)(0xff & (localIdValue >>> 48)); 125 dst[dstBegin + 2] = (byte)(0xff & (localIdValue >>> 40)); 126 dst[dstBegin + 3] = (byte)(0xff & (localIdValue >>> 32)); 127 dst[dstBegin + 4] = (byte)(0xff & (localIdValue >>> 24)); 128 dst[dstBegin + 5] = (byte)(0xff & (localIdValue >>> 16)); 129 dst[dstBegin + 6] = (byte)(0xff & (localIdValue >>> 8)); 130 dst[dstBegin + 7] = (byte)(0xff & (localIdValue >>> 0)); 131 } 132 133 141 public static long fromByteArray(byte[] src, int srcBegin) 142 { 143 return ((long)(src[srcBegin + 0] & 0xff) << 56) 144 | ((long)(src[srcBegin + 1] & 0xff) << 48) 145 | ((long)(src[srcBegin + 2] & 0xff) << 40) 146 | ((long)(src[srcBegin + 3] & 0xff) << 32) 147 | ((long)(src[srcBegin + 4] & 0xff) << 24) 148 | ((long)(src[srcBegin + 5] & 0xff) << 16) 149 | ((long)(src[srcBegin + 6] & 0xff) << 8) 150 | ((long)(src[srcBegin + 7] & 0xff)); 151 } 152 153 155 158 public LocalId() 159 { 160 } 161 162 166 public LocalId(long value) 167 { 168 this.value = value; 169 } 170 171 public LocalId(XidImpl xid) 172 { 173 this(xid.getLocalIdValue()); 174 } 175 176 178 public long getValue() 179 { 180 return value; 181 } 182 183 186 public boolean equals(Object obj) 187 { 188 return (obj instanceof LocalId) ? (value == ((LocalId)obj).value) 189 : false; 190 } 191 192 public int hashCode() 193 { 194 return (int)value; 195 } 196 197 public void writeExternal(java.io.ObjectOutput out) 199 throws java.io.IOException 200 { 201 out.writeLong(value); 202 } 203 204 public void readExternal(java.io.ObjectInput in) 205 throws java.io.IOException , ClassNotFoundException 206 { 207 value = in.readLong(); 208 } 209 210 } 211 212 | Popular Tags |