001: /*
002:
003: Derby - Class org.apache.derby.iapi.store.access.GlobalXact
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.iapi.store.access;
023:
024: /**
025:
026: This abstract class represents a global transaction id which can be tested
027: for equality against other transaction ids, which can be hashed into a
028: hash table, and which can be output as a string.
029: <P>
030: This class has 2 direct subclasses.
031: <UL>
032: <LI> org.apache.derby.iapi.store.access.xa.XAXactId :
033: this class is a specific implementation of the JTA Xid interface
034: <LI> org.apache.derby.impl.store.access.GlobalXactId :
035: this class represents internal cloudscape transaction ids
036: </UL>
037: <P>
038: The main reason for this class is to ensure that equality etc. works in a
039: consistent way across both subclasses.
040: **/
041:
042: public abstract class GlobalXact {
043:
044: /**************************************************************************
045: * Protected Fields of the class
046: **************************************************************************
047: */
048: protected int format_id;
049: protected byte[] global_id;
050: protected byte[] branch_id;
051:
052: public boolean equals(Object other) {
053: if (other == this )
054: return true;
055:
056: if (other instanceof GlobalXact) {
057:
058: GlobalXact other_xact = (GlobalXact) other;
059:
060: return (java.util.Arrays.equals(other_xact.global_id,
061: this .global_id)
062: && java.util.Arrays.equals(other_xact.branch_id,
063: this .branch_id) && other_xact.format_id == this .format_id);
064:
065: }
066:
067: return false;
068: }
069:
070: public String toString() {
071: String globalhex = "";
072: String branchhex = "";
073: if (global_id != null) {
074: int mask = 0;
075: for (int i = 0; i < global_id.length; i++) {
076: mask = (global_id[i] & 0xFF);
077: globalhex += Integer.toHexString(mask);
078: }
079: }
080:
081: if (branch_id != null) {
082: int mask = 0;
083: for (int i = 0; i < branch_id.length; i++) {
084: mask = (branch_id[i] & 0xFF);
085: branchhex += Integer.toHexString(mask);
086: }
087: }
088:
089: return ("(" + format_id + "," + globalhex + "," + branchhex + ")");
090:
091: }
092:
093: /**
094: Provide a hashCode which is compatable with the equals() method.
095:
096: @see java.lang.Object#hashCode
097: **/
098: public int hashCode() {
099: // make sure hash does not overflow int, the only unknown is
100: // format_id. Lop off top bits.
101: int hash = global_id.length + branch_id.length
102: + (format_id & 0xFFFFFFF);
103:
104: for (int i = 0; i < global_id.length; i++) {
105: hash += global_id[i];
106: }
107: for (int i = 0; i < branch_id.length; i++) {
108: hash += branch_id[i];
109: }
110:
111: return (hash);
112: }
113:
114: }
|