/*
 * JBoss, the OpenSource J2EE webOS
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.aspects.versioned;
import org.jboss.aop.InstanceAdvised;
import org.jboss.util.id.GUID;

public class DistributedFieldUpdate implements java.io.Serializable
{
   private static final long serialVersionUID = -8249659475610689146L;
   
   protected Object val;
   protected long version;
   protected int index;

   public DistributedFieldUpdate() {}
   public DistributedFieldUpdate(Object val, long version, int index)
   {
      setValue(val);
      this.version = version;
      this.index = index;
   }
   public long getVersionId() { return version; }
   public void setVersionId(long newId) { version = newId; }
   public int getFieldIndex() { return index; }


   public boolean equals(Object obj)
   {
      DistributedFieldUpdate update = (DistributedFieldUpdate)obj;
      return update.index == this.index;
   }

   public int hashCode()
   {
      return index;
   }


   public Object getValue() 
   { 
      if (val instanceof VersionReference)
      {
         return ((VersionReference)val).get();
      }
      return val; 
   }
   public void setValue(Object newVal) 
   { 
      if (newVal instanceof InstanceAdvised)
      {
         InstanceAdvised advised = (InstanceAdvised)newVal;
         GUID guid = VersionManager.getGUID(advised);
         if (guid != null)
         {
            // we are versioned
            val = new VersionReference(guid, advised);
            return;
         }
         
      }
      val = newVal; 
   }
   public Object getNonDereferencedValue()
   {
      return val;
   }
}
