package org.jboss.mq;
import java.io.Serializable;
import java.util.Arrays;
import javax.transaction.xa.Xid;
public class JBossMQXid implements Serializable, Xid
{
private static final long serialVersionUID = -2227021688745286343L;
private int formatId;
private byte[] globalTransactionId;
private byte[] branchQualifier;
private transient String cachedToString;
private transient int cachedHashCode;
public JBossMQXid(Xid xid)
{
formatId = xid.getFormatId();
globalTransactionId = xid.getGlobalTransactionId();
branchQualifier = xid.getBranchQualifier();
}
public int getFormatId()
{
return formatId;
}
public byte[] getGlobalTransactionId()
{
return globalTransactionId;
}
public byte[] getBranchQualifier()
{
return branchQualifier;
}
public boolean equals(Object object)
{
if (object == null || (object instanceof Xid) == false)
return false;
Xid other = (Xid) object;
return
(
formatId == other.getFormatId() &&
Arrays.equals(globalTransactionId, other.getGlobalTransactionId()) &&
Arrays.equals(branchQualifier, other.getBranchQualifier())
);
}
public int hashCode()
{
if (cachedHashCode == 0)
{
cachedHashCode = formatId;
for (int i = 0; i < globalTransactionId.length; ++i)
cachedHashCode += globalTransactionId[i];
}
return cachedHashCode;
}
public String toString()
{
if (cachedToString == null)
{
StringBuffer buffer = new StringBuffer();
buffer.append("JBossMQXid[FormatId=").append(getFormatId());
buffer.append(" GlobalId=").append(new String(getGlobalTransactionId()).trim());
buffer.append(" BranchQual=").append(new String(getBranchQualifier()).trim());
buffer.append(']');
cachedToString = buffer.toString();
}
return cachedToString;
}
}