// (c) MIT 2003.  All rights reserved.

////////////////////////////////////////////////////////////////////////////////
//
// FILE:	Id.java
//
// AUTHOR:	Manoj D. Muzumdar
//		archon@ai.mit.edu
//
// DATE:	June, 1998
//
// COPYRIGHT:	Massachusetts Institute of Technology
//		All rights reserved.
//
// PURPOSE:	Utility class
//
////////////////////////////////////////////////////////////////////////////////

package rec.util;

import java.io.*;

/**
 * Id is an immutable class.
 */
public final class Id implements Serializable, Comparable
{
  public Id()
  {
    m_Id = getNextId();
  }

  //////////////////////////////////////////////////////////////////////////////
  public Id( Id id )
  {
    m_Id = id.m_Id;
  }

  public boolean equals( Id id )
  {
    return( m_Id == id.m_Id );
  }

  public int compareTo( Object o )
  {
    Assert.check( o instanceof Id );

    Id id = (Id)o;

    if ( m_Id < id.m_Id ) {
      return -1;
    }
    if ( m_Id > id.m_Id ) {
      return 1;
    }
    return 0;
  }

  /*
    This should not be here.
    Just cast the object to send it in.

    Yes it should.  If a collection has Id's in it it will use
    equals(Object) to compare the objects not equals(Id).  It needs to
    explcitly do the cast if both objects are Ids.
    -MO 2-28-2000
  */
  public boolean equals( Object id )
  {
    if ( id instanceof Id ) {
      return equals( (Id)id );
    }
    return false;
  } 

  public int hashCode()
  {
    return( (new Integer( m_Id ) ).hashCode() );
  }

  public String toString()
  {
    return( (new Integer( m_Id )).toString() );
  }

  /////////////////////////////////////////////////////////////////////////////

  private int getNextId()
  {
    //    Console.writeln( "Making id " + s_NextId );
    return( s_NextId++ );
  }

  /////////////////////////////////////////////////////////////////////////////

  private static int s_NextId = 1;
  private int m_Id;
}

///////////////////////////////////////////////////////////////////////////////
