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 org.apache.commons.lang.math.NumberUtils;
  56. /**
  57. * <p>Operations on boolean primitives and Boolean objects.</p>
  58. *
  59. * <p>This class tries to handle <code>null</code> input gracefully.
  60. * An exception will not be thrown for a <code>null</code> input.
  61. * Each method documents its behaviour in more detail.</p>
  62. *
  63. * @author Stephen Colebourne
  64. * @author Matthew Hawthorne
  65. * @author Gary Gregory
  66. * @since 2.0
  67. * @version $Id: BooleanUtils.java,v 1.14 2003/08/22 17:25:33 ggregory Exp $
  68. */
  69. public class BooleanUtils {
  70. /**
  71. * <p><code>BooleanUtils</code> instances should NOT be constructed in standard programming.
  72. * Instead, the class should be used as <code>BooleanUtils.toBooleanObject(true);</code>.</p>
  73. *
  74. * <p>This constructor is public to permit tools that require a JavaBean instance
  75. * to operate.</p>
  76. */
  77. public BooleanUtils() {
  78. }
  79. // Boolean utilities
  80. //--------------------------------------------------------------------------
  81. /**
  82. * <p>Negates the specified boolean.</p>
  83. *
  84. * <p>If <code>null</code> is passed in, <code>null</code> will be returned.</p>
  85. *
  86. * @param bool the Boolean to negate, may be null
  87. * @return the negated Boolean, or <code>null</code> if <code>null</code> input
  88. */
  89. public static Boolean negate(Boolean bool) {
  90. if (bool == null) {
  91. return null;
  92. }
  93. return (bool.booleanValue() ? Boolean.FALSE : Boolean.TRUE);
  94. }
  95. // boolean Boolean methods
  96. //-----------------------------------------------------------------------
  97. /**
  98. * <p>Boolean factory that avoids creating new Boolean objecs all the time.</p>
  99. *
  100. * <p>This method was added to JDK1.4 but is available here for earlier JDKs.</p>
  101. *
  102. * @param bool the boolean to convert
  103. * @return Boolean.TRUE or Boolean.FALSE as appropriate
  104. */
  105. public static Boolean toBooleanObject(boolean bool) {
  106. return (bool ? Boolean.TRUE : Boolean.FALSE);
  107. }
  108. /**
  109. * <p>Converts a Boolean to a boolean handling <code>null</code>
  110. * by returning <code>false</code>.</p>
  111. *
  112. * @param bool the boolean to convert
  113. * @return <code>true</code> or <code>false</code>,
  114. * <code>null</code> returns <code>false</code>
  115. */
  116. public static boolean toBoolean(Boolean bool) {
  117. if (bool == null) {
  118. return false;
  119. }
  120. return (bool.booleanValue() ? true : false);
  121. }
  122. /**
  123. * <p>Converts a Boolean to a boolean handling <code>null</code>.</p>
  124. *
  125. * @param bool the boolean to convert
  126. * @param valueIfNull the boolean value to return if <code>null</code>
  127. * @return <code>true</code> or <code>false</code>
  128. */
  129. public static boolean toBooleanDefaultIfNull(Boolean bool, boolean valueIfNull) {
  130. if (bool == null) {
  131. return valueIfNull;
  132. }
  133. return (bool.booleanValue() ? true : false);
  134. }
  135. // Integer to Boolean methods
  136. //-----------------------------------------------------------------------
  137. /**
  138. * <p>Converts an int to a boolean using the convention that <code>zero</code>
  139. * is <code>false</code>.</p>
  140. *
  141. * @param value the int to convert
  142. * @return <code>true</code> if non-zero, <code>false</code>
  143. * if zero
  144. */
  145. public static boolean toBoolean(int value) {
  146. return (value == 0 ? false : true);
  147. }
  148. /**
  149. * <p>Converts an int to a Boolean using the convention that <code>zero</code>
  150. * is <code>false</code>.</p>
  151. *
  152. * @param value the int to convert
  153. * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero,
  154. * <code>null</code> if <code>null</code>
  155. */
  156. public static Boolean toBooleanObject(int value) {
  157. return (value == 0 ? Boolean.FALSE : Boolean.TRUE);
  158. }
  159. /**
  160. * <p>Converts an Integer to a Boolean using the convention that <code>zero</code>
  161. * is <code>false</code>.</p>
  162. *
  163. * <p><code>null</code> will be converted to <code>null</code>.</p>
  164. *
  165. * @param value the Integer to convert
  166. * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero,
  167. * <code>null</code> if <code>null</code> input
  168. */
  169. public static Boolean toBooleanObject(Integer value) {
  170. if (value == null) {
  171. return null;
  172. }
  173. return (value.intValue() == 0 ? Boolean.FALSE : Boolean.TRUE);
  174. }
  175. /**
  176. * <p>Converts an int to a boolean specifying the conversion values.</p>
  177. *
  178. * @param value the Integer to convert
  179. * @param trueValue the value to match for <code>true</code>
  180. * @param falseValue the value to match for <code>false</code>
  181. * @return <code>true</code> or <code>false</code>
  182. * @throws IllegalArgumentException if no match
  183. */
  184. public static boolean toBoolean(int value, int trueValue, int falseValue) {
  185. if (value == trueValue) {
  186. return true;
  187. } else if (value == falseValue) {
  188. return false;
  189. }
  190. // no match
  191. throw new IllegalArgumentException("The Integer did not match either specified value");
  192. }
  193. /**
  194. * <p>Converts an Integer to a boolean specifying the conversion values.</p>
  195. *
  196. * @param value the Integer to convert
  197. * @param trueValue the value to match for <code>true</code>,
  198. * may be <code>null</code>
  199. * @param falseValue the value to match for <code>false</code>,
  200. * may be <code>null</code>
  201. * @return <code>true</code> or <code>false</code>
  202. * @throws IllegalArgumentException if no match
  203. */
  204. public static boolean toBoolean(Integer value, Integer trueValue, Integer falseValue) {
  205. if (value == null) {
  206. if (trueValue == null) {
  207. return true;
  208. } else if (falseValue == null) {
  209. return false;
  210. }
  211. } else if (value.equals(trueValue)) {
  212. return true;
  213. } else if (value.equals(falseValue)) {
  214. return false;
  215. }
  216. // no match
  217. throw new IllegalArgumentException("The Integer did not match either specified value");
  218. }
  219. /**
  220. * <p>Converts an int to a Boolean specifying the conversion values.</p>
  221. *
  222. * @param value the Integer to convert
  223. * @param trueValue the value to match for <code>true</code>
  224. * @param falseValue the value to match for <code>false</code>
  225. * @param nullValue the value to to match for <code>null</code>
  226. * @return Boolean.TRUE, Boolean.FALSE, or <code>null</code>
  227. * @throws IllegalArgumentException if no match
  228. */
  229. public static Boolean toBooleanObject(int value, int trueValue, int falseValue, int nullValue) {
  230. if (value == trueValue) {
  231. return Boolean.TRUE;
  232. } else if (value == falseValue) {
  233. return Boolean.FALSE;
  234. } else if (value == nullValue) {
  235. return null;
  236. }
  237. // no match
  238. throw new IllegalArgumentException("The Integer did not match any specified value");
  239. }
  240. /**
  241. * <p>Converts an Integer to a Boolean specifying the conversion values.</p>
  242. *
  243. * @param value the Integer to convert
  244. * @param trueValue the value to match for <code>true</code>,
  245. * may be <code>null</code>
  246. * @param falseValue the value to match for <code>false</code>,
  247. * may be <code>null</code>
  248. * @param nullValue the value to to match for <code>null</code>,
  249. * may be <code>null</code>
  250. * @return Boolean.TRUE, Boolean.FALSE, or <code>null</code>
  251. * @throws IllegalArgumentException if no match
  252. */
  253. public static Boolean toBooleanObject(Integer value, Integer trueValue, Integer falseValue, Integer nullValue) {
  254. if (value == null) {
  255. if (trueValue == null) {
  256. return Boolean.TRUE;
  257. } else if (falseValue == null) {
  258. return Boolean.FALSE;
  259. } else if (nullValue == null) {
  260. return null;
  261. }
  262. } else if (value.equals(trueValue)) {
  263. return Boolean.TRUE;
  264. } else if (value.equals(falseValue)) {
  265. return Boolean.FALSE;
  266. } else if (value.equals(nullValue)) {
  267. return null;
  268. }
  269. // no match
  270. throw new IllegalArgumentException("The Integer did not match any specified value");
  271. }
  272. // Boolean to Integer methods
  273. //-----------------------------------------------------------------------
  274. /**
  275. * <p>Converts a boolean to an int using the convention that
  276. * <code>zero</code> is <code>false</code>.</p>
  277. *
  278. * @param bool the boolean to convert
  279. * @return one if <code>true</code>, zero if <code>false</code>
  280. */
  281. public static int toInteger(boolean bool) {
  282. return (bool ? 1 : 0);
  283. }
  284. /**
  285. * <p>Converts a boolean to an Integer using the convention that
  286. * <code>zero</code> is <code>false</code>.</p>
  287. *
  288. * @param bool the boolean to convert
  289. * @return one if <code>true</code>, zero if <code>false</code>
  290. */
  291. public static Integer toIntegerObject(boolean bool) {
  292. return (bool ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO);
  293. }
  294. /**
  295. * <p>Converts a Boolean to a Integer using the convention that
  296. * <code>zero</code> is <code>false</code>.</p>
  297. *
  298. * <p><code>null</code> will be converted to <code>null</code>.</p>
  299. *
  300. * @param bool the Boolean to convert
  301. * @return one if Boolean.TRUE, zero if Boolean.FALSE, <code>null</code> if <code>null</code>
  302. */
  303. public static Integer toIntegerObject(Boolean bool) {
  304. if (bool == null) {
  305. return null;
  306. }
  307. return (bool.booleanValue() ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO);
  308. }
  309. /**
  310. * <p>Converts a boolean to an int specifying the conversion values.</p>
  311. *
  312. * @param bool the to convert
  313. * @param trueValue the value to return if <code>true</code>
  314. * @param falseValue the value to return if <code>false</code>
  315. * @return the appropriate value
  316. */
  317. public static int toInteger(boolean bool, int trueValue, int falseValue) {
  318. return (bool ? trueValue : falseValue);
  319. }
  320. /**
  321. * <p>Converts a Boolean to an int specifying the conversion values.</p>
  322. *
  323. * @param bool the Boolean to convert
  324. * @param trueValue the value to return if <code>true</code>
  325. * @param falseValue the value to return if <code>false</code>
  326. * @param nullValue the value to return if <code>null</code>
  327. * @return the appropriate value
  328. */
  329. public static int toInteger(Boolean bool, int trueValue, int falseValue, int nullValue) {
  330. if (bool == null) {
  331. return nullValue;
  332. }
  333. return (bool.booleanValue() ? trueValue : falseValue);
  334. }
  335. /**
  336. * <p>Converts a boolean to an Integer specifying the conversion values.</p>
  337. *
  338. * @param bool the to convert
  339. * @param trueValue the value to return if <code>true</code>,
  340. * may be <code>null</code>
  341. * @param falseValue the value to return if <code>false</code>,
  342. * may be <code>null</code>
  343. * @return the appropriate value
  344. */
  345. public static Integer toIntegerObject(boolean bool, Integer trueValue, Integer falseValue) {
  346. return (bool ? trueValue : falseValue);
  347. }
  348. /**
  349. * <p>Converts a Boolean to an Integer specifying the conversion values.</p>
  350. *
  351. * @param bool the Boolean to convert
  352. * @param trueValue the value to return if <code>true</code>,
  353. * may be <code>null</code>
  354. * @param falseValue the value to return if <code>false</code>,
  355. * may be <code>null</code>
  356. * @param nullValue the value to return if <code>null</code>,
  357. * may be <code>null</code>
  358. * @return the appropriate value
  359. */
  360. public static Integer toIntegerObject(Boolean bool, Integer trueValue, Integer falseValue, Integer nullValue) {
  361. if (bool == null) {
  362. return nullValue;
  363. }
  364. return (bool.booleanValue() ? trueValue : falseValue);
  365. }
  366. // String to Boolean methods
  367. //-----------------------------------------------------------------------
  368. /**
  369. * <p>Converts a String to a Boolean.</p>
  370. *
  371. * <p><code>'true'</code>, <code>'on'</code> or <code>'yes'</code>
  372. * (case insensitive) will return <code>true</code>.
  373. * <code>'false'</code>, <code>'off'</code> or <code>'no'</code>
  374. * (case insensitive) will return <code>false</code>.
  375. * Otherwise, <code>null</code> is returned.</p>
  376. *
  377. * @param str the String to check
  378. * @return the Boolean value of the string,
  379. * <code>null</code> if no match or <code>null</code> input
  380. */
  381. public static Boolean toBooleanObject(String str) {
  382. if ("true".equalsIgnoreCase(str)) {
  383. return Boolean.TRUE;
  384. } else if ("false".equalsIgnoreCase(str)) {
  385. return Boolean.FALSE;
  386. } else if ("on".equalsIgnoreCase(str)) {
  387. return Boolean.TRUE;
  388. } else if ("off".equalsIgnoreCase(str)) {
  389. return Boolean.FALSE;
  390. } else if ("yes".equalsIgnoreCase(str)) {
  391. return Boolean.TRUE;
  392. } else if ("no".equalsIgnoreCase(str)) {
  393. return Boolean.FALSE;
  394. }
  395. // no match
  396. return null;
  397. }
  398. /**
  399. * <p>Converts a String to a Boolean throwing an exception if no match.</p>
  400. *
  401. * @param str the String to check
  402. * @param trueString the String to match for <code>true</code>
  403. * (case sensitive), may be <code>null</code>
  404. * @param falseString the String to match for <code>false</code>
  405. * (case sensitive), may be <code>null</code>
  406. * @param nullString the String to match for <code>null</code>
  407. * (case sensitive), may be <code>null</code>
  408. * @return the Boolean value of the string,
  409. * <code>null</code> if no match or <code>null</code> input
  410. */
  411. public static Boolean toBooleanObject(String str, String trueString, String falseString, String nullString) {
  412. if (str == null) {
  413. if (trueString == null) {
  414. return Boolean.TRUE;
  415. } else if (falseString == null) {
  416. return Boolean.FALSE;
  417. } else if (nullString == null) {
  418. return null;
  419. }
  420. } else if (str.equals(trueString)) {
  421. return Boolean.TRUE;
  422. } else if (str.equals(falseString)) {
  423. return Boolean.FALSE;
  424. } else if (str.equals(nullString)) {
  425. return null;
  426. }
  427. // no match
  428. throw new IllegalArgumentException("The String did not match any specified value");
  429. }
  430. // String to boolean methods
  431. //-----------------------------------------------------------------------
  432. /**
  433. * <p>Converts a String to a boolean.</p>
  434. *
  435. * <p><code>'true'</code>, <code>'on'</code> or <code>'yes'</code>
  436. * (case insensitive) will return <code>true</code>. Otherwise,
  437. * <code>false</code> is returned.</p>
  438. *
  439. * @param str the String to check
  440. * @return the boolean value of the string, <code>false</code> if no match
  441. */
  442. public static boolean toBoolean(String str) {
  443. if ("true".equalsIgnoreCase(str)) {
  444. return true;
  445. } else if ("on".equalsIgnoreCase(str)) {
  446. return true;
  447. } else if ("yes".equalsIgnoreCase(str)) {
  448. return true;
  449. }
  450. // no match
  451. return false;
  452. }
  453. /**
  454. * <p>Converts a String to a Boolean throwing an exception if no match found.</p>
  455. *
  456. * <p>null is returned if there is no match.</p>
  457. *
  458. * @param str the String to check
  459. * @param trueString the String to match for <code>true</code>
  460. * (case sensitive), may be <code>null</code>
  461. * @param falseString the String to match for <code>false</code>
  462. * (case sensitive), may be <code>null</code>
  463. * @return the boolean value of the string
  464. * @throws IllegalArgumentException if the String doesn't match
  465. */
  466. public static boolean toBoolean(String str, String trueString, String falseString) {
  467. if (str == null) {
  468. if (trueString == null) {
  469. return true;
  470. } else if (falseString == null) {
  471. return false;
  472. }
  473. } else if (str.equals(trueString)) {
  474. return true;
  475. } else if (str.equals(falseString)) {
  476. return false;
  477. }
  478. // no match
  479. throw new IllegalArgumentException("The String did not match either specified value");
  480. }
  481. // Boolean to String methods
  482. //-----------------------------------------------------------------------
  483. /**
  484. * <p>Converts a Boolean to a String returning <code>'true'</code>,
  485. * <code>'false'</code>, or <code>null</code>.</p>
  486. *
  487. * @param bool the Boolean to check
  488. * @return <code>'true'</code>, <code>'false'</code>,
  489. * or <code>null</code>
  490. */
  491. public static String toStringTrueFalse(Boolean bool) {
  492. return toString(bool, "true", "false", null);
  493. }
  494. /**
  495. * <p>Converts a Boolean to a String returning <code>'on'</code>,
  496. * <code>'off'</code>, or <code>null</code>.</p>
  497. *
  498. * @param bool the Boolean to check
  499. * @return <code>'on'</code>, <code>'off'</code>,
  500. * or <code>null</code>
  501. */
  502. public static String toStringOnOff(Boolean bool) {
  503. return toString(bool, "on", "off", null);
  504. }
  505. /**
  506. * <p>Converts a Boolean to a String returning <code>'yes'</code>,
  507. * <code>'no'</code>, or <code>null</code>.</p>
  508. *
  509. * @param bool the Boolean to check
  510. * @return <code>'yes'</code>, <code>'no'</code>,
  511. * or <code>null</code>
  512. */
  513. public static String toStringYesNo(Boolean bool) {
  514. return toString(bool, "yes", "no", null);
  515. }
  516. /**
  517. * <p>Converts a Boolean to a String returning one of the input Strings.</p>
  518. *
  519. * @param bool the Boolean to check
  520. * @param trueString the String to return if <code>true</code>,
  521. * may be <code>null</code>
  522. * @param falseString the String to return if <code>false</code>,
  523. * may be <code>null</code>
  524. * @param nullString the String to return if <code>null</code>,
  525. * may be <code>null</code>
  526. * @return one of the three input Strings
  527. */
  528. public static String toString(Boolean bool, String trueString, String falseString, String nullString) {
  529. if (bool == null) {
  530. return nullString;
  531. }
  532. return (bool.booleanValue() ? trueString : falseString);
  533. }
  534. // boolean to String methods
  535. //-----------------------------------------------------------------------
  536. /**
  537. * <p>Converts a boolean to a String returning <code>'true'</code>
  538. * or <code>'false'</code>.</p>
  539. *
  540. * @param bool the Boolean to check
  541. * @return <code>'true'</code>, <code>'false'</code>,
  542. * or <code>null</code>
  543. */
  544. public static String toStringTrueFalse(boolean bool) {
  545. return toString(bool, "true", "false");
  546. }
  547. /**
  548. * <p>Converts a boolean to a String returning <code>'on'</code>
  549. * or <code>'off'</code>.</p>
  550. *
  551. * @param bool the Boolean to check
  552. * @return <code>'on'</code>, <code>'off'</code>,
  553. * or <code>null</code>
  554. */
  555. public static String toStringOnOff(boolean bool) {
  556. return toString(bool, "on", "off");
  557. }
  558. /**
  559. * <p>Converts a boolean to a String returning <code>'yes'</code>
  560. * or <code>'no'</code>.</p>
  561. *
  562. * @param bool the Boolean to check
  563. * @return <code>'yes'</code>, <code>'no'</code>,
  564. * or <code>null</code>
  565. */
  566. public static String toStringYesNo(boolean bool) {
  567. return toString(bool, "yes", "no");
  568. }
  569. /**
  570. * <p>Converts a boolean to a String returning one of the input Strings.</p>
  571. *
  572. * @param bool the Boolean to check
  573. * @param trueString the String to return if <code>true</code>,
  574. * may be <code>null</code>
  575. * @param falseString the String to return if <code>false</code>,
  576. * may be <code>null</code>
  577. * @return one of the two input Strings
  578. */
  579. public static String toString(boolean bool, String trueString, String falseString) {
  580. return (bool ? trueString : falseString);
  581. }
  582. // xor methods
  583. // ----------------------------------------------------------------------
  584. /**
  585. * <p>Performs an xor on a set of booleans.</p>
  586. *
  587. * @param array an array of <code>boolean<code>s
  588. * @return <code>true</code> if the xor is successful.
  589. * @throws IllegalArgumentException if <code>array</code> is <code>null</code>
  590. * @throws IllegalArgumentException if <code>array</code> is empty.
  591. */
  592. public static boolean xor(boolean[] array) {
  593. // Validates input
  594. if (array == null) {
  595. throw new IllegalArgumentException("The Array must not be null");
  596. } else if (array.length == 0) {
  597. throw new IllegalArgumentException("Array is empty");
  598. }
  599. // Loops through array, comparing each item
  600. int trueCount = 0;
  601. for (int i = 0; i < array.length; i++) {
  602. // If item is true, and trueCount is < 1, increments count
  603. // Else, xor fails
  604. if (array[i]) {
  605. if (trueCount < 1) {
  606. trueCount++;
  607. } else {
  608. return false;
  609. }
  610. }
  611. }
  612. // Returns true if there was exactly 1 true item
  613. return trueCount == 1;
  614. }
  615. /**
  616. * <p>Performs an xor on an array of Booleans.</p>
  617. *
  618. * @param array an array of <code>Boolean<code>s
  619. * @return <code>true</code> if the xor is successful.
  620. * @throws IllegalArgumentException if <code>array</code> is <code>null</code>
  621. * @throws IllegalArgumentException if <code>array</code> is empty.
  622. * @throws IllegalArgumentException if <code>array</code> contains a <code>null</code>
  623. */
  624. public static Boolean xor(Boolean[] array) {
  625. if (array == null) {
  626. throw new IllegalArgumentException("The Array must not be null");
  627. } else if (array.length == 0) {
  628. throw new IllegalArgumentException("Array is empty");
  629. }
  630. boolean[] primitive = null;
  631. try {
  632. primitive = ArrayUtils.toPrimitive(array);
  633. } catch (NullPointerException ex) {
  634. throw new IllegalArgumentException("The array must not conatin any null elements");
  635. }
  636. return (xor(primitive) ? Boolean.TRUE : Boolean.FALSE);
  637. }
  638. }