KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > Parameters


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.util;
21
22 /**
23  * Utilities for checking the values of method parameters.
24  *
25  * Methods in this class generally take the name of
26  * the parameter to check and its value and throw exceptions
27  * with messages according to the method name or just return. For example,
28  * if you have a <code>myMethod()</code> method taking a <code>myParam</code>
29  * parameter whose value must be a Java identifier, you usually check that
30  * by doing:
31  *
32  * <pre>
33  * public void myMethod(String myParam) {
34  * if (!Utilities.isJavaIdentifier(myParam)) {
35  * throw new IllegalArgumentException("The myParam parameter is not a valid Java identifier");
36  * }
37  * }
38  * </pre>
39  *
40  * Using this class you can do the same is a simpler way:
41  *
42  * <pre>
43  * public void myMethod(String myParam) {
44  * Parameters.javaIdentifier("myParam", myParam);
45  * }
46  * </pre>
47  *
48  * @author Andrei Badea
49  * @since org.openide.util 7.6
50  */

51 public class Parameters {
52
53     private Parameters() {}
54
55     /**
56      * Asserts the parameter value is not <code>null</code>.
57      *
58      * @param name the parameter name.
59      * @param value the parameter value.
60      * @throws NullPointerException if the parameter value is <code>null</code>.
61      */

62     public static void notNull(CharSequence JavaDoc name, Object JavaDoc value) {
63         if (value == null) {
64             throw new NullPointerException JavaDoc("The " + name + " parameter cannot be null"); // NOI18N
65
}
66     }
67
68     /**
69      * Asserts the parameter value is neither <code>null</code> nor an empty
70      * character sequence.
71      *
72      * @param name the parameter name.
73      * @param value the parameter value.
74      * @throws NullPointerException if the parameter value is <code>null</code>.
75      * @throws IllegalArgumentException if the parameter value is an empty
76      * character sequence.
77      */

78     public static void notEmpty(CharSequence JavaDoc name, CharSequence JavaDoc value) {
79         notNull(name, value);
80         if (value.length() == 0) {
81             throw new IllegalArgumentException JavaDoc("The " + name + " parameter cannot be an empty character sequence"); // NOI18N
82
}
83     }
84
85     /**
86      * Asserts the parameter value is not <code>null</code> and it contains
87      * at least one non-whitespace character. Whitespace is defined as by
88      * {@link String#trim}.
89      *
90      * @param name the parameter name.
91      * @param value the parameter value.
92      * @throws NullPointerException if the parameter value is <code>null</code>.
93      * @throws IllegalArgumentException if the parameter value does not
94      * contain at least one non-whitespace character.
95      */

96     public static void notWhitespace(CharSequence JavaDoc name, CharSequence JavaDoc value) {
97         notNull(name, value);
98         if (value.toString().trim().length() == 0) {
99             throw new IllegalArgumentException JavaDoc("The " + name + " parameter must contain at least one non-whitespace character"); // NOI18N
100
}
101     }
102
103     /**
104      * Asserts the parameter value is not <code>null</code> and it is
105      * a Java identifier.
106      *
107      * @param name the parameter name.
108      * @param value the parameter value.
109      * @throws NullPointerException if the parameter value is <code>null</code>.
110      * @throws IllegalArgumentException if the parameter value is not
111      * a Java identifier.
112      */

113     public static void javaIdentifier(CharSequence JavaDoc name, CharSequence JavaDoc value) {
114         notNull(name, value);
115         javaIdentifierOrNull(name, value);
116     }
117
118     /**
119      * Asserts the parameter value is either <code>null</code> or a Java
120      * identifier.
121      *
122      * @param name the parameter name.
123      * @param value the parameter value.
124      * @throws IllegalArgumentException if the parameter value is neither
125      * <code>null</code> nor a Java identifier.
126      */

127     public static void javaIdentifierOrNull(CharSequence JavaDoc name, CharSequence JavaDoc value) {
128         if (value != null && !Utilities.isJavaIdentifier(value.toString())) {
129             throw new IllegalArgumentException JavaDoc("The " + name + " parameter ('" + value + "') is not a valid Java identifier"); // NOI18N
130
}
131     }
132 }
133
Popular Tags