1   /*** 
2    * 
3    * Copyright 2004 Protique Ltd
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * You may obtain a copy of the License at 
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License. 
16   * 
17   **/
18  package org.codehaus.activemq.message;
19  
20  import javax.transaction.xa.Xid;
21  
22  /***
23   * @version $Revision: 1.1 $
24   */
25  public class XidStub implements Xid {
26  
27      private byte[] id;
28  
29      public XidStub(byte[] id) {
30          this.id = id;
31      }
32  
33      public int getFormatId() {
34          return 77;
35      }
36  
37      public byte[] getBranchQualifier() {
38          return id;
39      }
40  
41      public byte[] getGlobalTransactionId() {
42          return id;
43      }
44  
45      public int hashCode() {
46          int hash = 0;
47          hash = hash(id, hash);
48          return hash;
49      }
50  
51      public String toString() {
52          StringBuffer buffer = new StringBuffer(super.toString());
53          buffer.append("[id=[");
54          for (int i = 0; i < id.length; i++) {
55              if (i > 0) {
56                  buffer.append(", ");
57              }
58              buffer.append(Byte.toString(id[i]));
59          }
60          buffer.append("]");
61          return buffer.toString();
62      }
63  
64      protected int hash(byte[] bytes, int hash) {
65          for (int i = 0, size = bytes.length; i < size; i++) {
66              hash ^= bytes[i];
67          }
68          return hash;
69      }
70  
71      public boolean equals(Object that) {
72          if (this == that) {
73              return true;
74          }
75          if (hashCode() == that.hashCode() && that instanceof XidStub) {
76              return equals((XidStub) that);
77          }
78          return false;
79      }
80  
81      public boolean equals(XidStub that) {
82          if (this.id.length == that.id.length) {
83              for (int i = 0; i < id.length; i++) {
84                  if (this.id[i] != that.id[i]) {
85                      return false;
86                  }
87              }
88              return true;
89          }
90          return false;
91      }
92  }