KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > util > Assert


1 /*
2  * Assert.java
3  */

4
5 package polyglot.util;
6
7 /**
8  * Assert
9  *
10  * Overview:
11  * Assert contains a few methods helpful for implementing assertions in
12  * Java.
13  **/

14 public final class Assert {
15
16   /**
17    * static void check(boolean ok)
18    *
19    * Throws an error if not <ok>.
20    **/

21   public static void check(boolean ok) {
22     if (!ok)
23       throw new AssertionFailedError("Assertion failed");
24   }
25
26   /**
27    * static void check(String condition, boolean ok)
28    *
29    * Asserts that <condition> holds -- in other words, that <ok> is true.
30    * Throws an error otherwise.
31    **/

32   public static void check(String JavaDoc condition, boolean ok) {
33     if (!ok)
34       throw new AssertionFailedError("Assertion \"" +
35                      condition + "\" failed.");
36   }
37   
38   // This class cannot be instantiated.
39
private Assert() {}
40   // The error thrown.
41
private static class AssertionFailedError extends Error JavaDoc {
42     public AssertionFailedError() { super(); }
43     public AssertionFailedError(String JavaDoc s) { super(s); }
44   }
45
46 }
47
48
49
Popular Tags