com.bbn.openmap.util
Class Assert

java.lang.Object
  extended by com.bbn.openmap.util.Assert

public final class Assert
extends java.lang.Object

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 assertion
       
 
 Assert.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 write
       
 
 Assert.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.
  
 

Version:
$Revision: 1.4 $, $Date: 2004/10/14 18:06:29 $
Author:
Peter Van Der Linden, Maintained by: Tom Mitchell (tmitchell@bbn.com)

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

enabled

public static final boolean enabled
Globally enable or disable assertions.

See Also:
Constant Field Values
Method Detail

assertExp

public static final void assertExp(boolean b,
                                   java.lang.String s)
Assert a condition to be true. If it is not true, an exception is thrown.

Parameters:
b - An expression expected to be true
s - Exception string if expression is false
Throws:
AssertionException - if expression is false

assertExp

public static final void assertExp(boolean b)
Assert a condition to be true. If it is not true, an exception is thrown.

Parameters:
b - An expression expected to be true
Throws:
AssertionException - if expression is false


Copyright (C) BBNT Solutions LLC; See http://openmap.bbn.com/ for details