|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.bbn.openmap.util.Assert
public final class Assert
Assert provides an assertion facility in Java comparable to the assert macros in C/C++. This class was taken from the Java FAQ maintained by Perter Van Der Linden at http://www.afu.com/intro.html , section 17, question 6. Here is the original entry in the FAQ:
11.6 How can I write C/C++ style assertions in Java? A. The two classes shown below provide an assertion facility in Java. Set Assert.enabled to true to enable the assertions, and to false to disable assertions in production code. The AssertionException is not meant to be caught--instead, let it print a trace. With a good optimizing compiler there will be no run time overhead for many uses of these assertions when Assert.enabled is set to false. However, if the condition in the assertion may have side effects, the condition code cannot be optimized away. For example, in the assertionAssert.assertExp(size() <= maxSize, "Maximum size exceeded");
the call to size() cannot be optimized away unless the compiler can see that the call has no side effects. C and C++ use the preprocessor to guarantee that assertions will never cause overhead in production code. Without a preprocessor, it seems the best we can do in Java is to writeAssert.assertExp(Assert.enabled && size() <= maxSize, "Too big");
In this case, when Assert.enabled is false, the method call can always be optimized away, even if it has side effects.
Field Summary | |
---|---|
static boolean |
enabled
Globally enable or disable assertions. |
Method Summary | |
---|---|
static void |
assertExp(boolean b)
Assert a condition to be true. |
static void |
assertExp(boolean b,
java.lang.String s)
Assert a condition to be true. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final boolean enabled
Method Detail |
---|
public static final void assertExp(boolean b, java.lang.String s)
b
- An expression expected to be trues
- Exception string if expression is false
AssertionException
- if expression is falsepublic static final void assertExp(boolean b)
b
- An expression expected to be true
AssertionException
- if expression is false
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |