KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > util > ParameterCheck


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.util;
18
19 import java.util.Collection JavaDoc;
20
21 /**
22  * Utility class to perform various common parameter checks
23  *
24  * @author gavinc
25  */

26 public class ParameterCheck
27 {
28    /**
29     * Checks that the parameter with the given name has content i.e. it is not null
30     *
31     * @param strParamName Name of parameter to check
32     * @param object Value of the parameter to check
33     */

34    public static void mandatory(String JavaDoc strParamName, Object JavaDoc object)
35    {
36       if (strParamName == null || strParamName.length() == 0)
37       {
38          throw new IllegalArgumentException JavaDoc("Parameter name is mandatory");
39       }
40       
41       // check that the given string value has content
42
if (object == null)
43       {
44          throw new IllegalArgumentException JavaDoc(strParamName + " is a mandatory parameter");
45       }
46    }
47    
48    /**
49     * Checks that the string parameter with the given name has content
50     * i.e. it is not null and not zero length
51     *
52     * @param strParamName Name of parameter to check
53     * @param strParamValue Value of the parameter to check
54     */

55    public static void mandatoryString(String JavaDoc strParamName, String JavaDoc strParamValue)
56    {
57       if (strParamName == null || strParamName.length() == 0)
58       {
59          throw new IllegalArgumentException JavaDoc("Parameter name is mandatory");
60       }
61       
62       // check that the given string value has content
63
if (strParamValue == null || strParamValue.length() == 0)
64       {
65          throw new IllegalArgumentException JavaDoc(strParamName + " is a mandatory parameter");
66       }
67    }
68    
69    
70    /**
71     * Checks that the collection parameter contains at least one item.
72     *
73     * @param strParamName Name of parameter to check
74     * @param coll collection to check
75     */

76    public static void mandatoryCollection(String JavaDoc strParamName, Collection JavaDoc coll)
77    {
78       if (strParamName == null || strParamName.length() == 0)
79       {
80          throw new IllegalArgumentException JavaDoc("Parameter name is mandatory");
81       }
82       
83       if (coll == null || coll.size() == 0)
84       {
85          throw new IllegalArgumentException JavaDoc(strParamName + " collection must contain at least one item");
86       }
87    }
88
89 }
90
Popular Tags