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

////////////////////////////////////////////////////////////////////////////////
//
// FILE:        StringType.java
//
// AUTHOR:      Manoj D. Muzumdar
//              archon@ai.mit.edu
//
// DATE:        January, 1999
//
// COPYRIGHT:   Massachusetts Institute of Technology
//              All rights reserved.
//
// PURPOSE:     Basic data type analysis of strings
//
////////////////////////////////////////////////////////////////////////////////

package rec.util;

/**
 * archon:
 *
 * Currently, only boolean, int, and double have been implemented because
 *   that is all I forsee needing right now;
 **/
public final class StringType
{
  public static boolean isBoolean( String s )
  {
    String tmp = s.trim().toLowerCase();

    if( tmp.equals( "true" ) || tmp.equals( "false" ) )
      return( true );

    return( false );
  }

  public static boolean isInt( String s )
  {
    Integer i = null;

    try {
      i = new Integer( s );
    }
    catch( NumberFormatException e ) {
      return( false );
    }

    return( true );
  }

  public static boolean isDouble( String s )
  {
    Double d = null;

    try {
      d = new Double( s );
    }
    catch( NumberFormatException e ) {
      return( false );
    }

    return( true );
  }

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

  public static boolean getBoolean( String s )
  {
    Assert.check( isBoolean( s ), "string must be a boolean" );

    String tmp = s.trim().toLowerCase();

    // true case
    if( tmp.equals( "true" ) )
      return( true );

    // false case
    return( false );
  }

  public static int getInt( String s )
  {
    Assert.check( isInt( s ), "string must be an int" );

    Integer i = null;

    try {
      i = new Integer( s );
    }
    catch( NumberFormatException e ) {
      Assert.fatal();
    }

    return( i.intValue() );
  }

  public static double getDouble( String s )
  {
    Assert.check( isDouble( s ), "string must be a double" );

    Double d = null;

    try {
      d = new Double( s );
    }
    catch( NumberFormatException e ) {
      Assert.fatal();
    }

    return( d.doubleValue() );
  }
}

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