001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.common.internal; 014 015import java.text.*; 016 017/** 018 * Method argument utility methods. 019 */ 020public class ArgUtils { 021 022 /** 023 * Throws an {@link IllegalArgumentException} if the specified argument is <jk>null</jk>. 024 * 025 * <h5 class='section'>Example:</h5> 026 * <p class='bjava'> 027 * <jk>import static</jk> org.apache.juneau.internal.ArgUtils.*; 028 * 029 * <jk>public</jk> String setFoo(String <jv>foo</jv>) { 030 * <jsm>assertArgNotNull</jsm>(<js>"foo"</js>, <jv>foo</jv>); 031 * ... 032 * } 033 * </p> 034 * 035 * @param <T> The argument data type. 036 * @param name The argument name. 037 * @param o The object to check. 038 * @return The same argument. 039 * @throws IllegalArgumentException Constructed exception. 040 */ 041 public static final <T> T assertArgNotNull(String name, T o) throws IllegalArgumentException { 042 assertArg(o != null, "Argument ''{0}'' cannot be null.", name); 043 return o; 044 } 045 046 /** 047 * Throws an {@link IllegalArgumentException} if the specified expression is <jk>false</jk>. 048 * 049 * <h5 class='section'>Example:</h5> 050 * <p class='bjava'> 051 * <jk>import static</jk> org.apache.juneau.internal.ArgUtils.*; 052 * 053 * <jk>public</jk> String setFoo(List<String> <jv>foo</jv>) { 054 * <jsm>assertArg</jsm>(<jv>foo</jv> != <jk>null</jk> && ! <jv>foo</jv>.isEmpty(), <js>"'foo' cannot be null or empty."</js>); 055 * ... 056 * } 057 * </p> 058 * 059 * @param expression The boolean expression to check. 060 * @param msg The exception message. 061 * @param args The exception message args. 062 * @throws IllegalArgumentException Constructed exception. 063 */ 064 public static final void assertArg(boolean expression, String msg, Object...args) throws IllegalArgumentException { 065 if (! expression) 066 throw new IllegalArgumentException(MessageFormat.format(msg, args)); 067 } 068 069 /** 070 * Throws an {@link IllegalArgumentException} if the specified value doesn't have all subclasses of the specified type. 071 * 072 * @param <E> The element type. 073 * @param name The argument name. 074 * @param type The expected parent class. 075 * @param value The array value being checked. 076 * @return The value cast to the specified array type. 077 * @throws IllegalArgumentException Constructed exception. 078 */ 079 @SuppressWarnings("unchecked") 080 public static final <E> Class<E>[] assertClassArrayArgIsType(String name, Class<E> type, Class<?>[] value) throws IllegalArgumentException { 081 for (int i = 0; i < value.length; i++) 082 if (! type.isAssignableFrom(value[i])) 083 throw new IllegalArgumentException("Arg "+name+" did not have arg of type "+type.getName()+" at index "+i+": "+value[i].getName()); 084 return (Class<E>[])value; 085 } 086 087}