1 /* 2 * Copyright 2011-2012 smartics, Kronseder & Reiner GmbH 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package de.smartics.util.lang; 17 18 import org.apache.commons.lang.StringUtils; 19 20 /** 21 * Utility class to check arguments. 22 */ 23 public final class Arguments 24 { 25 // ********************************* Fields ********************************* 26 27 // --- constants ------------------------------------------------------------ 28 29 // --- members -------------------------------------------------------------- 30 31 // ****************************** Initializer ******************************* 32 33 // ****************************** Constructors ****************************** 34 35 /** 36 * Utility class pattern. 37 */ 38 private Arguments() 39 { 40 } 41 42 // ****************************** Inner Classes ***************************** 43 44 // ********************************* Methods ******************************** 45 46 // --- init ----------------------------------------------------------------- 47 48 // --- get&set -------------------------------------------------------------- 49 50 // --- business ------------------------------------------------------------- 51 52 /** 53 * Checks if {@code value} is <code>null</code>. 54 * 55 * @param name the name of the argument of error reporting. Not used if no 56 * exception is thrown. May be <code>null</code>, although not 57 * recommended. 58 * @param value the value of the argument to check to be not <code>null</code> 59 * . 60 * @throws NullPointerException if {@code value} is <code>null</code>. 61 */ 62 public static void checkNotNull(final String name, final Object value) 63 throws NullPointerException 64 { 65 checkNotNull(name, value, null); 66 } 67 68 /** 69 * Checks if {@code value} is <code>null</code> providing an additional 70 * message. 71 * 72 * @param name the name of the argument of error reporting. Not used if no 73 * exception is thrown. May be <code>null</code>, although not 74 * recommended. 75 * @param value the value of the argument to check to be not <code>null</code> 76 * . 77 * @param message the message to pass to the exception as additional 78 * information to the standard message being generated. 79 * @throws NullPointerException if {@code value} is <code>null</code>. 80 */ 81 public static void checkNotNull(final String name, final Object value, 82 final String message) throws NullPointerException 83 { 84 if (value == null) 85 { 86 throw new NullArgumentException(name, message); 87 } 88 } 89 90 /** 91 * Checks if {@code value} is blank. 92 * 93 * @param name the name of the argument of error reporting. Not used if no 94 * exception is thrown. May be <code>null</code>, although not 95 * recommended. 96 * @param value the value of the argument to check to be not blank. 97 * @throws BlankArgumentException if {@code value} is blank. 98 */ 99 public static void checkNotBlank(final String name, final String value) 100 throws BlankArgumentException 101 { 102 checkNotBlank(name, value, null); 103 } 104 105 /** 106 * Checks if {@code value} is blank providing an additional message. 107 * 108 * @param name the name of the argument of error reporting. Not used if no 109 * exception is thrown. May be <code>null</code>, although not 110 * recommended. 111 * @param value the value of the argument to check to be not blank. 112 * @param message the message to pass to the exception as additional 113 * information to the standard message being generated. 114 * @throws BlankArgumentException if {@code value} is blank. 115 */ 116 public static void checkNotBlank(final String name, final String value, 117 final String message) throws BlankArgumentException 118 { 119 if (StringUtils.isBlank(value)) 120 { 121 throw new BlankArgumentException(name, message); 122 } 123 } 124 125 /** 126 * Checks if {@code value} is blank except <code>null</code>. 127 * 128 * @param name the name of the argument of error reporting. Not used if no 129 * exception is thrown. May be <code>null</code>, although not 130 * recommended. 131 * @param value the value of the argument to check to be not blank but may be 132 * <code>null</code>. 133 * @throws BlankExceptNullArgumentException if {@code value} is blank and not 134 * <code>null</code>. 135 */ 136 public static void checkNotBlankExceptNull(final String name, 137 final String value) throws BlankExceptNullArgumentException 138 { 139 checkNotBlankExceptNull(name, value, null); 140 } 141 142 /** 143 * Checks if {@code value} is blank except <code>null</code> providing an 144 * additional message. 145 * 146 * @param name the name of the argument of error reporting. Not used if no 147 * exception is thrown. May be <code>null</code>, although not 148 * recommended. 149 * @param value the value of the argument to check to be not blank but may be 150 * <code>null</code>. 151 * @param message the message to pass to the exception as additional 152 * information to the standard message being generated. 153 * @throws BlankExceptNullArgumentException if {@code value} is blank and not 154 * <code>null</code>. 155 */ 156 public static void checkNotBlankExceptNull(final String name, 157 final String value, final String message) 158 throws BlankExceptNullArgumentException 159 { 160 if (value != null && StringUtils.isBlank(value)) 161 { 162 throw new BlankArgumentException(name, message); 163 } 164 } 165 // --- object basics -------------------------------------------------------- 166 167 }