001// Copyright (c) 2001 Hursh Jain (http://www.mollypages.org) 002// The Molly framework is freely distributable under the terms of an 003// MIT-style license. For details, see the molly pages web site at: 004// http://www.mollypages.org/. Use, modify, have fun ! 005 006package fc.util; 007 008/** 009This class is intended primarily for checking whether method paramaters 010meet various pre-conditions. Throws a <tt>IllegalArgumentException</tt> in 011case of failure. 012<p> 013ThreadSafety: This class <b>is</b> thread safe and can be used by multiple threads 014concurrently. 015 016@author hursh jain 017@version 1.0 018@date 5/30/2002 019**/ 020public final class Argcheck 021{ 022protected static IArgcheckFailHandler handler = new BasicArgcheckFailHandler(); 023 024private Argcheck() { /* no instantiation */ } 025 026/** 027Checks that the specified argument is false. 028@param val value to be tested 029**/ 030public static void isfalse(boolean val) 031 { 032 if (val) { 033 fail(null); 034 } 035 } 036 037/** 038Checks that the specified argument is false. 039@param val value to be tested 040@param message message for the exception 041**/ 042public static void isfalse(boolean val, String message) 043 { 044 if (val) { 045 fail(message); 046 } 047 } 048 049/** 050Checks that the specified argument is true. 051@param val value to be tested 052**/ 053public static void istrue(boolean val) 054 { 055 if (! val) { 056 fail(null); 057 } 058 } 059 060/** 061Checks that the specified argument is true. 062@param val value to be tested 063@param message message for the exception 064**/ 065public static void istrue(boolean val, String message) 066 { 067 if (! val) { 068 fail(message); 069 } 070 } 071 072/** 073Checks that the specified argument is not null. 074@param obj value to be tested 075**/ 076public static void notnull(Object obj) 077 { 078 if (obj == null) { 079 fail(null); 080 } 081 } 082 083/** 084Checks that the specified argument is not null. 085@param obj value to be tested 086@param message message for the exception 087**/ 088public static void notnull(Object obj, String message) 089 { 090 if (obj == null) { 091 fail(message); 092 } 093 } 094 095/** 096Checks that the specified argument is null. 097@param obj value to be tested 098**/ 099public static void isnull(Object obj) 100 { 101 if (obj != null) { 102 fail(null); 103 } 104 } 105 106/** 107Checks that the specified argument is null. 108@param obj value to be tested 109@param message message for the exception 110**/ 111public static void isnull(Object obj, String message) 112 { 113 if (obj != null) { 114 fail(message); 115 } 116 } 117 118/** 119Checks that the specified arguments are equal to each other by using 120<tt>Object.equals()</tt>. 121@param a first value to be tested 122@param b second value to be tested 123**/ 124public static void isequal(Object a, Object b) 125 { 126 isequal(a, b, null); 127 } 128 129/** 130Checks that the specified arguments are equal to each other by using 131<tt>Object.equals()</tt>. 132@param a first value to be tested 133@param b second value to be tested 134@param message message message for the exception 135**/ 136public static void isequal(Object a, Object b, String message) 137 { 138 /* A B equals? 139 1 null notnull forsure no, can also check using b.equals(a) 140 2 null null yes, cannot check using equals 141 3 notnull null forsure no, can also check using a.equals(b) 142 4 notnull notnull have to check using equals() 143 */ 144 if (a != b && a !=null && !a.equals(b)) { 145 fail(message); 146 } 147 } 148 149 150/** 151Checks that the specified arguments are not equal to each other by using 152<tt>Object.equals()</tt>. 153@param a first value to be tested 154@param b second value to be tested 155**/ 156public static void notequal(Object a, Object b) 157 { 158 notequal(a,b,null); 159 } 160 161/** 162Checks that the specified arguments are not equal to each other by using 163<tt>Object.equals()</tt>. 164@param a first value to be tested 165@param b second value to be tested 166@param message message message for the exception 167**/ 168public static void notequal(Object a, Object b, String message) 169 { 170 /* A B not equal ? 171 1 null notnull forsure yes, can also check using b.equals(a) 172 2 null null no, cannot check using equals 173 3 notnull null forsure yes ,can also check using a.equals(b) 174 4 notnull notnull have to check using equals() 175 */ 176 if ( a == b || (a !=null && a.equals(b)) ) { 177 fail(message); 178 } 179 } 180 181/** 182Checks to see if specified objects are the same object, that is, the 183<b>references</b> are identical. This provides a stronger notion of 184equality than the <tt>Object.equals()</tt> method. 185@param a first value to be tested 186@param b second value to be tested 187**/ 188public static void issame(Object a, Object b) 189 { 190 if (a != b) { 191 fail(null); 192 } 193 } 194 195/** 196Checks to see if specified objects are the same object, that is, the 197<b>references</b> are identical. This provides a stronger notion of 198equality than the <tt>Object.equals()</tt> method. 199@param a first value to be tested 200@param b second value to be tested 201@param message message message for the exception 202**/ 203public static void issame(Object a, Object b, String message) 204 { 205 if (a != b) { 206 fail(message); 207 } 208 } 209 210/** 211Checks to see if specified objects are not the same object, that is, the 212<b>references</b> are not identical. 213@param a first value to be tested 214@param b second value to be tested 215**/ 216public static void notsame(Object a, Object b) 217 { 218 if (a == b) { 219 fail(null); 220 } 221 } 222 223/** 224Checks to see if specified objects are not the same object, that is, the 225<b>references</b> are not identical. 226@param a first value to be tested 227@param b second value to be tested 228@param message message message for the exception 229**/ 230public static void notsame(Object a, Object b, String message) 231 { 232 if (a == b) { 233 fail(message); 234 } 235 } 236 237/** 238Sets a different error handler. 239@param thehandler the new error handler 240@return <tt>true</tt> is successful, <tt>false</tt> otherwise 241**/ 242public static synchronized boolean setHandler(IArgcheckFailHandler thehandler) 243 { 244 if (thehandler != null) { 245 handler = thehandler; 246 return true; 247 } 248 new Exception("Checker.setHandler(): could not set new handler because 'thehandler' paramater was null.").printStackTrace(System.err); 249 return false; 250 } 251 252protected static void fail(String message) { 253 handler.handle(message); 254 } 255 256public static void main(String args[]) 257 { 258 Object o1 = new Object(); 259 Object o2 = o1; 260 java.util.Map m1 = new java.util.HashMap(); 261 java.util.Map m2 = new java.util.HashMap(); 262 m1.put("hello", "world"); 263 m2.put("hello", "world"); 264 265 System.out.println("[check] o1["+o1+"] and o2["+o2+"] are equal...[should be ok]"); 266 try { 267 isequal(o1, o2, "o1 and o2 equal check"); 268 } 269 catch (RuntimeException e) { } 270 271 System.out.println("[check] o1["+o1+"] and o2["+o2+"] are not equal...[should throw error]"); 272 try { 273 notequal(o1, o2, "o1 and o2 not equal check"); 274 } 275 catch (RuntimeException e) { } 276 277 System.out.println("setting both o1 and o2 to be null"); 278 279 o1 = o2 = null; 280 281 System.out.println("[start] o1["+o1+"] and o2["+o2+"] are equal...[should be ok]"); 282 try { 283 isequal(o1, o2, "o1 and o2 equal check"); 284 } 285 catch (RuntimeException e) { } 286 287 System.out.println("[start] o1["+o1+"] and o2["+o2+"] are not equal...[should throw error]"); 288 try { 289 notequal(o1, o2, "o1 and o2 not equal check"); 290 } 291 catch (RuntimeException e) { } 292 293 o1 = new Object(); 294 295 System.out.println("[start] o1["+o1+"] and o2["+o2+"] are equal...[should throw error]"); 296 try { 297 isequal(o1, o2, "o1 and o2 equal check"); 298 } 299 catch (RuntimeException e) { } 300 301 System.out.println("[start] o1["+o1+"] and o2["+o2+"] are not equal...[should be ok]"); 302 try { 303 notequal(o1, o2, "o1 and o2 not equal check"); 304 } 305 catch (RuntimeException e) { } 306 307 System.out.println("[start] m1["+m1+"] and m2["+m2+"] are equal...[should be ok]"); 308 try { 309 isequal(m1, m2, "m1 and m2 equal check"); 310 } 311 catch (RuntimeException e) { } 312 313 System.out.println("[start] m1["+m1+"] and o2["+m2+"] are not equal...[should throw error]"); 314 try { 315 notequal(m1, m2, "m1 and m2 not equal check"); 316 } 317 catch (RuntimeException e) { } 318 319 320 System.out.println("[start] m1["+m1+"] and o1["+o1+"] are equal...[should throw error]"); 321 try { 322 isequal(m1, o1, "m1 and o1 equal check"); 323 } 324 catch (RuntimeException e) { } 325 326 System.out.println("[start] m1["+m1+"] and o1["+o1+"] are not equal...[should be ok]"); 327 try { 328 notequal(m1, o1, "m1 and o1 not equal check"); 329 } 330 catch (RuntimeException e) { } 331 332 333 } 334} //~class Argcheck