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

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

package rec.util;

/**
 * Assert is a library of assertion routines.
 */
public final class Assert
{
  private static boolean m_window = false;
  
  public static void error()
  {
    if( m_window ) {
      DebugWindow.addLine( "ERROR", "Asserted error" );
      DebugWindow.makeActive( "ERROR" );
    }
    
    throw(
      new RuntimeException( "AssertedErrorException" ) );
  }

  public static void error( String msg )
  {
    if( m_window ) {
      DebugWindow.addLine( "ERROR", "Asserted error: " + msg);
      DebugWindow.makeActive( "ERROR" );
    }

    throw( new RuntimeException( "AssertedErrorException: " + msg ) );
  }

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

  public static void check( boolean b )
  {
    if( !b ) {
      if( m_window ) {
	DebugWindow.addLine( "ERROR", "Asserted error" );
	DebugWindow.makeActive( "ERROR" );
      }
      
      throw( new RuntimeException( "FailedAssertionException" ) );
    }
    
  }

  public static void check( boolean b, String msg )
  {
    if( !b ) {
      if( m_window ) {
	DebugWindow.addLine( "ERROR", "Asserted error: " + msg);
	DebugWindow.makeActive( "ERROR" );
      }
    
      throw( new RuntimeException( "FailedAssertionException: "
                                   + msg ) );
    }
    
  }

  public static void notNull( Object o, String msg )
  {
    if( o == null ) {
      if( m_window ) {
	DebugWindow.addLine( "ERROR", "Asserted error: " + msg);
	DebugWindow.makeActive( "ERROR" );
      }
    
      throw( new RuntimeException( "FailedAssertionException: "
                                   + msg ) );
    }
    
  }

  public static void notNull( Object o )
  {
    if( o == null ) {
      if( m_window ) {
	DebugWindow.addLine( "ERROR", "Asserted error");
	DebugWindow.makeActive( "ERROR" );
      }

      throw( new RuntimeException( "FailedAssertionException" ) );
    }
    
  }

  public static void note( boolean b, String msg )
  {
    if( !b ) {
      if( m_window ) {
	DebugWindow.addLine( "Note", msg);
      }
      
      Console.writeln( "ASSERT NOTE: " + msg );
    }
  }

  public static void note( String msg )
  {
    if( m_window ) {
      DebugWindow.addLine( "Note", msg);
    }
    
    Console.writeln( "ASSERT NOTE: " + msg );
  }

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

  public static void fatal()
  {
    throw( ImplementationError.create() );
  }

  public static void fatal( String msg )
  {
    throw( ImplementationError.create( msg ) );
  }
}

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