01: // Copyright 2006 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.ioc.internal.util;
16:
17: /**
18: * Static utility methods for defensive programming.
19: */
20: public final class Defense {
21: private Defense() {
22: }
23:
24: /**
25: * Checks that a method parameter value is not null, and returns it. This method is used in
26: * situations where some of the parameters to a method are allowed to be null and other's arent.
27: * In that situation, the method will be annotated with {@link SuppressNullCheck}, and the
28: * relevent null checks will occur inside the method implementation.
29: *
30: * @param <T>
31: * the value type
32: * @param value
33: * the value (which is checked to ensure non-nullness)
34: * @param parameterName
35: * the name of the parameter, used for exception messages
36: * @return the value
37: * @throws IllegalArgumentException
38: * if the value is null
39: */
40: public static <T> T notNull(T value, String parameterName) {
41: if (value == null)
42: throw new IllegalArgumentException(UtilMessages
43: .parameterWasNull(parameterName));
44:
45: return value;
46: }
47:
48: /**
49: * Checks that a parameter value is not null and not empty.
50: *
51: * @param value
52: * value to check (which is returned)
53: * @param parameterName
54: * the name of the parameter, used for exception messages
55: * @return the value, trimmed, if non-blank
56: * @throws IllegalArgumentException
57: * if the value is null or empty
58: */
59: public static String notBlank(String value, String parameterName) {
60: if (value != null) {
61: String trimmedValue = value.trim();
62:
63: if (!trimmedValue.equals(""))
64: return trimmedValue;
65: }
66:
67: throw new IllegalArgumentException(UtilMessages
68: .parameterWasBlank(parameterName));
69: }
70:
71: /**
72: * Checks that the provided value is not null, and may be cast to the desired type.
73: *
74: * @param <T>
75: * @param parameterValue
76: * @param type
77: * @param parameterName
78: * @throws IllegalArgumentException
79: * if the value is null, or is not assignable to the indicated type
80: * @return the casted value
81: */
82: public static <T> T cast(Object parameterValue, Class<T> type,
83: String parameterName) {
84: notNull(parameterValue, parameterName);
85:
86: if (!type.isInstance(parameterValue))
87: throw new IllegalArgumentException(UtilMessages.badCast(
88: parameterName, parameterValue, type));
89:
90: return type.cast(parameterValue);
91: }
92: }
|