KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > tm > LocalId


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.tm;
23
24 /**
25  * LocalId is a wrapper for a long value that identifies a transaction within
26  * a JBoss server. This implementation is immutable and serializable at
27  * runtime.
28  *
29  * @author <a HREF="reverbel@ime.usp.br">Francisco Reverbel</a>
30  * @version $Revision: 37459 $
31  */

32 public class LocalId
33    implements java.io.Externalizable JavaDoc
34 {
35    static final long serialVersionUID = 2076780468014328911L;
36    
37    /**
38     * Long value that identifies a transaction within a JBoss server.
39     * This is really a sequence number generated by the XidFactory.
40     */

41    private long value;
42
43
44    // Static --------------------------------------------------------
45

46    /** Number of bits in the "transaction generation" part of the local id */
47    private static final int TX_GEN_SIZE = 20;
48    
49    /** Number of bits in a long */
50    private static final int LONG_SIZE = 64;
51    
52    /** Number of bits in the "transaction number" part of the local id */
53    private static final int TX_NUM_SIZE = LONG_SIZE - TX_GEN_SIZE;
54    
55    /** Mask to extract a transaction generation from the lower part of a long */
56    private static final long TX_GEN_MASK = (1L << TX_GEN_SIZE) - 1;
57    
58    /** Mask to extract the transaction number from a local id */
59    private static final long TX_NUM_MASK = (1L << TX_NUM_SIZE) - 1;
60    
61    /**
62     * Assemble a local id as a long value with a generation number in its high
63     * part (the <code>TX_GEN_SIZE</code> most significant bits) and a
64     * transaction number in its low part (the <code>TX_NUM_SIZE</code> least
65     * significant bits).
66     *
67     * @param genNumber the generation number
68     * @param txNumber the transaction number.
69     * @return a local transaction id.
70     */

71    public static long assemble(int genNumber, long txNumber)
72    {
73       return (((long) genNumber) << TX_NUM_SIZE) | (txNumber & TX_NUM_MASK);
74    }
75    
76    /**
77     * Extracts the generation number from a local id value.
78     *
79     * @param localIdValue a local id value.
80     * @return the generation number in the <code>TX_GEN_SIZE</code> most
81     * significant bits of the local id.
82     */

83    public static int toGenerationNumber(long localIdValue)
84    {
85       return (int)((localIdValue >> TX_NUM_SIZE) & TX_GEN_MASK);
86    }
87
88    /**
89     * Extracts a transaction number from a local id value.
90     *
91     * @param localIdValue a local id value.
92     * @return the transaction number in the <code>TX_NUM_SIZE</code> least
93     * significant bits of the local id.
94     */

95    public static long toTransactionNumber(long localIdValue)
96    {
97       return localIdValue & TX_NUM_MASK;
98    }
99    
100    /**
101     * Converts a local id value into a string.
102     *
103     * @param l a local id value
104     * @return a string of the form "<em>gn</em>:<em>tn</em>", where
105     * <em>gn</em> is the generation number and <em>tn</em> is
106     * the transaction number.
107     */

108    public static String JavaDoc toString(long l)
109    {
110       return "" + toGenerationNumber(l) + ":" + toTransactionNumber(l);
111    }
112
113    /**
114     * Copies a local id value into a byte array.
115     *
116     * @param localIdValue a local id value
117     * @param dst the destination byte array
118     * @param dstBegin the index of the first element of <code>dst</code> that
119     * will receive a byte of the local id.
120     */

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    /**
134     * Gets a local id value from a byte array/
135     *
136     * @param src the source byte array
137     * @param srcBegin the index of the first element of <code>src</code> that
138     * contains a byte of the local id.
139     * @return a local id extracted from the byte array.
140     */

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    // Constructors --------------------------------------------------
154

155    /**
156     * No-arg constructor for Externalizable support.
157     */

158    public LocalId()
159    {
160    }
161    
162    /**
163     * Create a new instance. This constructor is public <em>only</em>
164     * to get around a class loader problem; it should be package-private.
165     */

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    // Public --------------------------------------------------------
177

178    public long getValue()
179    {
180       return value;
181    }
182
183    /**
184     * Compare for equality.
185     */

186    public boolean equals(Object JavaDoc 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    // Externalizable implementation ---------------------------------
198
public void writeExternal(java.io.ObjectOutput JavaDoc out)
199       throws java.io.IOException JavaDoc
200    {
201       out.writeLong(value);
202    }
203    
204    public void readExternal(java.io.ObjectInput JavaDoc in)
205       throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc
206    {
207       value = in.readLong();
208    }
209
210 }
211
212
Popular Tags