/*
 * JBossMQ, the OpenSource JMS implementation
 * 
 * Distributable under LGPL license. See terms of license at gnu.org.
 */
package org.jboss.mq;

import java.io.Serializable;
import java.util.Arrays;

import javax.transaction.xa.Xid;

/**
 * This class is a wrapper for non-serializable implementations of 
 * java.transaction.xa.Xid.
 *
 * @author Daniel Bloomfield Ramagem (daniel.ramagem@gmail.com) 
 * @author <a href="mailto:adrian@jboss.com">Adrian Brock</a>
 * @version $Revision: 1.1.4.1 $
 */
public class JBossMQXid implements Serializable, Xid
{
   // Constants -----------------------------------------------------

   /** The serialVersionUID */
   private static final long serialVersionUID = -2227021688745286343L;

   // Attributes ----------------------------------------------------

   /** The format id */
   private int formatId;

   /** The gid */
   private byte[] globalTransactionId;

   /** The branch */
   private byte[] branchQualifier;

   /** Cached toString() */
   private transient String cachedToString;

   /** Cached hashCode() */
   private transient int cachedHashCode;

   // Static --------------------------------------------------------

   // Constructors --------------------------------------------------

   /**
    * Create a new wrapper Xid
    * 
    * @param xid the wrapped xid
    */
   public JBossMQXid(Xid xid)
   {
      formatId = xid.getFormatId();
      globalTransactionId = xid.getGlobalTransactionId();
      branchQualifier = xid.getBranchQualifier();
   }

   // Public --------------------------------------------------------

   // Xid implementation --------------------------------------------

   public int getFormatId()
   {
      return formatId;
   }

   public byte[] getGlobalTransactionId()
   {
      return globalTransactionId;
   }

   public byte[] getBranchQualifier()
   {
      return branchQualifier;
   }

   // Object overrides ----------------------------------------------

   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;
   }

   // Package protected ---------------------------------------------

   // Protected -----------------------------------------------------

   // Private -------------------------------------------------------

   // Inner classes -------------------------------------------------
}