01: /*
02: * Copyright 2006 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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 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 name, String 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(name
53: + " can not be empty");
54: }
55: }
56:
57: /**
58: * Throws a {@link NullPointerException} if the value is <code>null</code>.
59: *
60: * @param name the name of the value, used in error messages
61: * @param value the string value that needs to be validated
62: *
63: * @throws NullPointerException if the value is <code>null</code>
64: */
65: public static void throwIfNull(String name, String value) {
66: if (null == value) {
67: throw new NullPointerException(name + " can not be null");
68: }
69: }
70:
71: private StringValidator() {
72: }
73: }
|