/Validate.java

https://gitlab.com/praveen.pixlrit/SonarApp2 · Java · 507 lines · 127 code · 33 blank · 347 comment · 46 complexity · e095391e08f549a3db21a95b67ecf7ad MD5 · raw file

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