KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > transaction > util > xa > XidWrapper


1 /*
2  * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//transaction/src/java/org/apache/commons/transaction/util/xa/XidWrapper.java,v 1.3 2005/01/05 18:56:16 ozeigermann Exp $
3 <<<<<<< .mine
4  * $Revision: 1.3 $
5  * $Date: 2005-02-26 14:16:14 +0100 (Sa, 26 Feb 2005) $
6 =======
7  * $Revision$
8  * $Date: 2005-02-26 14:16:14 +0100 (Sa, 26 Feb 2005) $
9 >>>>>>> .r168169
10  *
11  * ====================================================================
12  *
13  * Copyright 1999-2002 The Apache Software Foundation
14  *
15  * Licensed under the Apache License, Version 2.0 (the "License");
16  * you may not use this file except in compliance with the License.
17  * You may obtain a copy of the License at
18  *
19  * http://www.apache.org/licenses/LICENSE-2.0
20  *
21  * Unless required by applicable law or agreed to in writing, software
22  * distributed under the License is distributed on an "AS IS" BASIS,
23  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24  * See the License for the specific language governing permissions and
25  * limitations under the License.
26  *
27  */

28
29 package org.apache.commons.transaction.util.xa;
30
31 import javax.transaction.xa.Xid JavaDoc;
32 import java.lang.Object JavaDoc;
33 //import java.util.Arrays;
34
import java.lang.String JavaDoc;
35
36 /**
37  * Wraps an <code>Xid</code> to guarantee methods for equality and hashcode are
38  * implemented correctly. This is escpecially necessary when the <code>Xid</code> is used as a key in a <code>HashMap</code>.
39  *
40  * @version $Revision$
41  *
42  */

43 public class XidWrapper implements Xid JavaDoc {
44
45     public static final Xid JavaDoc wrap(Xid JavaDoc xid) {
46         return wrap(xid, false); // for Slide branch qualifier must not be included in onePhase commit
47
}
48
49     public static final Xid JavaDoc wrap(Xid JavaDoc xid, boolean includeBranch) {
50         return (xid instanceof XidWrapper ? xid : new XidWrapper(xid, includeBranch));
51     }
52
53     private final Xid JavaDoc xid;
54     private final String JavaDoc asString;
55     private final int hashCode;
56
57     private XidWrapper(Xid JavaDoc xid, boolean includeBranch) {
58         this.xid = xid;
59         // do calculations once for performance
60
StringBuffer JavaDoc b = new StringBuffer JavaDoc(64);
61         b.append(new String JavaDoc(xid.getGlobalTransactionId()));
62         if (includeBranch) {
63             b.append("-").append(new String JavaDoc(xid.getBranchQualifier()));
64         }
65
66         asString = b.toString();
67         hashCode = asString.hashCode();
68     }
69
70     public Xid JavaDoc getXid() {
71         return xid;
72     }
73
74     public int getFormatId() {
75         return xid.getFormatId();
76     }
77
78     public byte[] getGlobalTransactionId() {
79         return xid.getGlobalTransactionId();
80     }
81
82     public byte[] getBranchQualifier() {
83         return xid.getBranchQualifier();
84     }
85
86     public boolean equals(Object JavaDoc o) {
87         return (o != null && asString.equals(o.toString()));
88         /*
89          if (this == o) {
90              return true;
91          }
92         
93          if (o != null && o instanceof Xid) {
94              Xid xid2 = (Xid) o;
95              // we do not need equality of format Id
96              return (
97                  Arrays.equals(xid.getGlobalTransactionId(), xid2.getGlobalTransactionId())
98                      && Arrays.equals(xid.getBranchQualifier(), xid2.getBranchQualifier()));
99          }
100         
101          return false;
102          */

103     }
104
105     public String JavaDoc toString() {
106         return asString;
107     }
108
109     public int hashCode() {
110         return hashCode;
111     }
112 }
113
Popular Tags