001/* 002 * Licensed under the Apache License, Version 2.0 (the "License"); 003 * you may not use this file except in compliance with the License. 004 * You may obtain a copy of the License at 005 * 006 * http://www.apache.org/licenses/LICENSE-2.0 007 * 008 * Unless required by applicable law or agreed to in writing, software 009 * distributed under the License is distributed on an "AS IS" BASIS, 010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 011 * See the License for the specific language governing permissions and 012 * limitations under the License. 013 */ 014package org.gbif.ws.util.spring; 015 016import org.apache.commons.lang3.StringUtils; 017 018/** 019 * Assertion utility class that assists in validating arguments. 020 * Useful for identifying programmer errors early and clearly at runtime. 021 * <p>For example, if the contract of a public method states it does not 022 * allow <code>null</code> arguments, Assert can be used to validate that 023 * contract. Doing this clearly indicates a contract violation when it 024 * occurs and protects the class's invariants. 025 * <p>Typically used to validate method arguments rather than configuration 026 * properties, to check for cases that are usually programmer errors rather than 027 * configuration errors. In contrast to config initialization code, there is 028 * usally no point in falling back to defaults in such methods. 029 * <p>This class is similar to JUnit's assertion library. If an argument value is 030 * deemed invalid, an {@link IllegalArgumentException} is thrown (typically). 031 * For example: 032 * <pre class="code"> 033 * Assert.notNull(clazz, "The class must not be null"); 034 * Assert.isTrue(i > 0, "The value must be greater than zero");</pre> 035 * Mainly for internal use within the framework; consider Jakarta's Commons Lang 036 * >= 2.0 for a more comprehensive suite of assertion utilities. 037 * 038 * @author Keith Donald 039 * @author Juergen Hoeller 040 * @author Colin Sampaleanu 041 * @author Rob Harrop 042 * @since 1.1.2 043 */ 044public abstract class Assert { 045 046 /** 047 * Assert a boolean expression, throwing <code>IllegalArgumentException</code> 048 * if the test result is <code>false</code>. 049 * <pre class="code">Assert.isTrue(i > 0, "The value must be greater than zero");</pre> 050 * 051 * @param expression a boolean expression 052 * @param message the exception message to use if the assertion fails 053 * 054 * @throws IllegalArgumentException if expression is <code>false</code> 055 */ 056 public static void isTrue(boolean expression, String message) { 057 if (!expression) { 058 throw new IllegalArgumentException(message); 059 } 060 } 061 062 /** 063 * Assert a boolean expression, throwing <code>IllegalArgumentException</code> 064 * if the test result is <code>false</code>. 065 * <pre class="code">Assert.isTrue(i > 0);</pre> 066 * 067 * @param expression a boolean expression 068 * 069 * @throws IllegalArgumentException if expression is <code>false</code> 070 */ 071 public static void isTrue(boolean expression) { 072 isTrue(expression, "[Assertion failed] - this expression must be true"); 073 } 074 075 /** 076 * Assert that an object is <code>null</code> . 077 * <pre class="code">Assert.isNull(value, "The value must be null");</pre> 078 * 079 * @param object the object to check 080 * @param message the exception message to use if the assertion fails 081 * 082 * @throws IllegalArgumentException if the object is not <code>null</code> 083 */ 084 public static void isNull(Object object, String message) { 085 if (object != null) { 086 throw new IllegalArgumentException(message); 087 } 088 } 089 090 /** 091 * Assert that an object is <code>null</code> . 092 * <pre class="code">Assert.isNull(value);</pre> 093 * 094 * @param object the object to check 095 * 096 * @throws IllegalArgumentException if the object is not <code>null</code> 097 */ 098 public static void isNull(Object object) { 099 isNull(object, "[Assertion failed] - the object argument must be null"); 100 } 101 102 /** 103 * Assert that an object is not <code>null</code> . 104 * <pre class="code">Assert.notNull(clazz, "The class must not be null");</pre> 105 * 106 * @param object the object to check 107 * @param message the exception message to use if the assertion fails 108 * 109 * @throws IllegalArgumentException if the object is <code>null</code> 110 */ 111 public static void notNull(Object object, String message) { 112 if (object == null) { 113 throw new IllegalArgumentException(message); 114 } 115 } 116 117 /** 118 * Assert that an object is not <code>null</code> . 119 * <pre class="code">Assert.notNull(clazz);</pre> 120 * 121 * @param object the object to check 122 * 123 * @throws IllegalArgumentException if the object is <code>null</code> 124 */ 125 public static void notNull(Object object) { 126 notNull(object, "[Assertion failed] - this argument is required; it must not be null"); 127 } 128 129 /** 130 * Assert that the given String has valid text content; that is, it must not 131 * be <code>null</code> and must contain at least one non-whitespace character. 132 * <pre class="code">Assert.hasText(name, "'name' must not be empty");</pre> 133 * 134 * @param text the String to check 135 * @param message the exception message to use if the assertion fails 136 */ 137 public static void hasText(String text, String message) { 138 if (StringUtils.isEmpty(text)) { 139 throw new IllegalArgumentException(message); 140 } 141 } 142 143 /** 144 * Assert that the given String has valid text content; that is, it must not 145 * be <code>null</code> and must contain at least one non-whitespace character. 146 * <pre class="code">Assert.hasText(name, "'name' must not be empty");</pre> 147 * 148 * @param text the String to check 149 */ 150 public static void hasText(String text) { 151 hasText( 152 text, 153 "[Assertion failed] - this String argument must have text; it must not be <code>null</code>, empty, or blank"); 154 } 155}