KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > Assertion


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.util;
25
26 import com.sun.enterprise.util.diagnostics.CallerInfo;
27 import com.sun.enterprise.util.diagnostics.CallerInfoException;
28
29 /**
30    A class for assertion checking
31
32    @version 1.00 1 May 1999
33    @version 1.10 5 June 2000
34    @author Byron Nevins
35 */

36
37 public class Assertion
38 {
39    /**
40       Check an assertion
41       @param b the condition to check
42       @param s a string describing the check
43       @throws Assertion.Failure if condition not true
44    */

45
46    public static void check(boolean b, String JavaDoc s)
47    { if (doCheck && !b)
48          toss(s);
49    }
50
51    /**
52       Check an assertion
53       @param b the condition to check
54       @throws Assertion.Failure if condition not true
55    */

56
57    public static void check(boolean b)
58    { if (doCheck && !b)
59          toss();
60    }
61
62    /**
63       Check an assertion
64       @param obj an object to check
65       @param s a string describing the check
66       @throws Assertion.Failure if object is null
67    */

68
69    public static void check(Object JavaDoc obj, String JavaDoc s)
70    { if (doCheck && obj == null)
71          toss(s);
72    }
73
74
75    /**
76       Check an assertion
77       @param checkMe a String to check for length > 0
78       @param s a string describing the check
79       @throws Assertion.Failure if checkMe is null or zero-length
80    */

81
82    public static void check(String JavaDoc checkMe, String JavaDoc s)
83    { if (doCheck && (checkMe == null || checkMe.length() <= 0))
84          toss(s);
85    }
86
87
88    /**
89       Check an assertion
90       @param checkMe a String to check for length > 0
91       @throws Assertion.Failure if checkMe is null or zero-length
92    */

93
94    public static void check(String JavaDoc checkMe)
95    { if (doCheck && (checkMe == null || checkMe.length() <= 0))
96          toss();
97    }
98
99    /**
100       Check an assertion
101       @param obj an object to check
102       @throws Assertion.Failure if object is null
103    */

104
105    public static void check(Object JavaDoc obj)
106    { if (doCheck && obj == null)
107          toss();
108    }
109   
110    /**
111       Check an assertion
112       @param x a number
113       @param s a string describing the check
114       @throws Assertion.Failure if number is 0
115    */

116
117    public static void check(double x, String JavaDoc s)
118    { if (doCheck && x == 0)
119          toss(s);
120    }
121
122    /**
123       Check an assertion
124       @param x a number
125       @throws Assertion.Failure if number is 0
126    */

127
128    public static void check(double x)
129    { if (doCheck && x == 0)
130          toss();
131    }
132
133    /**
134       Check an assertion
135       @param x a number
136       @param s a string describing the check
137       @throws Assertion.Failure if number is 0
138    */

139
140    public static void check(long x, String JavaDoc s)
141    { if (doCheck && x == 0)
142          toss(s);
143    }
144
145    /**
146       Check an assertion
147       @param x a number
148       @throws Assertion.Failure if number is 0
149    */

150
151    public static void check(long x)
152    { if (doCheck && x == 0)
153          toss();
154    }
155
156    /**
157       Turn checking on or off
158       @param c true to turn checking on, false to turn checking off
159    */

160
161    public static void setCheck(boolean c)
162    { doCheck = c;
163    }
164    
165    private static boolean doCheck = true;
166
167    /**
168       test stub
169    */

170
171    public static void main(String JavaDoc[] args)
172    { Assertion.check(args);
173       Assertion.check(args.length, "No command line arguments");//NOI18N
174
}
175
176     /////////////////////////////////////////////////////////////////////////
177

178     private static void toss()
179     {
180         toss(null);
181     }
182
183     /////////////////////////////////////////////////////////////////////////
184

185     private static void toss(String JavaDoc gripe)
186     {
187         String JavaDoc msg = "\nAssertion failed";//NOI18N
188
String JavaDoc ci = getCallerInfo();
189         
190         if(ci != null)
191         {
192             msg += " at " + ci;//NOI18N
193
}
194
195         if(gripe != null)
196             msg += " --> " + gripe;//NOI18N
197

198         //assert false : gripe;
199
//assert false;
200
throw new Failure(msg);
201     }
202
203     /////////////////////////////////////////////////////////////////////////
204

205     private static String JavaDoc getCallerInfo()
206     {
207         try
208         {
209             CallerInfo ci = new CallerInfo( new Object JavaDoc[] { staticInstance });
210             return ci.toString();
211         }
212         catch(CallerInfoException e)
213         {
214             return null;
215         }
216     }
217
218     /////////////////////////////////////////////////////////////////////////
219

220     private static Assertion staticInstance = null;
221
222     static
223     {
224         staticInstance = new Assertion();
225     }
226
227     /////////////////////////////////////////////////////////////////////////
228
/////////////////////////////////////////////////////////////////////////
229

230         
231    public static class Failure extends RuntimeException JavaDoc
232    {
233         /**
234         @param gripe a description of the reason for the failure
235         */

236         public Failure(String JavaDoc gripe)
237         {
238             super(gripe);
239         }
240     }
241 }
242
243
Popular Tags