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 */ 017package org.apache.wicket.util.lang; 018 019import java.util.Collection; 020 021import org.apache.wicket.util.string.Strings; 022 023/** 024 * Class with methods for asserting conditions on arguments. 025 */ 026public class Args 027{ 028 /** 029 * Checks argument is not null 030 * 031 * @param <T> 032 * @param argument 033 * @param name 034 * @return The 'argument' parameter 035 * @throws IllegalArgumentException 036 */ 037 public static <T> T notNull(final T argument, final String name) 038 { 039 if (argument == null) 040 { 041 throw new IllegalArgumentException("Argument '" + name + "' may not be null."); 042 } 043 return argument; 044 } 045 046 /** 047 * Checks argument is not empty (not null and has a non-whitespace character) 048 * 049 * @param <T> 050 * the type of the argument to check for emptiness 051 * 052 * @param argument 053 * the argument to check for emptiness 054 * @param name 055 * the name to use in the error message 056 * @return The {@code argument} parameter if not empty 057 * @throws IllegalArgumentException 058 * when the passed {@code argument} is empty 059 */ 060 public static <T extends CharSequence> T notEmpty(final T argument, final String name) 061 { 062 if (Strings.isEmpty(argument)) 063 { 064 throw new IllegalArgumentException("Argument '" + name + "' may not be null or empty."); 065 } 066 return argument; 067 } 068 069 /** 070 * Checks argument is not empty (not null and has a non-whitespace character) 071 * 072 * Note: This method overloads {@link #notEmpty(CharSequence, String)} for performance reasons. 073 * 074 * @param argument 075 * the argument to check for emptiness 076 * @param name 077 * the name to use in the error message 078 * @return The {@code argument} parameter if not empty 079 * @throws IllegalArgumentException 080 * when the passed {@code argument} is empty 081 */ 082 public static String notEmpty(final String argument, final String name) 083 { 084 if (Strings.isEmpty(argument)) 085 { 086 throw new IllegalArgumentException("Argument '" + name + "' may not be null or empty."); 087 } 088 return argument; 089 } 090 091 092 /** 093 * 094 * Checks argument is not null or empty 095 * 096 * @param collection 097 * @param message 098 * @param params 099 * @return the {code collection} 100 * @throws IllegalArgumentException 101 * if the passed collection is either null or empty 102 */ 103 public static <T extends Collection<?>> T notEmpty(final T collection, final String message, 104 final Object... params) 105 { 106 if (collection == null || collection.isEmpty()) 107 { 108 throw new IllegalArgumentException(Args.format(message, params)); 109 } 110 return collection; 111 } 112 113 /** 114 * Checks argument is not null or empty 115 * 116 * @param collection 117 * @param name 118 * @return the {code collection} 119 * @throws IllegalArgumentException 120 * if the passed collection is either null or empty 121 */ 122 public static <T extends Collection<?>> T notEmpty(final T collection, final String name) 123 { 124 return notEmpty(collection, "Collection '%s' may not be null or empty.", name); 125 } 126 127 /** 128 * Checks if argument is within a range 129 * 130 * @param <T> 131 * @param min 132 * @param max 133 * @param value 134 * @param name 135 * @return the {code value} 136 * @throws IllegalArgumentException 137 */ 138 public static <T extends Comparable<? super T>> T withinRange(final T min, final T max, 139 final T value, final String name) 140 { 141 notNull(min, name); 142 notNull(max, name); 143 if ((value.compareTo(min) < 0) || (value.compareTo(max) > 0)) 144 { 145 throw new IllegalArgumentException( 146 String.format("Argument '%s' must have a value within [%s,%s], but was %s", name, 147 min, max, value)); 148 } 149 return value; 150 } 151 152 /** 153 * Check if argument is true 154 * 155 * @param argument 156 * @param msg 157 * @param params 158 * @return argument 159 */ 160 public static boolean isTrue(final boolean argument, final String msg, final Object... params) 161 { 162 if (argument == false) 163 { 164 throw new IllegalArgumentException(format(msg, params)); 165 } 166 return argument; 167 } 168 169 /** 170 * Check if argument is false 171 * 172 * @param argument 173 * @param msg 174 * @param params 175 * @return argument 176 */ 177 public static boolean isFalse(final boolean argument, final String msg, final Object... params) 178 { 179 if (argument == true) 180 { 181 throw new IllegalArgumentException(format(msg, params)); 182 } 183 return argument; 184 } 185 186 /** 187 * Format the message. Allow for "{}" as well as %s etc. (see Formatter). 188 * 189 * @param msg 190 * @param params 191 * @return formatted message 192 */ 193 static String format(String msg, final Object... params) 194 { 195 msg = msg.replace("{}", "%s"); 196 return String.format(msg, params); 197 } 198}