// Copyright (c) 1996-2002 Brian D. Carlstrom

package bdc.util;

import java.util.logging.Level;

/**
    Assert methods
*/
public final class Assert
{
    /**
        Standard assertion method for declaring invariants. If b is
        false, an error message including the specified string is
        logged and a FatalAssertionException is thrown.

        @param b <b>true</b> if the invariant is true, <b>false</b>
        otherwise
        @param string message to log with the error

        @exception FatalAssertionException if the invariant <b>b</b>
        is false
    */
    public static void that (boolean b, String string)
    {
        if (!b) {
            fatal(string);
        }
    }

    /**
        Standard assertion method for declaring invariants. If b is
        false, an error message including the specified string is
        logged and a FatalAssertionException is thrown.

        @param b <b>true</b> if the invariant is true, <b>false</b>
        otherwise
        @param fmt control string for the error message as used in
        Fmt.S
        @param a1 the first argument to the format string for the
        error message.

        @exception FatalAssertionException if the invariant <b>b</b>
        is false

        @see bdc.util.Fmt#S(String, Object)
    */
    public static void that (boolean b, String fmt, Object a1)
    {
        if (!b) {
            fatal(Fmt.S(fmt, a1));
        }
    }

    /**
        Standard assertion method for declaring invariants. If b is
        false, an error message including the specified string is
        logged and a FatalAssertionException is thrown.

        @param b <b>true</b> if the invariant is true, <b>false</b>
        otherwise
        @param fmt control string for the error message as used in
        Fmt.S
        @param a1 the first argument to the format string for the
        error message.
        @param a2 the second argument to the format string for the
        error message.

        @exception FatalAssertionException if the invariant <b>b</b>
        is false

        @see bdc.util.Fmt#S(String, Object, Object)
    */
    public static void that (boolean b, String fmt, Object a1, Object a2)
    {
        if (!b) {
            fatal(Fmt.S(fmt, a1, a2));
        }
    }

    /**
        Standard assertion method for declaring invariants. If b is
        false, an error message including the specified string is
        logged and a FatalAssertionException is thrown.

        @param b <b>true</b> if the invariant is true, <b>false</b>
        otherwise
        @param fmt control string for the error message as used in
        Fmt.S
        @param a1 the first argument to the format string for the
        error message.
        @param a2 the second argument to the format string for the
        error message.
        @param a3 the third argument to the format string for the
        error message.

        @exception FatalAssertionException if the invariant <b>b</b>
        is false

        @see bdc.util.Fmt#S(String, Object, Object)
    */
    public static void that (boolean b, String fmt, Object a1, Object a2,
                               Object  a3)
    {
        if (!b) {
            fatal(Fmt.S(fmt, a1, a2, a3));
        }
    }

    /**
        Standard assertion method for declaring invariants. If b is
        false, an error message including the specified string is
        logged and a FatalAssertionException is thrown.

        @param b <b>true</b> if the invariant is true, <b>false</b>
        otherwise
        @param fmt control string for the error message as used in
        Fmt.S
        @param a1 the first argument to the format string for the
        error message.
        @param a2 the second argument to the format string for the
        error message.
        @param a3 the third argument to the format string for the
        error message.
        @param a4 the fourth argument to the format string for the
        error message.

        @exception FatalAssertionException if the invariant <b>b</b>
        is false

        @see bdc.util.Fmt#S(String, Object, Object)
    */
    public static void that (boolean b, String fmt, Object a1, Object a2,
                               Object  a3, Object a4)
    {
        if (!b) {
            fatal(Fmt.S(fmt, a1, a2, a3, a4));
        }
    }

    /**
        Standard assertion method for declaring invariants. If b is
        false, an error message including the specified string is
        logged and a FatalAssertionException is thrown.

        @param b <b>true</b> if the invariant is true, <b>false</b>
        otherwise
        @param fmt control string for the error message as used in
        Fmt.S
        @param a1 the first argument to the format string for the
        error message.
        @param a2 the second argument to the format string for the
        error message.
        @param a3 the third argument to the format string for the
        error message.
        @param a4 the fourth argument to the format string for the
        error message.
        @param a5 the fifth argument to the format string for the
        error message.

        @exception FatalAssertionException if the invariant <b>b</b>
        is false

        @see bdc.util.Fmt#S(String, Object, Object)
    */
    public static void that (boolean b, String fmt, Object a1, Object a2,
                               Object  a3, Object a4, Object a5)
    {
        if (!b) {
            fatal(Fmt.S(fmt, a1, a2, a3, a4, a5));
        }
    }

