KickJava   Java API By Example, From Geeks To Geeks.

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


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 /*
25  * StringValidator.java
26  *
27  * Created on April 12, 2001, 10:42 PM
28  */

29 package com.sun.enterprise.admin.util;
30
31 /**
32  * Class for validating String method parameters. Current implementation's
33  * stock object (getInstance()) verifies that the String exists and has length > 0
34  * @author bnevins
35  * @version $Revision: 1.3 $
36  */

37
38 public class StringValidator extends BaseValidator
39 {
40     private final int mMininumLength;
41     private static final int kDefaultMinimumLength = 1;
42     private static StringValidator sDefaultInstance = null;
43     private static final String JavaDoc badArgMessage =
44         "You can't call StringValidator.validate() with a non-String argument";
45
46     /** Create a StringValidator
47      * @param minimumLength The String is invalid if its length is less than this
48      */

49     public StringValidator(int minimumLength)
50     {
51         Assert.assertRange(minimumLength, 0, Integer.MAX_VALUE, "minimumLength");
52         mMininumLength = minimumLength;
53     }
54
55     /** Get the standard StringValidator
56      * @return A class variable with a default minimum length of 1
57      */

58     public static Validator getInstance()
59     {
60         // lazy creation...
61
if(sDefaultInstance == null)
62         {
63             sDefaultInstance = new StringValidator(kDefaultMinimumLength);
64         }
65         return sDefaultInstance;
66     }
67
68     /** Validate a String
69      * @param obj The String to be validated
70      * @return ValidatorResult is invalid if the String's length was
71      * less than the minimum required length
72      */

73     public ValidatorResult validate(Object JavaDoc obj)
74     {
75         ValidatorResult result = super.validate(obj);
76
77         if (result.isValid())
78         {
79             Assert.assertit( (obj instanceof String JavaDoc), badArgMessage);
80
81             final String JavaDoc str = (String JavaDoc)obj;
82             final int len = str.length();
83
84             if(len < mMininumLength)
85             {
86                 result = makeBadResult(len);
87             }
88         }
89         return result;
90     }
91
92     private ValidatorResult makeBadResult(final int len)
93     {
94         return new ValidatorResult(false,
95                         "The String argument is invalid. The minimum required length is " +
96                         mMininumLength + " and the String's actual length is " + len);
97     }
98 }
Popular Tags