KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > store > access > GlobalXact


1 /*
2
3    Derby - Class org.apache.derby.iapi.store.access.GlobalXact
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.iapi.store.access;
23
24
25 /**
26
27 This abstract class represents a global transaction id which can be tested
28 for equality against other transaction ids, which can be hashed into a
29 hash table, and which can be output as a string.
30 <P>
31 This class has 2 direct subclasses.
32 <UL>
33 <LI> org.apache.derby.iapi.store.access.xa.XAXactId :
34 this class is a specific implementation of the JTA Xid interface
35 <LI> org.apache.derby.impl.store.access.GlobalXactId :
36 this class represents internal cloudscape transaction ids
37 </UL>
38 <P>
39 The main reason for this class is to ensure that equality etc. works in a
40 consistent way across both subclasses.
41 **/

42
43 public abstract class GlobalXact {
44     
45     /**************************************************************************
46      * Protected Fields of the class
47      **************************************************************************
48      */

49     protected int format_id;
50     protected byte[] global_id;
51     protected byte[] branch_id;
52
53     public boolean equals(Object JavaDoc other)
54     {
55         if (other == this)
56             return true;
57
58         if (other instanceof GlobalXact) {
59     
60             GlobalXact other_xact = (GlobalXact) other;
61         
62             return(
63                    java.util.Arrays.equals(
64                                     other_xact.global_id,
65                                     this.global_id) &&
66                    java.util.Arrays.equals(
67                                     other_xact.branch_id,
68                                     this.branch_id) &&
69                    other_xact.format_id == this.format_id);
70         
71         }
72
73         return false;
74     }
75
76     public String JavaDoc toString()
77     {
78         String JavaDoc globalhex = "";
79         String JavaDoc branchhex = "";
80         if (global_id != null)
81         {
82             int mask = 0;
83             for (int i = 0; i < global_id.length; i++)
84             {
85                 mask = (global_id[i] & 0xFF);
86                 globalhex += Integer.toHexString(mask);
87             }
88         }
89     
90         if (branch_id != null)
91         {
92             int mask = 0;
93             for (int i = 0; i < branch_id.length; i++)
94             {
95                 mask = (branch_id[i] & 0xFF);
96                 branchhex += Integer.toHexString(mask);
97             }
98         }
99
100         return("(" + format_id + "," + globalhex + "," + branchhex + ")");
101     
102     }
103
104
105     /**
106        Provide a hashCode which is compatable with the equals() method.
107        
108        @see java.lang.Object#hashCode
109     **/

110     public int hashCode()
111     {
112         // make sure hash does not overflow int, the only unknown is
113
// format_id. Lop off top bits.
114
int hash = global_id.length + branch_id.length + (format_id & 0xFFFFFFF);
115
116         for (int i = 0; i < global_id.length; i++)
117         {
118             hash += global_id[i];
119         }
120         for (int i = 0; i < branch_id.length; i++)
121         {
122             hash += branch_id[i];
123         }
124     
125         return(hash);
126     }
127     
128 }
129
130
131
132
Popular Tags