Created
February 21, 2011 18:48
-
-
Save dustingetz/837504 to your computer and use it in GitHub Desktop.
custom assert and verify
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Locale; | |
| public class Util | |
| { | |
| /** | |
| * Always-enabled assertions. | |
| * | |
| * "Each assertion contains a boolean expression that you believe will be true | |
| * when the assertion executes. If it is not true, the system will throw an error. | |
| * By verifying that the boolean expression is indeed true, the assertion confirms | |
| * your assumptions about the behavior of your program, increasing your confidence | |
| * that the program is free of errors. Experience has shown that writing assertions | |
| * while programming is one of the quickest and most effective ways to detect and | |
| * correct bugs. As an added benefit, assertions serve to document the inner workings | |
| * of your program, enhancing maintainability." | |
| * See: http://download.oracle.com/javase/1.4.2/docs/guide/lang/assert.html | |
| * | |
| * We are extending Java's built-in assert functionality because: | |
| * 1. we want the assert exception to derive from RuntimeException, not RuntimeError | |
| * 2. we want to warn if they are turned off | |
| * | |
| * You don't need to bother to i18n an assert message, because asserts are meant | |
| * to validate invariants. | |
| * | |
| * @param assertedCondition if false, throws an exception | |
| * @param locale | |
| * @param fmt error message format string | |
| * @param fmtArgs format string args | |
| */ | |
| public static void Verify(boolean assertedCondition, Locale locale, String fmt, Object... fmtArgs) | |
| { | |
| if (assertedCondition) | |
| return; | |
| String sErrorPreamble = (new UnknownException()).getMessage(locale); | |
| String sCallerError = String.format(fmt, fmtArgs); | |
| String sError = sErrorPreamble + ": " + sCallerError; | |
| throw new RuntimeException(sError); | |
| } | |
| public static void Verify(boolean condition, String fmt, Object... fmtArgs) | |
| { | |
| Locale locale = null; | |
| Verify(condition, locale, fmt, fmtArgs); | |
| } | |
| public static void Verify(boolean condition) | |
| { | |
| Verify(condition, ""); | |
| } | |
| /** | |
| * Regular assertions may be disabled in production. | |
| * | |
| * any in-line boolean expressions shouldn't have side effects. | |
| * Under current implentation its OK if they have unintentional side effects | |
| * because | |
| * | |
| * @param condition if false, throws an exception | |
| * @param locale | |
| * @param fmt error message format string | |
| * @param fmtArgs format string args | |
| */ | |
| public static void Assert(boolean condition, Locale locale, String fmt, Object... fmtArgs) | |
| { | |
| try | |
| { | |
| Verify(condition, locale, fmt, fmtArgs); | |
| } | |
| catch (RuntimeException e) | |
| { | |
| if(debug) | |
| { | |
| logger.warn(e); | |
| return; | |
| } | |
| throw e; | |
| } | |
| } | |
| public static void Assert(boolean condition, String fmt, Object... fmtArgs) | |
| { | |
| Locale locale = null; | |
| Assert(condition, locale, fmt, fmtArgs); | |
| } | |
| public static void Assert(boolean condition) | |
| { | |
| Assert(condition, ""); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment