KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > util > ArgChecker


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.admin.util;
25
26 import com.sun.enterprise.admin.util.Assert;
27 import com.sun.enterprise.admin.util.AssertImpl;
28 import com.sun.enterprise.admin.util.Validator;
29 import com.sun.enterprise.admin.util.ValidatorResult;
30
31 /**
32     Use ArgChecker to check method parameters
33
34     <BR>Example usage:
35     <BR>ArgChecker.check( name != null, "null name" );
36     <BR>ArgChecker.check( index, 0, numItems, "index" );
37     <BR>ArgChecker.check( str, "userName");
38     <BR>ArgChecker.check( str, minimumStringLength, "userName");
39     <BR>ArgChecker.checkValid(parameter, "parameter-name");
40 */

41
42 public class ArgChecker
43 {
44     /*
45         We've made this 'final' because we will never disable checks
46         during development. We *may* change it during release, since
47         an IllegalArgumentException could result in the killing of a process,
48         since RuntimeExceptions are not generally caught.
49
50         don't change until we ship (and then only maybe)
51      */

52     private final static boolean sChecksEnabled = true;
53     private final static AssertImpl sImpl;
54
55     static
56     {
57         // need to do this in a static block to guarantee the
58
// setWantStackTrace() call
59
sImpl = new AssertImpl("ArgChecker Failure", AssertImpl.sIllegalArgument);
60         sImpl.setWantStackTrace(false);
61     }
62
63     private ArgChecker()
64     {
65         Assert.assertit(false, "You can't call the ArgChecker constructor!");
66     }
67
68     /**
69         If expression is false, take appropriate action to note the failure.
70         <p>
71         If expression is true, do nothing.
72
73         @param b boolean derived from callers expression.
74         @param msg message to be added to IllegalArgumentException upon failure
75         @throws IllegalArgumentException if b is false
76      */

77     static public final void check( boolean b, Object JavaDoc msg )
78         throws IllegalArgumentException JavaDoc
79     {
80         if ( sChecksEnabled )
81         {
82             sImpl.assertIt(b, msg);
83         }
84     }
85
86     /**
87         Checks that the specified value is in a specified range.
88         <p>
89         A convenience method which calls checkRange()
90         @see #checkRange
91         @param value the value to be range-checked
92         @param min minimum value. value must be >= min
93         @param max maximum value. value must be <= max
94         @param userMsg additional user message (optional) to be included
95         @throws IllegalArgumentException if the specified value is not in the specified range.
96      */

97     public static final void check(long value, long min, long max,
98                                    Object JavaDoc userMsg)
99         throws IllegalArgumentException JavaDoc
100     {
101         checkRange(value, min, max, userMsg);
102     }
103
104     /**
105         Checks that the specified value is in a specified range.
106         <p>
107         The test done is ( value >= min && value <= max )
108
109         If the test fails, then a descriptive string is generated
110         which lists the value together with the min and max and
111         user-specified message.
112
113         @param value the value to be range-checked
114         @param min minimum value. value must be >= min
115         @param max maximum value. value must be <= max
116         @param userMsg additional user message (optional) to be included
117         @throws IllegalArgumentException if the specified value is not in the specified range.
118      */

119     public static final void checkRange(long value, long min, long max,
120                                         Object JavaDoc userMsg)
121         throws IllegalArgumentException JavaDoc
122     {
123         if ( sChecksEnabled )
124         {
125             sImpl.assertRange(value, min, max, userMsg);
126         }
127     }
128
129     /**
130         Checks that the object is valid generically by using a Validation
131         object.<p>
132         If the validation fails, then the check fails as with other checks.
133         @param object the value to be validated
134         @param name name of the object to be validated
135         @param validator validation object to validate the object
136         @throws IllegalArgumentException if validator says that object is NOT valid
137      */

138     public static void checkValid(Object JavaDoc object,
139                                   String JavaDoc name,
140                                   Validator validator)
141         throws IllegalArgumentException JavaDoc
142     {
143         if ( sChecksEnabled )
144         {
145             sImpl.assertValid(object, name, validator);
146         }
147     }
148     
149     /**
150         Checks that the object is valid generically by using a Validation object.
151         <p>
152         Convenience method. Calls checkValid().
153
154         @see checkValid(Object, String, IValidator)
155         @param object the value to be validated
156         @param name name of the object to be validated
157         @param validator validation object to validate the object
158         @throws IllegalArgumentException if validator says that object is NOT valid
159      */

160     public static void check(Object JavaDoc object,
161                              String JavaDoc name,
162                              Validator validator)
163         throws IllegalArgumentException JavaDoc
164     {
165         checkValid(object, name, validator);
166     }
167
168     /**
169         Check that the object is valid.
170         Calls check( object, name, validator ) where validator is either
171         the non-null validator or the object itself, if the object implements
172         IValidator.
173         @param object the value to be validated
174         @param name name of the object to be validated
175         @throws IllegalArgumentException if validator says that object is NOT valid
176      */

177     public static void checkValid(Object JavaDoc object, String JavaDoc name )
178         throws IllegalArgumentException JavaDoc
179     {
180         final Validator validator = (object instanceof Validator ) ?
181                                         (Validator)object :
182                                         BaseValidator.getInstance();
183         check( object, name, validator );
184     }
185
186     /**
187         Check that the object is valid.
188         Convenience method. Calls checkValid().
189
190         @see checkValid(Object, String)
191         @param object the value to be validated
192         @param name name of the object to be validated
193         @throws IllegalArgumentException if validator says that object is NOT valid
194     */

195     public static void check(Object JavaDoc object, String JavaDoc name )
196         throws IllegalArgumentException JavaDoc
197     {
198         checkValid( object, name);
199     }
200
201     /**
202       Check the String with the standard StringValidator
203       @param checkMe a String to check
204       @param name The name of the String
205       @throws IllegalArgumentException if checkMe is null or zero-length
206     */

207     public static void check(String JavaDoc checkMe, String JavaDoc name)
208         throws IllegalArgumentException JavaDoc
209    {
210         check(checkMe, name, StringValidator.getInstance());
211    }
212
213     /**
214       Check the String with a custom StringValidator
215       @param checkMe a String to check
216       @param minimumLength The minimum acceptable length of the String to allow
217       @param name The name of the String
218       @throws IllegalArgumentException if checkMe is null or zero-length
219     */

220     public static void check(String JavaDoc checkMe, int minimumLength, String JavaDoc name)
221         throws IllegalArgumentException JavaDoc
222     {
223         check(checkMe, name, new StringValidator(minimumLength));
224     }
225 }
226
Popular Tags