/*
* JBoss, the OpenSource EJB server
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.mx.util;

import javax.management.ObjectName;

/**
 * An observed object
 */
public class ObservedObject
{
  // Constants -----------------------------------------------------
   /**
    * Used to reset errors in {@link #alreadyNotified}.
    */
   public static final int RESET_FLAGS_ALREADY_NOTIFIED = 0;

   /**
    * An observed attribute type error has been notified.
    */
   public static final int RUNTIME_ERROR_NOTIFIED = 8;

   /**
    * An observed object error has been notified.
    */
   public static final int OBSERVED_OBJECT_ERROR_NOTIFIED = 1;

   /**
    * An observed attribute error has been notified.
    */
   public static final int OBSERVED_ATTRIBUTE_ERROR_NOTIFIED = 2;

   /**
    * An observed attribute type error has been notified.
    */
   public static final int OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED = 4;

  // Attributes ----------------------------------------------------
  
  /**
   * The object name.
   */
  private ObjectName objectName;
  
  /**
   * The notified attribute.
   */
  private int alreadyNotified = RESET_FLAGS_ALREADY_NOTIFIED;
  
  /**
   * The derived gauge.
   */
  private Object derivedGauge;
  
  /**
   * The last value.
   */
  private Object lastValue;
  
  /**
   * The derived gauge timestamp.
   */
  private long derivedGaugeTimeStamp;
  
  /**
   * The threshold.
   */
  private Object threshold;

  // Static --------------------------------------------------------
  
  // Constructors --------------------------------------------------

  /**
   * Construct a new observed object.
   *
   * @param objectName the object name.
   */
  public ObservedObject(ObjectName objectName)
  {
     if (objectName == null)
        throw new IllegalArgumentException("Null object name");
     this.objectName = objectName;
  }

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

  public ObjectName getObjectName()
  {
     return objectName;
  }

  public int getAlreadyNotified()
  {
     return alreadyNotified;
  }

  public boolean isAlreadyNotified(int mask)
  {
     return (alreadyNotified & mask) != 0;
  }

  public boolean notAlreadyNotified(int mask)
  {
     if ((alreadyNotified & mask) == 0)
     {
        alreadyNotified |= mask;
        return true;
     }
     return false;
  }

  public void setNotAlreadyNotified(int mask)
  {
     alreadyNotified &= ~mask;
  }

  public void setAlreadyNotified(int mask)
  {
     alreadyNotified |= mask;
  }

  public void resetAlreadyNotified()
  {
     alreadyNotified = RESET_FLAGS_ALREADY_NOTIFIED;
  }

  public Object getDerivedGauge()
  {
     return derivedGauge;
  }

  public void setDerivedGauge(Object gauge)
  {
     derivedGauge = gauge;
  }

  public Object getLastValue()
  {
     return lastValue;
  }

  public void setLastValue(Object last)
  {
     lastValue = last;
  }

  public long getDerivedGaugeTimeStamp()
  {
     return derivedGaugeTimeStamp;
  }

  public void setDerivedGaugeTimeStamp(long ts)
  {
     derivedGaugeTimeStamp = ts;
  }

  public Object getThreshold()
  {
     return threshold;
  }

  public void setThreshold(Object threshold)
  {
     this.threshold = threshold;
  }

   /**
    * @return human readable string.
    */
   public String toString()
   {
      StringBuffer buffer = new StringBuffer(100);
      buffer.append(getClass().getName()).append("@").append(System.identityHashCode(this)).append("{");
      buffer.append(" objectName=").append(getObjectName());
      buffer.append(" alreadyNotified=").append(getAlreadyNotified());
      buffer.append(" threshold=").append(getThreshold());
      buffer.append(" derivedGauge=").append(getDerivedGauge());
      buffer.append(" derivedGaugeTS=").append(getDerivedGaugeTimeStamp());
      buffer.append(" lastValue=").append(getLastValue());
      return buffer.append("}").toString();
   }

}