1 package net.sourceforge.argval; 2 3 import java.io.File; 4 import java.text.DateFormat; 5 import java.util.Collection; 6 import java.util.Map; 7 import java.util.Set; 8 9 import org.apache.oro.text.regex.Pattern; 10 11 public interface ArgumentValidation { 12 13 public final static String VALIDATION_TYPE_NAME_ARGUMENT = "Argument"; 14 public final static String VALIDATION_TYPE_NAME_PROPERTY_KEY = "Property-key"; 15 16 /** 17 * Creates a message about all the validated conditions which are not satisfied. 18 * 19 * @return The message containing the details about the not met conditions. 20 */ 21 String getMessage(); 22 23 /** 24 * Returns <code>true</code> if there are arguments checked (through one of the methods 25 * <code>isValidXxxxx</code>), that did not full fill the condition of that validation check, 26 * otherwise <code>false</code>. 27 * 28 * @return <code>true</code> if not all validated conditions are satisfied, otherwise 29 * <code>false</code>. 30 */ 31 boolean containsIllegalArgument(); 32 33 /** 34 * Creates an <code>IllegalArgumentException</code> with a detailed message about which 35 * arguments did not satisfy the required conditions. 36 * 37 * @return a new <code>IllegalArgumentException</code> 38 * 39 * @see #getMessage() 40 */ 41 IllegalArgumentException createIllegalArgumentException(); 42 43 /** 44 * Validates if the String <code>argumentValue</code> contains an instance reference 45 * (is not <code>null</code>). 46 * 47 * @param argumentName The name of the argument, which is validated. 48 * @param argumentValue The value of the argument, which is validated. 49 * @return <code>true</code> if the condition is met, otherwise <code>false</code>. 50 */ 51 boolean isValidWhenNotNull(String argumentName, Object argumentValue); 52 53 /** 54 * Validates if the String <code>argumentValue</code> contains the text <code>true</code> or 55 * <code>false</code>. 56 * 57 * @param argumentName The name of the argument, which is validated. 58 * @param argumentValue The value of the argument, which is validated. 59 * @return <code>true</code> if the condition is met, otherwise <code>false</code>. 60 */ 61 boolean isValidWhenBoolean(String argumentName, String argumentValue); 62 63 /** 64 * Validates if the String <code>argumentValue</code> is an Integer value. 65 * 66 * @param argumentName The name of the argument, which is validated. 67 * @param argumentValue The value of the argument, which is validated. 68 * @return <code>true</code> if the condition is met, otherwise <code>false</code>. 69 */ 70 boolean isValidWhenInteger(String argumentName, String argumentValue); 71 72 /** 73 * Validates if the String <code>argumentValue</code> is a Long value. 74 * 75 * @param argumentName The name of the argument, which is validated. 76 * @param argumentValue The value of the argument, which is validated. 77 * @return <code>true</code> if the condition is met, otherwise <code>false</code>. 78 */ 79 boolean isValidWhenLong(String argumentName, String argumentValue); 80 81 /** 82 * Validates if the String <code>argumentValue</code> is a directory. 83 * 84 * @param argumentName The name of the argument, which is validated. 85 * @param argumentValue The value of the argument, which is validated. 86 * @return <code>true</code> if the condition is met, otherwise <code>false</code>. 87 */ 88 boolean isValidWhenDirectory(String argumentName, String argumentValue); 89 boolean isValidWhenDirectory(String argumentName, File argumentValue); 90 91 /** 92 * Validates if the String <code>argumentValue</code> is a file. 93 * 94 * @param argumentName The name of the argument, which is validated. 95 * @param argumentValue The value of the argument, which is validated. 96 * @return <code>true</code> if the condition is met, otherwise <code>false</code>. 97 */ 98 boolean isValidWhenFile(String argumentName, String argumentValue); 99 boolean isValidWhenFile(String argumentName, File argumentValue); 100 101 /** 102 * Validates if the String <code>argumentValue</code> is an valid url. 103 * 104 * @param argumentName The name of the argument, which is validated. 105 * @param argumentValue The value of the argument, which is validated. 106 * @return <code>true</code> if the condition is met, otherwise <code>false</code>. 107 */ 108 boolean isValidWhenUrl(String argumentName, String argumentValue); 109 110 /** 111 * Validates if the String <code>argumentValue</code> is an valid uri. 112 * 113 * @param argumentName The name of the argument, which is validated. 114 * @param argumentValue The value of the argument, which is validated. 115 * @return <code>true</code> if the condition is met, otherwise <code>false</code>. 116 */ 117 boolean isValidWhenUri(String argumentName, String argumentValue); 118 119 /** 120 * Validates if the String <code>argumentValue</code> is an element contained by 121 * the Set <code>argumentSet</code>. 122 * 123 * @param argumentName The name of the argument, which is validated. 124 * @param argumentValue The value of the argument, which is validated. 125 * @param argumentSet The <code>Set</code> to validate against. 126 * @return <code>true</code> if the condition is met, otherwise <code>false</code>. 127 */ 128 boolean isValidWhenInSet(String argumentName, String argumentValue, Set<String> argumentSet); 129 130 /** 131 * Validates if the String <code>argumentValue</code> is not already contained by 132 * the Set <code>argumentSet</code>. 133 * 134 * @param argumentName The name of the argument, which is validated. 135 * @param argumentValue The value of the argument, which is validated. 136 * @param argumentSet The <code>Set</code> to validate against. 137 * @return <code>true</code> if the condition is met, otherwise <code>false</code>. 138 */ 139 boolean isValidWhenNotInSet(String argumentName, String argumentValue, Set<String> argumentSet); 140 141 boolean isValidWhenNotNullInCollection(String string, Collection<?> collection); 142 143 /** 144 * Validates if the Collection <code>argumentValue</code> is not null and that its 145 * containing elements are also not null. Meaning the Collection may be empty. And when not 146 * empty, each entry should contain a instance. 147 * 148 * @param argumentName The name of the argument, which is validated. 149 * @param argumentValue The value of the argument, which is validated. 150 * @return <code>true</code> if the condition is met, otherwise <code>false</code>. 151 */ 152 public boolean isValidCollectionWhenNoNulls(String argumentName, Collection<?> argumentValue); 153 154 /** 155 * Validates if the Collection <code>argumentValue</code> is at least containing the 156 * minimal number of elements (<code>minElements</code>). 157 * 158 * @param argumentName The name of the argument, which is validated. 159 * @param argumentValue The value of the argument, which is validated. 160 * @param minElements The minimal number of arguments, the collection should contain. 161 * @return <code>true</code> if the condition is met, otherwise <code>false</code>. 162 */ 163 public boolean isValidCollectionWhenMinElements(String argumentName, Collection<?> argumentValue, int minElements); 164 165 /** 166 * Validates if the Integer <code>argumentValue</code> is greate then Integer <code>value</code>. 167 * 168 * @param argumentName The name of the argument, which is validated. 169 * @param argumentValue The value of the argument, which is validated. 170 * @param value The value to validate against. 171 * @return <code>true</code> if the condition is met, otherwise <code>false</code>. 172 */ 173 boolean isValidWhenGreaterThen(String argumentName, int argumentValue, int value); 174 175 /** 176 * Validates if the String <code>argumentValue</code> has a minimal length of <code>minLength</code>. 177 * 178 * @param argumentName The name of the argument, which is validated. 179 * @param argumentValue The value of the argument, which is validated. 180 * @param minLength The length to validate against. 181 * @return <code>true</code> if the condition is met, otherwise <code>false</code>. 182 */ 183 boolean isValidStringMinLength(String argumentName, String argumentValue, int minLength); 184 185 /** 186 * Validates if the String <code>argumentValue</code> has a maximum length of <code>maxLength</code>. 187 * 188 * @param argumentName The name of the argument, which is validated. 189 * @param argumentValue The value of the argument, which is validated. 190 * @param maxLength The length to validate against. 191 * @return <code>true</code> if the condition is met, otherwise <code>false</code>. 192 */ 193 boolean isValidStringMaxLength(String argumentName, String argumentValue, int maxLength); 194 195 /** 196 * Validates if the String <code>argumentValue</code> has an exact length of <code>length</code>. 197 * 198 * @param argumentName The name of the argument, which is validated. 199 * @param argumentValue The value of the argument, which is validated. 200 * @param length The length to validate against. 201 * @return <code>true</code> if the condition is met, otherwise <code>false</code>. 202 */ 203 boolean isValidStringLength(String argumentName, String argumentValue, int length); 204 205 /** 206 * Validates if the String <code>argumentValue</code> has a minimal length of <code>minLength</code> and 207 * a maximum length of <code>maxLength</code>. 208 * 209 * @param argumentName The name of the argument, which is validated. 210 * @param argumentValue The value of the argument, which is validated. 211 * @param minLength The minimal length to validate against. 212 * @param maxLength The maximum length to validate against. 213 * @return <code>true</code> if the condition is met, otherwise <code>false</code>. 214 */ 215 boolean isValidStringMinAndMaxLength(String argumentName, String argumentValue, int minLength, int maxLength); 216 217 /** 218 * Validates if the String <code>argumentValue</code> matches the <code>dateFormat</code>. 219 * 220 * @param argumentName The name of the argument, which is validated. 221 * @param argumentValue The value of the argument, which is validated. 222 * @param dateFormat The date format which is used for matching. 223 * @return <code>true</code> if the condition is met, otherwise <code>false</code>. 224 */ 225 boolean isValidMatchingDateFormat(String argumentName, String argumentValue, DateFormat dateFormat); 226 227 /** 228 * Validates if the String <code>argumentValue</code> matches the date format registered under the 229 * <code>dateFormatName</code>. 230 * 231 * @param argumentName The name of the argument, which is validated. 232 * @param argumentValue The value of the argument, which is validated. 233 * @param patternName The name of the date format, which is used for matching. 234 * @return <code>true</code> if the condition is met, otherwise <code>false</code>. 235 * @see #isValidMatchingDateFormat(String, String, DateFormat) 236 */ 237 boolean isValidMatchingDateFormat(String argumentName, String argumentValue, String dateFormatName); 238 239 /** 240 * Validates if the String <code>argumentValue</code> matches the pattern regisstered under the 241 * <code>patternName</code>. 242 * 243 * @param argumentName The name of the argument, which is validated. 244 * @param argumentValue The value of the argument, which is validated. 245 * @param patternName The name of the pattern, which is used for matching. 246 * @return <code>true</code> if the condition is met, otherwise <code>false</code>. 247 */ 248 boolean isValidMatchingPattern(String argumentName, String argumentValue, String patternName); 249 250 /** 251 * Add an error message to the list of error messages. The list of error messages is used 252 * to create the <code>IllegalArgumentException</code> message. 253 * 254 * @param error The error message to add. 255 * @see #addError(String, String) 256 */ 257 void addError(String error); 258 259 /** 260 * Add an error message, for the given argumentName, to the list of error messages. 261 * @param argumentName - the argument (or property-key) in which the problem is found. 262 * @param message - the message that describes the problem. 263 */ 264 void addError(String argumentName, String message); 265 266 /** 267 * Add an error message, for when the argument is a Map instance, to the list of error messages, 268 * for the given key. 269 * 270 * @param argumentName - the argument Map (or property-key). 271 * @param key - the key under which the problem is found. 272 * @param message - the message that describes the problem. 273 */ 274 void addError(String argumentName, String key, String message); 275 276 /** 277 * Returns the <code>Set</code> of pattern names. 278 * @return The names of all the patterns. 279 */ 280 Set<String> getPatternNames(); 281 282 /** 283 * Returns the <code>Map</code> of pattern names and their regular expression. 284 * @return All pattern names and their regular expression (as <code>String</code>). 285 */ 286 Map<String, String> getPattern(); 287 288 /** 289 * Returns the <code>Pattern</code> found under the <code>patternName</code>, or throws 290 * an <code>IllegalArgumentException</code> if the <code>patternName</code> is not available. 291 * 292 * @param patternName The name of the pattern. 293 * @return The found <code>Pattern</code>. 294 */ 295 Pattern getPattern(String patternName); 296 297 /** 298 * Returns the regular expression found under the <code>patternName</code>, or throws 299 * an <code>IllegalArgumentException</code> if the <code>patternName</code> is not available. 300 * 301 * @param patternName The name of the pattern. 302 * @return The regular expression. 303 */ 304 String getRegularExpression(String patternName); 305 306 boolean isValidNotNullForKey(String string, Map<String, String> dataModelIdMap, String keyDataVendor); 307 308 }