1. /* ====================================================================
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowledgement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowledgement may appear in the software itself,
  24. * if and wherever such third-party acknowledgements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Commons", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Software Foundation.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.commons.lang;
  55. import java.util.Collection;
  56. import java.util.Iterator;
  57. import java.util.Map;
  58. /**
  59. * <p>Assists in validating arguments.</p>
  60. *
  61. * <p>The class is based along the lines of JUnit. If an argument value is
  62. * deemed invalid, an IllegalArgumentException is thrown. For example:</p>
  63. *
  64. * <pre>
  65. * Validate.isTrue( i > 0, "The value must be greater than zero: ", i);
  66. * Validate.notNull( surname, "The surname must not be null");
  67. * </pre>
  68. *
  69. * @author <a href="mailto:ola.berg@arkitema.se">Ola Berg</a>
  70. * @author Stephen Colebourne
  71. * @author Gary Gregory
  72. * @since 2.0
  73. * @version $Id: Validate.java,v 1.6 2003/08/23 10:39:20 scolebourne Exp $
  74. */
  75. public class Validate {
  76. /**
  77. * Constructor. This class should not normally be instantiated.
  78. */
  79. public Validate() {
  80. }
  81. // isTrue
  82. //---------------------------------------------------------------------------------
  83. /**
  84. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  85. * if the test result is <code>false</code>.</p>
  86. *
  87. * <p>This is used when validating according to an arbitrary boolean expression,
  88. * such as validating a primitive number or using your own custom validation
  89. * expression.</p>
  90. *
  91. * <pre>
  92. * Validate.isTrue( myObject.isOk(), "The object is not OK: ", myObject);
  93. * </pre>
  94. *
  95. * <p>For performance reasons, the object is passed as a separate parameter and
  96. * appended to the message string only in the case of an error.</p>
  97. *
  98. * @param expression a boolean expression
  99. * @param message the exception message you would like to see if the
  100. * expression is <code>false</code>
  101. * @param value the value to append to the message in case of error
  102. * @throws IllegalArgumentException if expression is <code>false</code>
  103. */
  104. public static void isTrue(boolean expression, String message, Object value) {
  105. if (expression == false) {
  106. throw new IllegalArgumentException(message + value);
  107. }
  108. }
  109. /**
  110. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  111. * if the test result is <code>false</code>.</p>
  112. *
  113. * <p>This is used when validating according to an arbitrary boolean expression,
  114. * such as validating a primitive number or using your own custom validation
  115. * expression.</p>
  116. *
  117. * <pre>
  118. * Validate.isTrue( i > 0, "The value must be greater than zero: ", i);
  119. * </pre>
  120. *
  121. * <p>For performance reasons, the object is passed as a separate parameter and
  122. * appended to the message string only in the case of an error.</p>
  123. *
  124. * @param expression a boolean expression
  125. * @param message the exception message you would like to see if the expression is <code>false</code>
  126. * @param value the value to append to the message in case of error
  127. * @throws IllegalArgumentException if expression is <code>false</code>
  128. */
  129. public static void isTrue(boolean expression, String message, long value) {
  130. if (expression == false) {
  131. throw new IllegalArgumentException(message + value);
  132. }
  133. }
  134. /**
  135. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  136. * if the test result is <code>false</code>.</p>
  137. *
  138. * <p>This is used when validating according to an arbitrary boolean expression,
  139. * such as validating a primitive number or using your own custom validation
  140. * expression.</p>
  141. *
  142. * <pre>
  143. * Validate.isTrue( d > 0.0, "The value must be greater than zero: ", d);
  144. * </pre>
  145. *
  146. * <p>For performance reasons, the object is passed as a separate parameter and
  147. * appended to the message string only in the case of an error.</p>
  148. *
  149. * @param expression a boolean expression
  150. * @param message the exception message you would like to see if the expression
  151. * is <code>false</code>
  152. * @param value the value to append to the message in case of error
  153. * @throws IllegalArgumentException if expression is <code>false</code>
  154. */
  155. public static void isTrue(boolean expression, String message, double value) {
  156. if (expression == false) {
  157. throw new IllegalArgumentException(message + value);
  158. }
  159. }
  160. /**
  161. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  162. * if the test result is <code>false</code>.</p>
  163. *
  164. * <p>This is used when validating according to an arbitrary boolean expression,
  165. * such as validating a primitive number or using your own custom validation
  166. * expression.</p>
  167. *
  168. * <pre>
  169. * Validate.isTrue( (i > 0), "The value must be greater than zero");
  170. * Validate.isTrue( myObject.isOk(), "The object is not OK");
  171. * </pre>
  172. *
  173. * <p>For performance reasons, the message string should not involve a string append,
  174. * instead use the {@link #isTrue(boolean, String, Object)} method.</p>
  175. *
  176. * @param expression a boolean expression
  177. * @param message the exception message you would like to see if the expression
  178. * is <code>false</code>
  179. * @throws IllegalArgumentException if expression is <code>false</code>
  180. */
  181. public static void isTrue(boolean expression, String message) {
  182. if (expression == false) {
  183. throw new IllegalArgumentException(message);
  184. }
  185. }
  186. /**
  187. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  188. * if the test result is <code>false</code>.</p>
  189. *
  190. * <p>This is used when validating according to an arbitrary boolean expression,
  191. * such as validating a primitive number or using your own custom validation
  192. * expression.</p>
  193. *
  194. * <pre>
  195. * Validate.isTrue( i > 0 );
  196. * Validate.isTrue( myObject.isOk() );
  197. * </pre>
  198. *
  199. * <p>The message in the exception is 'The validated expression is false'.</p>
  200. *
  201. * @param expression a boolean expression
  202. * @throws IllegalArgumentException if expression is <code>false</code>
  203. */
  204. public static void isTrue(boolean expression) {
  205. if (expression == false) {
  206. throw new IllegalArgumentException("The validated expression is false");
  207. }
  208. }
  209. // notNull
  210. //---------------------------------------------------------------------------------
  211. /**
  212. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  213. * if the argument is <code>null</code>.</p>
  214. *
  215. * <pre>
  216. * Validate.notNull(myObject, "The object must not be null");
  217. * </pre>
  218. *
  219. * @param object the object to check is not <code>null</code>
  220. * @param message the exception message you would like to see
  221. * if the object is <code>null</code>
  222. * @throws IllegalArgumentException if the object is <code>null</code>
  223. */
  224. public static void notNull(Object object, String message) {
  225. if (object == null) {
  226. throw new IllegalArgumentException(message);
  227. }
  228. }
  229. /**
  230. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  231. * if the argument is <code>null</code>.</p>
  232. *
  233. * <pre>
  234. * Validate.notNull(myObject);
  235. * </pre>
  236. *
  237. * <p>The message in the exception is 'The validated object is null'.</p>
  238. *
  239. * @param object the object to check is not <code>null</code>
  240. * @throws IllegalArgumentException if the object is <code>null</code>
  241. */
  242. public static void notNull(Object object) {
  243. if (object == null) {
  244. throw new IllegalArgumentException("The validated object is null");
  245. }
  246. }
  247. // notEmpty array
  248. //---------------------------------------------------------------------------------
  249. /**
  250. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  251. * if the argument array is empty (<code>null</code> or no elements).</p>
  252. *
  253. * <pre>
  254. * Validate.notEmpty(myArray, "The array must not be empty");
  255. * </pre>
  256. *
  257. * @param array the array to check is not empty
  258. * @param message the exception message you would like to see if the array is empty
  259. * @throws IllegalArgumentException if the array is empty
  260. */
  261. public static void notEmpty(Object[] array, String message) {
  262. if (array == null || array.length == 0) {
  263. throw new IllegalArgumentException(message);
  264. }
  265. }
  266. /**
  267. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  268. * if the argument array is empty (<code>null</code> or no elements).</p>
  269. *
  270. * <pre>
  271. * Validate.notEmpty(myArray);
  272. * </pre>
  273. *
  274. * <p>The message in the exception is 'The validated array is empty'.
  275. *
  276. * @param array the array to check is not empty
  277. * @throws IllegalArgumentException if the array is empty
  278. */
  279. public static void notEmpty(Object[] array) {
  280. if (array == null || array.length == 0) {
  281. throw new IllegalArgumentException("The validated array is empty");
  282. }
  283. }
  284. // notEmpty collection
  285. //---------------------------------------------------------------------------------
  286. /**
  287. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  288. * if the argument Collection is empty (<code>null</code> or no elements).</p>
  289. *
  290. * <pre>
  291. * Validate.notEmpty(myCollection, "The collection must not be empty");
  292. * </pre>
  293. *
  294. * @param collection the collection to check is not empty
  295. * @param message the exception message you would like to see if the collection is empty
  296. * @throws IllegalArgumentException if the collection is empty
  297. */
  298. public static void notEmpty(Collection collection, String message) {
  299. if (collection == null || collection.size() == 0) {
  300. throw new IllegalArgumentException(message);
  301. }
  302. }
  303. /**
  304. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  305. * if the argument Collection is empty (<code>null</code> or no elements).</p>
  306. *
  307. * <pre>
  308. * Validate.notEmpty(myCollection);
  309. * </pre>
  310. *
  311. * <p>The message in the exception is 'The validated collection is empty'.</p>
  312. *
  313. * @param collection the collection to check is not empty
  314. * @throws IllegalArgumentException if the collection is empty
  315. */
  316. public static void notEmpty(Collection collection) {
  317. if (collection == null || collection.size() == 0) {
  318. throw new IllegalArgumentException("The validated collection is empty");
  319. }
  320. }
  321. // notEmpty map
  322. //---------------------------------------------------------------------------------
  323. /**
  324. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  325. * if the argument Map is empty (<code>null</code> or no elements).</p>
  326. *
  327. * <pre>
  328. * Validate.notEmpty(myMap, "The collection must not be empty");
  329. * </pre>
  330. *
  331. * @param map the map to check is not empty
  332. * @param message the exception message you would like to see if the map is empty
  333. * @throws IllegalArgumentException if the map is empty
  334. */
  335. public static void notEmpty(Map map, String message) {
  336. if (map == null || map.size() == 0) {
  337. throw new IllegalArgumentException(message);
  338. }
  339. }
  340. /**
  341. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  342. * if the argument Map is empty (<code>null</code> or no elements).</p>
  343. *
  344. * <pre>
  345. * Validate.notEmpty(myMap);
  346. * </pre>
  347. *
  348. * <p>The message in the exception is 'The validated map is empty'.</p>
  349. *
  350. * @param map the map to check is not empty
  351. * @throws IllegalArgumentException if the map is empty
  352. */
  353. public static void notEmpty(Map map) {
  354. if (map == null || map.size() == 0) {
  355. throw new IllegalArgumentException("The validated map is empty");
  356. }
  357. }
  358. // notEmpty string
  359. //---------------------------------------------------------------------------------
  360. /**
  361. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  362. * if the argument String is empty (<code>null</code> or zero length).</p>
  363. *
  364. * <pre>
  365. * Validate.notEmpty(myString, "The string must not be empty");
  366. * </pre>
  367. *
  368. * @param string the string to check is not empty
  369. * @param message the exception message you would like to see if the string is empty
  370. * @throws IllegalArgumentException if the string is empty
  371. */
  372. public static void notEmpty(String string, String message) {
  373. if (string == null || string.length() == 0) {
  374. throw new IllegalArgumentException(message);
  375. }
  376. }
  377. /**
  378. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  379. * if the argument String is empty (<code>null</code> or zero length).</p>
  380. *
  381. * <pre>
  382. * Validate.notEmpty(myString);
  383. * </pre>
  384. *
  385. * <p>The message in the exception is 'The validated string is empty'.</p>
  386. *
  387. * @param string the string to check is not empty
  388. * @throws IllegalArgumentException if the string is empty
  389. */
  390. public static void notEmpty(String string) {
  391. if (string == null || string.length() == 0) {
  392. throw new IllegalArgumentException("The validated string is empty");
  393. }
  394. }
  395. // notNullElements array
  396. //---------------------------------------------------------------------------------
  397. /**
  398. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  399. * if the argument array has <code>null</code> elements or is
  400. * <code>null</code>.</p>
  401. *
  402. * <pre>
  403. * Validate.notEmpty(myArray, "The array must not contain null elements");
  404. * </pre>
  405. *
  406. * @param array the array to check
  407. * @param message the exception message if the array has
  408. * <code>null</code> elements
  409. * @throws IllegalArgumentException if the array has <code>null</code>
  410. * elements or is <code>null</code>
  411. */
  412. public static void noNullElements(Object[] array, String message) {
  413. Validate.notNull(array);
  414. for (int i = 0; i < array.length; i++) {
  415. if (array[i] == null) {
  416. throw new IllegalArgumentException(message);
  417. }
  418. }
  419. }
  420. /**
  421. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  422. * if the argument array has <code>null</code> elements or is
  423. * <code>null</code>.</p>
  424. *
  425. * <pre>
  426. * Validate.notEmpty(myArray);
  427. * </pre>
  428. *
  429. * <p>The message in the exception is 'The validated array contains null element at index: '.</p>
  430. *
  431. * @param array the array to check
  432. * @throws IllegalArgumentException if the array has <code>null</code>
  433. * elements or is <code>null</code>
  434. */
  435. public static void noNullElements(Object[] array) {
  436. Validate.notNull(array);
  437. for (int i = 0; i < array.length; i++) {
  438. if (array[i] == null) {
  439. throw new IllegalArgumentException("The validated array contains null element at index: " + i);
  440. }
  441. }
  442. }
  443. // notNullElements collection
  444. //---------------------------------------------------------------------------------
  445. /**
  446. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  447. * if the argument collection has <code>null</code> elements or is
  448. * <code>null</code>.</p>
  449. *
  450. * <pre>
  451. * Validate.notEmpty(myCollection, "The collection must not contain null elements");
  452. * </pre>
  453. *
  454. * @param collection the collection to check
  455. * @param message the exception message if the array has
  456. * <code>null</code> elements
  457. * @throws IllegalArgumentException if the collection has
  458. * <code>null</code> elements or is <code>null</code>
  459. */
  460. public static void noNullElements(Collection collection, String message) {
  461. Validate.notNull(collection);
  462. int i = 0;
  463. for (Iterator it = collection.iterator(); it.hasNext(); i++) {
  464. if (it.next() == null) {
  465. throw new IllegalArgumentException(message);
  466. }
  467. }
  468. }
  469. /**
  470. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  471. * if the argument collection has <code>null</code> elements or is
  472. * <code>null</code>.</p>
  473. *
  474. * <pre>
  475. * Validate.notEmpty(myCollection);
  476. * </pre>
  477. *
  478. * <p>The message in the exception is 'The validated collection contains null element at index: '.</p>
  479. *
  480. * @param collection the collection to check
  481. * @throws IllegalArgumentException if the collection has
  482. * <code>null</code> elements or is <code>null</code>
  483. */
  484. public static void noNullElements(Collection collection) {
  485. Validate.notNull(collection);
  486. int i = 0;
  487. for (Iterator it = collection.iterator(); it.hasNext(); i++) {
  488. if (it.next() == null) {
  489. throw new IllegalArgumentException("The validated collection contains null element at index: " + i);
  490. }
  491. }
  492. }
  493. }