/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.tm;

import java.io.Externalizable;
import java.io.IOException;
import javax.transaction.xa.Xid;

/**
 * This object encapsulates the global transaction ID of a transaction.
 * It is similar to an Xid, but holds only the GlobalId part.
 * This implementation is immutable and always serializable at runtime.
 *
 * @see XidImpl
 * @author <a href="mailto:osh@sparre.dk">Ole Husgaard</a>
 * @author <a href="mailto:adrian@jboss.com">Adrian Brock</a>
 * @version $Revision: 1.2.2.2 $
 */
public class GlobalId implements Externalizable
{
   static final long serialVersionUID = 6879509375433435464L;

   /**
    *  Hash code of this instance. This is really a sequence number.
    */
   private int hash;

   /**
    *  Global transaction id of this instance.
    *  The coding of this class depends on the fact that this variable is
    *  initialized in the constructor and never modified. References to
    *  this array are never given away, instead a clone is delivered.
    */
   private byte[] globalId;

   public GlobalId()
   {
      // Used for Externalizable support
   }

   /**
    *  Create a new instance. This constructor is public <em>only</em>
    *  to get around a class loader problem; it should be package-private.
    */
   public GlobalId(int hash, byte[] globalId)
   {
      this.hash = hash;
      this.globalId = globalId;
   }

   public GlobalId(Xid xid)
   {
      this.hash = xid.hashCode();
      if (xid instanceof XidImpl)
         this.globalId = ((XidImpl) xid).getInternalGlobalTransactionId();
      else
         this.globalId = xid.getGlobalTransactionId();
   }

   /**
    *  Compare for equality.
    *
    *  Instances are considered equal if they both refer to the same
    *  global transaction id.
    */
   public boolean equals(Object obj)
   {
      if (obj instanceof GlobalId)
      {
         GlobalId other = (GlobalId) obj;

         if (hash != other.hash)
            return false;

         if (globalId == other.globalId)
            return true;

         if (globalId.length != other.globalId.length)
            return false;

         int len = globalId.length;
         for (int i = 0; i < len; ++i)
            if (globalId[i] != other.globalId[i])
               return false;

         return true;
      }
      return false;
   }

   public int hashCode()
   {
      return hash;
   }

   public void writeExternal(java.io.ObjectOutput out) throws IOException
   {
      out.writeInt(hash);
      out.writeObject(globalId);
   }

   public void readExternal(java.io.ObjectInput in) throws IOException, ClassNotFoundException
   {
      hash = in.readInt();
      globalId = (byte[]) in.readObject();
   }
}
