001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.util;
018:
019: /**
020: * <p>
021: * ArgUtil
022: * </p>
023: *
024: * Misc. utilities for rudimentary argument validation
025: *
026: * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
027: * @version $Id: ArgUtil.java 516448 2007-03-09 16:25:47Z ate $
028: *
029: */
030: public final class ArgUtil {
031: private static final String MSG_1 = "Argument \"";
032: private static final String MSG_2 = "\" cannot be null for method ";
033:
034: /**
035: *
036: * @throws java.lang.IllegalArgumentException If ANY of the arguments are <code>null</code>
037: * @param args array of arguments to validate as not nul
038: * @param argNames array of arguments names, idexes should match with args.
039: * @param methodName Name of method we are validating arguments for.
040: */
041: public static void notNull(Object[] args, String[] argNames,
042: String methodName) {
043: for (int i = 0; i < args.length; i++) {
044: Object arg = args[i];
045: if (arg == null) {
046: StringBuffer buf = new StringBuffer(150);
047: String argName = null;
048: if (i < argNames.length) {
049: argName = argNames[i];
050: } else {
051: argName = String.valueOf(i);
052: }
053:
054: buf.append(MSG_1 + argName + MSG_2 + methodName);
055: throw new IllegalArgumentException(buf.toString());
056: }
057: }
058: }
059:
060: /**
061: *
062: * <p>
063: * notNull
064: * </p>
065: *
066: * @param nonNullObject
067: * @param thisObject
068: * @throws IllegalArgumentException
069: */
070: public static final void assertNotNull(Class nonNullClass,
071: Object nonNullObject, Object this Object)
072: throws IllegalArgumentException {
073: if (nonNullObject == null) {
074: throw new IllegalArgumentException(this Object.getClass()
075: .getName()
076: + " requires a non-null "
077: + nonNullClass.getName()
078: + " as an argument.");
079: }
080: }
081:
082: public static final void assertNotNull(Class nonNullClass,
083: Object nonNullObject, Object this Object, String methodName)
084: throws IllegalArgumentException {
085: if (nonNullObject == null) {
086: throw new IllegalArgumentException(this Object.getClass()
087: .getName()
088: + "."
089: + methodName
090: + " requires a non-null "
091: + nonNullClass.getName() + " as an argument.");
092: }
093: }
094:
095: public static final void assertPropertyNotNull(
096: Object nonNullObject, Object this Object, String methodName,
097: String property) throws IllegalArgumentException {
098: if (nonNullObject == null) {
099: throw new IllegalStateException(this Object.getClass()
100: .getName()
101: + "."
102: + methodName
103: + " cannot be invoked until the property "
104: + property + " has been set.");
105: }
106: }
107: }
|