KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > JBossMQXid


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.mq;
23
24 import java.io.Serializable JavaDoc;
25 import java.util.Arrays JavaDoc;
26
27 import javax.transaction.xa.Xid JavaDoc;
28
29 /**
30  * This class is a wrapper for non-serializable implementations of
31  * java.transaction.xa.Xid.
32  *
33  * @author Daniel Bloomfield Ramagem (daniel.ramagem@gmail.com)
34  * @author <a HREF="mailto:adrian@jboss.com">Adrian Brock</a>
35  * @version $Revision: 45317 $
36  */

37 public class JBossMQXid implements Serializable JavaDoc, Xid JavaDoc
38 {
39    /** The serialVersionUID */
40    private static final long serialVersionUID = -2227021688745286343L;
41
42    /** The format id */
43    private int formatId;
44
45    /** The gid */
46    private byte[] globalTransactionId;
47
48    /** The branch */
49    private byte[] branchQualifier;
50
51    /** Cached toString() */
52    private transient String JavaDoc cachedToString;
53
54    /** Cached hashCode() */
55    private transient int cachedHashCode;
56
57    /**
58     * Create a new wrapper Xid
59     *
60     * @param xid the wrapped xid
61     */

62    public JBossMQXid(Xid JavaDoc xid)
63    {
64       formatId = xid.getFormatId();
65       globalTransactionId = xid.getGlobalTransactionId();
66       branchQualifier = xid.getBranchQualifier();
67    }
68
69    public int getFormatId()
70    {
71       return formatId;
72    }
73
74    public byte[] getGlobalTransactionId()
75    {
76       return globalTransactionId;
77    }
78
79    public byte[] getBranchQualifier()
80    {
81       return branchQualifier;
82    }
83
84    public boolean equals(Object JavaDoc object)
85    {
86       if (object == null || (object instanceof Xid JavaDoc) == false)
87          return false;
88
89       Xid JavaDoc other = (Xid JavaDoc) object;
90       return
91       (
92          formatId == other.getFormatId() &&
93          Arrays.equals(globalTransactionId, other.getGlobalTransactionId()) &&
94          Arrays.equals(branchQualifier, other.getBranchQualifier())
95       );
96    }
97
98    public int hashCode()
99    {
100       if (cachedHashCode == 0)
101       {
102          cachedHashCode = formatId;
103          for (int i = 0; i < globalTransactionId.length; ++i)
104             cachedHashCode += globalTransactionId[i];
105       }
106       return cachedHashCode;
107    }
108
109    public String JavaDoc toString()
110    {
111       if (cachedToString == null)
112       {
113          StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
114          buffer.append("JBossMQXid[FormatId=").append(getFormatId());
115          buffer.append(" GlobalId=").append(new String JavaDoc(getGlobalTransactionId()).trim());
116          byte[] branchQualifer = getBranchQualifier();
117          buffer.append(" BranchQual=");
118          if (branchQualifer == null)
119             buffer.append("null");
120          else
121             buffer.append(new String JavaDoc(getBranchQualifier()).trim());
122          buffer.append(']');
123          cachedToString = buffer.toString();
124       }
125       return cachedToString;
126    }
127 }
Popular Tags