1 /* 2 * Copyright 2011-2013 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 /** 19 * Utility class to check an argument. 20 */ 21 public final class Arg 22 { 23 // ********************************* Fields ********************************* 24 25 // --- constants ------------------------------------------------------------ 26 27 // --- members -------------------------------------------------------------- 28 29 // ****************************** Initializer ******************************* 30 31 // ****************************** Constructors ****************************** 32 33 /** 34 * Utility class pattern. 35 */ 36 private Arg() 37 { 38 } 39 40 // ****************************** Inner Classes ***************************** 41 42 // ********************************* Methods ******************************** 43 44 // --- init ----------------------------------------------------------------- 45 46 // --- get&set -------------------------------------------------------------- 47 48 // --- business ------------------------------------------------------------- 49 50 /** 51 * Checks if {@code value} is <code>null</code>. 52 * 53 * @param name the name of the argument of error reporting. Not used if no 54 * exception is thrown. May be <code>null</code>, although not 55 * recommended. 56 * @param value the value of the argument to check to be not {@code null}. 57 * @return the passed in {@code value}. 58 * @throws NullPointerException if {@code value} is <code>null</code>. 59 * @param <T> the type of the argument to check. 60 */ 61 public static <T> T checkNotNull(final String name, final T value) 62 throws NullPointerException 63 { 64 checkNotNull(name, value, null); 65 return value; 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}. 76 * @param message the message to pass to the exception as additional 77 * information to the standard message being generated. 78 * @return the passed in {@code value}. 79 * @throws NullPointerException if {@code value} is <code>null</code>. 80 * @param <T> the type of the argument to check. 81 */ 82 public static <T> T checkNotNull(final String name, final T value, 83 final String message) throws NullPointerException 84 { 85 if (value == null) 86 { 87 throw new NullArgumentException(name, message); 88 } 89 90 return value; 91 } 92 93 /** 94 * Checks if {@code value} is blank. 95 * 96 * @param name the name of the argument of error reporting. Not used if no 97 * exception is thrown. May be <code>null</code>, although not 98 * recommended. 99 * @param value the value of the argument to check to be not blank. 100 * @return the passed in {@code value}. 101 * @throws NullArgumentException if {@code value} is <code>null</code>. 102 * @throws BlankArgumentException if {@code value} is blank. 103 * @param <T> a character sequence like a {@link String}. 104 */ 105 public static <T extends CharSequence> T checkNotBlank(final String name, 106 final T value) throws NullArgumentException, BlankArgumentException 107 { 108 checkNotBlank(name, value, null); 109 return value; 110 } 111 112 /** 113 * Checks if {@code value} is blank providing an additional message. 114 * 115 * @param name the name of the argument of error reporting. Not used if no 116 * exception is thrown. May be <code>null</code>, although not 117 * recommended. 118 * @param value the value of the argument to check to be not blank. 119 * @param message the message to pass to the exception as additional 120 * information to the standard message being generated. 121 * @return the passed in {@code value}. 122 * @throws NullArgumentException if {@code value} is <code>null</code>. 123 * @throws BlankArgumentException if {@code value} is blank. 124 * @param <T> a character sequence like a {@link String}. 125 */ 126 public static <T extends CharSequence> T checkNotBlank(final String name, 127 final T value, final String message) throws NullArgumentException, 128 BlankArgumentException 129 { 130 if (value == null) 131 { 132 throw new NullArgumentException(name, message); 133 } 134 if (isBlank(value)) 135 { 136 throw new BlankArgumentException(name, message); 137 } 138 139 return value; 140 } 141 142 /** 143 * Checks if {@code value} is blank except <code>null</code>. 144 * 145 * @param name the name of the argument of error reporting. Not used if no 146 * exception is thrown. May be <code>null</code>, although not 147 * recommended. 148 * @param value the value of the argument to check to be not blank but may be 149 * <code>null</code>. 150 * @return the passed in {@code value}. 151 * @throws BlankExceptNullArgumentException if {@code value} is blank and not 152 * <code>null</code>. 153 * @param <T> a character sequence like a {@link String}. 154 */ 155 public static <T extends CharSequence> T checkNotBlankExceptNull( 156 final String name, final T value) throws BlankExceptNullArgumentException 157 { 158 checkNotBlankExceptNull(name, value, null); 159 return value; 160 } 161 162 /** 163 * Checks if {@code value} is blank except <code>null</code> providing an 164 * additional message. 165 * 166 * @param name the name of the argument of error reporting. Not used if no 167 * exception is thrown. May be <code>null</code>, although not 168 * recommended. 169 * @param value the value of the argument to check to be not blank but may be 170 * <code>null</code>. 171 * @param message the message to pass to the exception as additional 172 * information to the standard message being generated. 173 * @return the passed in {@code value}. 174 * @throws BlankExceptNullArgumentException if {@code value} is blank and not 175 * <code>null</code>. 176 * @param <T> a character sequence like a {@link String}. 177 */ 178 public static <T extends CharSequence> T checkNotBlankExceptNull( 179 final String name, final T value, final String message) 180 throws BlankExceptNullArgumentException 181 { 182 if (value != null && isBlank(value)) 183 { 184 throw new BlankArgumentException(name, message); 185 } 186 return value; 187 } 188 189 // Copied from Apaches StringUtils and modified to cope with CharSequences. 190 // http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/StringUtils.html#isBlank%28java.lang.String%29 191 private static <T extends CharSequence> boolean isBlank(final T str) 192 { 193 // CHECKSTYLE:OFF 194 int strLen; 195 if (str == null || (strLen = str.length()) == 0) // NOPMD 196 { 197 return true; 198 } 199 for (int i = 0; i < strLen; i++) 200 { 201 if ((Character.isWhitespace(str.charAt(i)) == false)) // NOPMD 202 { 203 return false; 204 } 205 } 206 return true; 207 // CHECKSTYLE:ON 208 } 209 210 // --- object basics -------------------------------------------------------- 211 212 }