    /**
        Standard assertion method for declaring invariants. If b is
        false, an error message including the specified string is
        logged.

        @param b <b>true</b> if the invariant is true, <b>false</b>
        otherwise
        @param string message to log with the error
    */
    public static void nonFatal (boolean b, String string)
    {
        if (!b) {
            nonFatal(string);
        }
    }

    /**
        Standard assertion method for declaring invariants. If b is
        false, an error message including the specified string is
        logged.

        @param b <b>true</b> if the invariant is true, <b>false</b>
        otherwise
        @param fmt control string for the error message as used in
        Fmt.S
        @param arg the first argument to the format string for the
        error message.
    */
    public static void nonFatal (boolean b, String fmt, Object arg)
    {
        if (!b) {
            nonFatal(Fmt.S(fmt, arg));
        }
    }

    /**
        Standard assertion method for declaring invariants. If b is
        false, an error message including the specified string is
        logged.

        @param b <b>true</b> if the invariant is true, <b>false</b>
        otherwise
        @param fmt control string for the error message as used in
        Fmt.S
        @param arg1 the first argument to the format string for the
        error message.
        @param arg2 the second argument to the format string for the
        error message.
    */
    public static void nonFatal (boolean b, String fmt, Object arg1,
                                       Object arg2)
    {
        if (!b) {
            nonFatal(Fmt.S(fmt, arg1, arg2));
        }
    }
    
    /**
        Standard assertion method for declaring invariants. If b is
        false, an error message including the specified string is
        logged.

        @param b <b>true</b> if the invariant is true, <b>false</b>
        otherwise
        @param fmt control string for the error message as used in
        Fmt.S
        @param arg1 the first argument to the format string for the
        error message.
        @param arg2 the second argument to the format string for the
        error message.
        @param arg3 the third argument to the format string for the
        error message.
    */
    public static void nonFatal (boolean b, String fmt, Object arg1,
                                       Object arg2, Object arg3)
    {
        if (!b) {
            nonFatal(Fmt.S(fmt, arg1, arg2, arg3));
        }
    }
    
    /**
        Standard assertion method for declaring invariants. If b is
        false, an error message including the specified string is
        logged.

        @param b <b>true</b> if the invariant is true, <b>false</b>
        otherwise
        @param fmt control string for the error message as used in
        Fmt.S
        @param arg1 the first argument to the format string for the
        error message.
        @param arg2 the second argument to the format string for the
        error message.
        @param arg3 the third argument to the format string for the
        error message.
        @param arg4 the fourth argument to the format string for the
        error message.
        @param arg5 the fifth argument to the format string for the
        error message.
    */
    public static void nonFatal (boolean b, String fmt, Object arg1,
                                       Object arg2, Object arg3, Object arg4)
    {
        if (!b) {
            nonFatal(Fmt.S(fmt, arg1, arg2, arg3,arg4));
        }
    }
    
    /**
        Standard assertion method for declaring invariants. If b is
        false, an error message including the specified string is
        logged.

        @param b <b>true</b> if the invariant is true, <b>false</b>
        otherwise
        @param fmt control string for the error message as used in
        Fmt.S
        @param arg1 the first argument to the format string for the
        error message.
        @param arg2 the second argument to the format string for the
        error message.
        @param arg3 the third argument to the format string for the
        error message.
        @param arg4 the fourth argument to the format string for the
        error message.
        @param arg5 the fifth argument to the format string for the
        error message.
    */
    public static void nonFatal (boolean b, String fmt, Object arg1,
                                       Object arg2, Object arg3, Object arg4,
                                       Object arg5)
    {
        if (!b) {
            nonFatal(Fmt.S(fmt, arg1, arg2, arg3,arg4, arg5));
        }
    }

    private static void nonFatal (String message)
    {
        Log.util.log(Level.SEVERE,
                     "Assert failed: {0}\nThread: {1} {2}\n{3}", 
                     new Object[] {
                         message,
                         Thread.currentThread().getName(),
                         ThreadDebugState.makeString(),
                         SystemUtil.stackTrace()}
                     );
    }

    private static void fatal (String message)
    {
        nonFatal(message);
            // intentionally stop debugger in its tracks
        throw new AssertionError(message);
    }

    /**
        prevent people from creating instances of this class
    */
    private Assert ()
    {
    }
}
