/tooling/org.apache.commons.lang/source-bundle/org/apache/commons/lang/Validate.java

https://github.com/RefuX/applause · Java · 554 lines · 137 code · 33 blank · 384 comment · 64 complexity · b425561722708217d9b68bddb950b19d MD5 · raw file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.commons.lang;
  18. import java.util.Collection;
  19. import java.util.Iterator;
  20. import java.util.Map;
  21. /**
  22. * <p>Assists in validating arguments.</p>
  23. *
  24. * <p>The class is based along the lines of JUnit. If an argument value is
  25. * deemed invalid, an IllegalArgumentException is thrown. For example:</p>
  26. *
  27. * <pre>
  28. * Validate.isTrue( i > 0, "The value must be greater than zero: ", i);
  29. * Validate.notNull( surname, "The surname must not be null");
  30. * </pre>
  31. *
  32. * @author <a href="mailto:ola.berg@arkitema.se">Ola Berg</a>
  33. * @author Stephen Colebourne
  34. * @author Gary Gregory
  35. * @author Norm Deane
  36. * @since 2.0
  37. * @version $Id: Validate.java 437554 2006-08-28 06:21:41Z bayard $
  38. */
  39. public class Validate {
  40. // Validate has no dependencies on other classes in Commons Lang at present
  41. /**
  42. * Constructor. This class should not normally be instantiated.
  43. */
  44. public Validate() {
  45. super();
  46. }
  47. // isTrue
  48. //---------------------------------------------------------------------------------
  49. /**
  50. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  51. * if the test result is <code>false</code>.</p>
  52. *
  53. * <p>This is used when validating according to an arbitrary boolean expression,
  54. * such as validating a primitive number or using your own custom validation
  55. * expression.</p>
  56. *
  57. * <pre>
  58. * Validate.isTrue( myObject.isOk(), "The object is not OK: ", myObject);
  59. * </pre>
  60. *
  61. * <p>For performance reasons, the object is passed as a separate parameter and
  62. * appended to the message string only in the case of an error.</p>
  63. *
  64. * @param expression a boolean expression
  65. * @param message the exception message you would like to see if the
  66. * expression is <code>false</code>
  67. * @param value the value to append to the message in case of error
  68. * @throws IllegalArgumentException if expression is <code>false</code>
  69. */
  70. public static void isTrue(boolean expression, String message, Object value) {
  71. if (expression == false) {
  72. throw new IllegalArgumentException(message + value);
  73. }
  74. }
  75. /**
  76. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  77. * if the test result is <code>false</code>.</p>
  78. *
  79. * <p>This is used when validating according to an arbitrary boolean expression,
  80. * such as validating a primitive number or using your own custom validation
  81. * expression.</p>
  82. *
  83. * <pre>
  84. * Validate.isTrue( i > 0, "The value must be greater than zero: ", i);
  85. * </pre>
  86. *
  87. * <p>For performance reasons, the long value is passed as a separate parameter and
  88. * appended to the message string only in the case of an error.</p>
  89. *
  90. * @param expression a boolean expression
  91. * @param message the exception message you would like to see if the expression is <code>false</code>
  92. * @param value the value to append to the message in case of error
  93. * @throws IllegalArgumentException if expression is <code>false</code>
  94. */
  95. public static void isTrue(boolean expression, String message, long value) {
  96. if (expression == false) {
  97. throw new IllegalArgumentException(message + value);
  98. }
  99. }
  100. /**
  101. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  102. * if the test result is <code>false</code>.</p>
  103. *
  104. * <p>This is used when validating according to an arbitrary boolean expression,
  105. * such as validating a primitive number or using your own custom validation
  106. * expression.</p>
  107. *
  108. * <pre>
  109. * Validate.isTrue( d > 0.0, "The value must be greater than zero: ", d);
  110. * </pre>
  111. *
  112. * <p>For performance reasons, the double value is passed as a separate parameter and
  113. * appended to the message string only in the case of an error.</p>
  114. *
  115. * @param expression a boolean expression
  116. * @param message the exception message you would like to see if the expression
  117. * is <code>false</code>
  118. * @param value the value to append to the message in case of error
  119. * @throws IllegalArgumentException if expression is <code>false</code>
  120. */
  121. public static void isTrue(boolean expression, String message, double value) {
  122. if (expression == false) {
  123. throw new IllegalArgumentException(message + value);
  124. }
  125. }
  126. /**
  127. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  128. * if the test result is <code>false</code>.</p>
  129. *
  130. * <p>This is used when validating according to an arbitrary boolean expression,
  131. * such as validating a primitive number or using your own custom validation
  132. * expression.</p>
  133. *
  134. * <pre>
  135. * Validate.isTrue( (i > 0), "The value must be greater than zero");
  136. * Validate.isTrue( myObject.isOk(), "The object is not OK");
  137. * </pre>
  138. *
  139. * <p>For performance reasons, the message string should not involve a string append,
  140. * instead use the {@link #isTrue(boolean, String, Object)} method.</p>
  141. *
  142. * @param expression a boolean expression
  143. * @param message the exception message you would like to see if the expression
  144. * is <code>false</code>
  145. * @throws IllegalArgumentException if expression is <code>false</code>
  146. */
  147. public static void isTrue(boolean expression, String message) {
  148. if (expression == false) {
  149. throw new IllegalArgumentException(message);
  150. }
  151. }
  152. /**
  153. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  154. * if the test result is <code>false</code>.</p>
  155. *
  156. * <p>This is used when validating according to an arbitrary boolean expression,
  157. * such as validating a primitive number or using your own custom validation
  158. * expression.</p>
  159. *
  160. * <pre>
  161. * Validate.isTrue( i > 0 );
  162. * Validate.isTrue( myObject.isOk() );
  163. * </pre>
  164. *
  165. * <p>The message in the exception is 'The validated expression is false'.</p>
  166. *
  167. * @param expression a boolean expression
  168. * @throws IllegalArgumentException if expression is <code>false</code>
  169. */
  170. public static void isTrue(boolean expression) {
  171. if (expression == false) {
  172. throw new IllegalArgumentException("The validated expression is false");
  173. }
  174. }
  175. // notNull
  176. //---------------------------------------------------------------------------------
  177. /**
  178. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  179. * if the argument is <code>null</code>.</p>
  180. *
  181. * <pre>
  182. * Validate.notNull(myObject, "The object must not be null");
  183. * </pre>
  184. *
  185. * @param object the object to check is not <code>null</code>
  186. * @param message the exception message you would like to see
  187. * if the object is <code>null</code>
  188. * @throws IllegalArgumentException if the object is <code>null</code>
  189. */
  190. public static void notNull(Object object, String message) {
  191. if (object == null) {
  192. throw new IllegalArgumentException(message);
  193. }
  194. }
  195. /**
  196. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  197. * if the argument is <code>null</code>.</p>
  198. *
  199. * <pre>
  200. * Validate.notNull(myObject);
  201. * </pre>
  202. *
  203. * <p>The message in the exception is 'The validated object is null'.</p>
  204. *
  205. * @param object the object to check is not <code>null</code>
  206. * @throws IllegalArgumentException if the object is <code>null</code>
  207. */
  208. public static void notNull(Object object) {
  209. if (object == null) {
  210. throw new IllegalArgumentException("The validated object is null");
  211. }
  212. }
  213. // notEmpty array
  214. //---------------------------------------------------------------------------------
  215. /**
  216. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  217. * if the argument array is empty (<code>null</code> or no elements).</p>
  218. *
  219. * <pre>
  220. * Validate.notEmpty(myArray, "The array must not be empty");
  221. * </pre>
  222. *
  223. * @param array the array to check is not empty
  224. * @param message the exception message you would like to see if the array is empty
  225. * @throws IllegalArgumentException if the array is empty
  226. */
  227. public static void notEmpty(Object[] array, String message) {
  228. if (array == null || array.length == 0) {
  229. throw new IllegalArgumentException(message);
  230. }
  231. }
  232. /**
  233. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  234. * if the argument array is empty (<code>null</code> or no elements).</p>
  235. *
  236. * <pre>
  237. * Validate.notEmpty(myArray);
  238. * </pre>
  239. *
  240. * <p>The message in the exception is 'The validated array is empty'.
  241. *
  242. * @param array the array to check is not empty
  243. * @throws IllegalArgumentException if the array is empty
  244. */
  245. public static void notEmpty(Object[] array) {
  246. if (array == null || array.length == 0) {
  247. throw new IllegalArgumentException("The validated array is empty");
  248. }
  249. }
  250. // notEmpty collection
  251. //---------------------------------------------------------------------------------
  252. /**
  253. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  254. * if the argument Collection is empty (<code>null</code> or no elements).</p>
  255. *
  256. * <pre>
  257. * Validate.notEmpty(myCollection, "The collection must not be empty");
  258. * </pre>
  259. *
  260. * @param collection the collection to check is not empty
  261. * @param message the exception message you would like to see if the collection is empty
  262. * @throws IllegalArgumentException if the collection is empty
  263. */
  264. public static void notEmpty(Collection collection, String message) {
  265. if (collection == null || collection.size() == 0) {
  266. throw new IllegalArgumentException(message);
  267. }
  268. }
  269. /**
  270. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  271. * if the argument Collection is empty (<code>null</code> or no elements).</p>
  272. *
  273. * <pre>
  274. * Validate.notEmpty(myCollection);
  275. * </pre>
  276. *
  277. * <p>The message in the exception is 'The validated collection is empty'.</p>
  278. *
  279. * @param collection the collection to check is not empty
  280. * @throws IllegalArgumentException if the collection is empty
  281. */
  282. public static void notEmpty(Collection collection) {
  283. if (collection == null || collection.size() == 0) {
  284. throw new IllegalArgumentException("The validated collection is empty");
  285. }
  286. }
  287. // notEmpty map
  288. //---------------------------------------------------------------------------------
  289. /**
  290. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  291. * if the argument Map is empty (<code>null</code> or no elements).</p>
  292. *
  293. * <pre>
  294. * Validate.notEmpty(myMap, "The map must not be empty");
  295. * </pre>
  296. *
  297. * @param map the map to check is not empty
  298. * @param message the exception message you would like to see if the map is empty
  299. * @throws IllegalArgumentException if the map is empty
  300. */
  301. public static void notEmpty(Map map, String message) {
  302. if (map == null || map.size() == 0) {
  303. throw new IllegalArgumentException(message);
  304. }
  305. }
  306. /**
  307. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  308. * if the argument Map is empty (<code>null</code> or no elements).</p>
  309. *
  310. * <pre>
  311. * Validate.notEmpty(myMap);
  312. * </pre>
  313. *
  314. * <p>The message in the exception is 'The validated map is empty'.</p>
  315. *
  316. * @param map the map to check is not empty
  317. * @throws IllegalArgumentException if the map is empty
  318. */
  319. public static void notEmpty(Map map) {
  320. if (map == null || map.size() == 0) {
  321. throw new IllegalArgumentException("The validated map is empty");
  322. }
  323. }
  324. // notEmpty string
  325. //---------------------------------------------------------------------------------
  326. /**
  327. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  328. * if the argument String is empty (<code>null</code> or zero length).</p>
  329. *
  330. * <pre>
  331. * Validate.notEmpty(myString, "The string must not be empty");
  332. * </pre>
  333. *
  334. * @param string the string to check is not empty
  335. * @param message the exception message you would like to see if the string is empty
  336. * @throws IllegalArgumentException if the string is empty
  337. */
  338. public static void notEmpty(String string, String message) {
  339. if (string == null || string.length() == 0) {
  340. throw new IllegalArgumentException(message);
  341. }
  342. }
  343. /**
  344. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  345. * if the argument String is empty (<code>null</code> or zero length).</p>
  346. *
  347. * <pre>
  348. * Validate.notEmpty(myString);
  349. * </pre>
  350. *
  351. * <p>The message in the exception is 'The validated string is empty'.</p>
  352. *
  353. * @param string the string to check is not empty
  354. * @throws IllegalArgumentException if the string is empty
  355. */
  356. public static void notEmpty(String string) {
  357. if (string == null || string.length() == 0) {
  358. throw new IllegalArgumentException("The validated string is empty");
  359. }
  360. }
  361. // notNullElements array
  362. //---------------------------------------------------------------------------------
  363. /**
  364. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  365. * if the argument array has <code>null</code> elements or is
  366. * <code>null</code>.</p>
  367. *
  368. * <pre>
  369. * Validate.noNullElements(myArray, "The array must not contain null elements");
  370. * </pre>
  371. *
  372. * <p>If the array is null then the message in the exception is 'The validated object is null'.</p>
  373. *
  374. * @param array the array to check
  375. * @param message the exception message if the array has
  376. * <code>null</code> elements
  377. * @throws IllegalArgumentException if the array has <code>null</code>
  378. * elements or is <code>null</code>
  379. */
  380. public static void noNullElements(Object[] array, String message) {
  381. Validate.notNull(array);
  382. for (int i = 0; i < array.length; i++) {
  383. if (array[i] == null) {
  384. throw new IllegalArgumentException(message);
  385. }
  386. }
  387. }
  388. /**
  389. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  390. * if the argument array has <code>null</code> elements or is
  391. * <code>null</code>.</p>
  392. *
  393. * <pre>
  394. * Validate.noNullElements(myArray);
  395. * </pre>
  396. *
  397. * <p>If the array has a null element the message in the exception is
  398. * 'The validated array contains null element at index: '.</p>
  399. *
  400. * <p>If the array is null then the message in the exception is 'The validated object is null'.</p>
  401. *
  402. * @param array the array to check
  403. * @throws IllegalArgumentException if the array has <code>null</code>
  404. * elements or is <code>null</code>
  405. */
  406. public static void noNullElements(Object[] array) {
  407. Validate.notNull(array);
  408. for (int i = 0; i < array.length; i++) {
  409. if (array[i] == null) {
  410. throw new IllegalArgumentException("The validated array contains null element at index: " + i);
  411. }
  412. }
  413. }
  414. // notNullElements collection
  415. //---------------------------------------------------------------------------------
  416. /**
  417. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  418. * if the argument Collection has <code>null</code> elements or is
  419. * <code>null</code>.</p>
  420. *
  421. * <pre>
  422. * Validate.noNullElements(myCollection, "The collection must not contain null elements");
  423. * </pre>
  424. *
  425. * <p>If the collection is null then the message in the exception is 'The validated object is null'.</p>
  426. *
  427. * @param collection the collection to check
  428. * @param message the exception message if the collection has
  429. * <code>null</code> elements
  430. * @throws IllegalArgumentException if the collection has
  431. * <code>null</code> elements or is <code>null</code>
  432. */
  433. public static void noNullElements(Collection collection, String message) {
  434. Validate.notNull(collection);
  435. for (Iterator it = collection.iterator(); it.hasNext();) {
  436. if (it.next() == null) {
  437. throw new IllegalArgumentException(message);
  438. }
  439. }
  440. }
  441. /**
  442. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  443. * if the argument Collection has <code>null</code> elements or is
  444. * <code>null</code>.</p>
  445. *
  446. * <pre>
  447. * Validate.noNullElements(myCollection);
  448. * </pre>
  449. *
  450. * <p>The message in the exception is 'The validated collection contains null element at index: '.</p>
  451. *
  452. * <p>If the collection is null then the message in the exception is 'The validated object is null'.</p>
  453. *
  454. * @param collection the collection to check
  455. * @throws IllegalArgumentException if the collection has
  456. * <code>null</code> elements or is <code>null</code>
  457. */
  458. public static void noNullElements(Collection collection) {
  459. Validate.notNull(collection);
  460. int i = 0;
  461. for (Iterator it = collection.iterator(); it.hasNext(); i++) {
  462. if (it.next() == null) {
  463. throw new IllegalArgumentException("The validated collection contains null element at index: " + i);
  464. }
  465. }
  466. }
  467. /**
  468. * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
  469. * if the argument collection is <code>null</code> or has elements that
  470. * are not of type <code>clazz</code> or a subclass.</p>
  471. *
  472. * <pre>
  473. * Validate.allElementsOfType(collection, String.class, "Collection has invalid elements");
  474. * </pre>
  475. *
  476. * @param collection the collection to check, not null
  477. * @param clazz the <code>Class</code> which the collection's elements are expected to be, not null
  478. * @param message the exception message if the <code>Collection</code> has elements not of type <code>clazz</code>
  479. * @since 2.1
  480. */
  481. public static void allElementsOfType(Collection collection, Class clazz, String message) {
  482. Validate.notNull(collection);
  483. Validate.notNull(clazz);
  484. for (Iterator it = collection.iterator(); it.hasNext(); ) {
  485. if (clazz.isInstance(it.next()) == false) {
  486. throw new IllegalArgumentException(message);
  487. }
  488. }
  489. }
  490. /**
  491. * <p>
  492. * Validate an argument, throwing <code>IllegalArgumentException</code> if the argument collection is
  493. * <code>null</code> or has elements that are not of type <code>clazz</code> or a subclass.
  494. * </p>
  495. *
  496. * <pre>
  497. * Validate.allElementsOfType(collection, String.class);
  498. * </pre>
  499. *
  500. * <p>
  501. * The message in the exception is 'The validated collection contains an element not of type clazz at index: '.
  502. * </p>
  503. *
  504. * @param collection
  505. * the collection to check, not null
  506. * @param clazz
  507. * the <code>Class</code> which the collection's elements are expected to be, not null
  508. * @since 2.1
  509. */
  510. public static void allElementsOfType(Collection collection, Class clazz) {
  511. Validate.notNull(collection);
  512. Validate.notNull(clazz);
  513. int i = 0;
  514. for (Iterator it = collection.iterator(); it.hasNext(); i++) {
  515. if (clazz.isInstance(it.next()) == false) {
  516. throw new IllegalArgumentException("The validated collection contains an element not of type "
  517. + clazz.getName() + " at index: " + i);
  518. }
  519. }
  520. }
  521. }