KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > http > client > StringValidator


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.http.client;
17
18 /**
19  * Utility class for validating strings.
20  *
21  * TODO(mmendez): Is there a better place for this?
22  */

23 final class StringValidator {
24   /**
25    * Returns true if the string is empty or null.
26    *
27    * @param string to test if null or empty
28    *
29    * @return true if the string is empty or null
30    */

31   public static boolean isEmptyOrNullString(String JavaDoc string) {
32     return (string == null) || (0 == string.trim().length());
33   }
34
35   /**
36    * Throws if <code>value</code> is <code>null</code> or empty. This method
37    * ignores leading and trailing whitespace.
38    *
39    * @param name the name of the value, used in error messages
40    * @param value the string value that needs to be validated
41    *
42    * @throws IllegalArgumentException if the string is empty, or all whitespace
43    * @throws NullPointerException if the string is <code>null</code>
44    */

45   public static void throwIfEmptyOrNull(String JavaDoc name, String JavaDoc value) {
46     assert (name != null);
47     assert (name.trim().length() != 0);
48
49     throwIfNull(name, value);
50
51     if (0 == value.trim().length()) {
52       throw new IllegalArgumentException JavaDoc(name + " can not be empty");
53     }
54   }
55
56   /**
57    * Throws a {@link NullPointerException} if the value is <code>null</code>.
58    *
59    * @param name the name of the value, used in error messages
60    * @param value the string value that needs to be validated
61    *
62    * @throws NullPointerException if the value is <code>null</code>
63    */

64   public static void throwIfNull(String JavaDoc name, String JavaDoc value) {
65     if (null == value) {
66       throw new NullPointerException JavaDoc(name + " can not be null");
67     }
68   }
69
70   private StringValidator() {
71   }
72 }
73
Popular Tags