View Javadoc

1   /*
2      Copyright 2002-2006 Martin van den Bemt
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15  */
16  /* ====================================================================
17   * The Apache Software License, Version 1.1
18   *
19   * Copyright (c) 2002-2003 The Apache Software Foundation.  All rights
20   * reserved.
21   *
22   * Redistribution and use in source and binary forms, with or without
23   * modification, are permitted provided that the following conditions
24   * are met:
25   *
26   * 1. Redistributions of source code must retain the above copyright
27   *    notice, this list of conditions and the following disclaimer.
28   *
29   * 2. Redistributions in binary form must reproduce the above copyright
30   *    notice, this list of conditions and the following disclaimer in
31   *    the documentation and/or other materials provided with the
32   *    distribution.
33   *
34   * 3. The end-user documentation included with the redistribution, if
35   *    any, must include the following acknowledgement:
36   *       "This product includes software developed by the
37   *        Apache Software Foundation (http://www.apache.org/)."
38   *    Alternately, this acknowledgement may appear in the software itself,
39   *    if and wherever such third-party acknowledgements normally appear.
40   *
41   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
42   *    Foundation" must not be used to endorse or promote products derived
43   *    from this software without prior written permission. For written
44   *    permission, please contact apache@apache.org.
45   *
46   * 5. Products derived from this software may not be called "Apache"
47   *    nor may "Apache" appear in their names without prior written
48   *    permission of the Apache Software Foundation.
49   *
50   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
51   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
53   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
54   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
57   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
58   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
59   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
60   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61   * SUCH DAMAGE.
62   * ====================================================================
63   *
64   * This software consists of voluntary contributions made by many
65   * individuals on behalf of the Apache Software Foundation.  For more
66   * information on the Apache Software Foundation, please see
67   * <http://www.apache.org/>.
68   */
69  package org.xulux.utils;
70  
71  /**
72   * This class is a copy of BooleanUtils from jakarta commons lang, version
73   * 1.17. This is to replace the dependency of complete jakarta commons lang
74   * see http://jakarta.apache.org/commons/lang for the complete lang package.
75   *
76   * <p>Operations on boolean primitives and Boolean objects.</p>
77   *
78   * <p>This class tries to handle <code>null</code> input gracefully.
79   * An exception will not be thrown for a <code>null</code> input.
80   * Each method documents its behaviour in more detail.</p>
81   *
82   * @author Stephen Colebourne
83   * @author Matthew Hawthorne
84   * @author Gary Gregory
85   * @author Martin van den Bemt
86   * @since 2.0
87   * @version $Id: BooleanUtils.java,v 1.1 2005/12/18 12:58:15 mvdb Exp $
88   */
89  public class BooleanUtils {
90  
91      /** Reusable Integer constant for zero. */
92      public static final Integer INTEGER_ZERO = new Integer(0);
93      /** Reusable Integer constant for one. */
94      public static final Integer INTEGER_ONE = new Integer(1);
95  
96      /**
97       * <p><code>BooleanUtils</code> instances should NOT be constructed in standard programming.
98       * Instead, the class should be used as <code>BooleanUtils.toBooleanObject(true);</code>.</p>
99       *
100      * <p>This constructor is public to permit tools that require a JavaBean instance
101      * to operate.</p>
102      */
103     public BooleanUtils() {
104     }
105 
106     // Boolean utilities
107     //--------------------------------------------------------------------------
108     /**
109      * <p>Negates the specified boolean.</p>
110      *
111      * <p>If <code>null</code> is passed in, <code>null</code> will be returned.</p>
112      *
113      * <pre>
114      *   BooleanUtils.negate(Boolean.TRUE)  = Boolean.FALSE;
115      *   BooleanUtils.negate(Boolean.FALSE) = Boolean.TRUE;
116      *   BooleanUtils.negate(null)          = null;
117      * </pre>
118      *
119      * @param bool  the Boolean to negate, may be null
120      * @return the negated Boolean, or <code>null</code> if <code>null</code> input
121      */
122     public static Boolean negate(Boolean bool) {
123         if (bool == null) {
124             return null;
125         }
126         return (bool.booleanValue() ? Boolean.FALSE : Boolean.TRUE);
127     }
128 
129     // boolean Boolean methods
130     //-----------------------------------------------------------------------
131     /**
132      * <p>Boolean factory that avoids creating new Boolean objecs all the time.</p>
133      *
134      * <p>This method was added to JDK1.4 but is available here for earlier JDKs.</p>
135      *
136      * <pre>
137      *   BooleanUtils.toBooleanObject(false) = Boolean.FALSE
138      *   BooleanUtils.toBooleanObject(true)  = Boolean.TRUE
139      * </pre>
140      *
141      * @param bool  the boolean to convert
142      * @return Boolean.TRUE or Boolean.FALSE as appropriate
143      */
144     public static Boolean toBooleanObject(boolean bool) {
145         return (bool ? Boolean.TRUE : Boolean.FALSE);
146     }
147 
148     /**
149      * <p>Converts a Boolean to a boolean handling <code>null</code>
150      * by returning <code>false</code>.</p>
151      *
152      * <pre>
153      *   BooleanUtils.toBoolean(Boolean.TRUE)  = true
154      *   BooleanUtils.toBoolean(Boolean.FALSE) = false
155      *   BooleanUtils.toBoolean(null)          = false
156      * </pre>
157      *
158      * @param bool  the boolean to convert
159      * @return <code>true</code> or <code>false</code>,
160      *  <code>null</code> returns <code>false</code>
161      */
162     public static boolean toBoolean(Boolean bool) {
163         if (bool == null) {
164             return false;
165         }
166         return (bool.booleanValue() ? true : false);
167     }
168 
169     /**
170      * <p>Converts a Boolean to a boolean handling <code>null</code>.</p>
171      *
172      * <pre>
173      *   BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, false) = true
174      *   BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, true) = false
175      *   BooleanUtils.toBooleanDefaultIfNull(null, true)          = true
176      * </pre>
177      *
178      * @param bool  the boolean to convert
179      * @param valueIfNull  the boolean value to return if <code>null</code>
180      * @return <code>true</code> or <code>false</code>
181      */
182     public static boolean toBooleanDefaultIfNull(Boolean bool, boolean valueIfNull) {
183         if (bool == null) {
184             return valueIfNull;
185         }
186         return (bool.booleanValue() ? true : false);
187     }
188 
189     // Integer to Boolean methods
190     //-----------------------------------------------------------------------
191     /**
192      * <p>Converts an int to a boolean using the convention that <code>zero</code>
193      * is <code>false</code>.</p>
194      *
195      * <pre>
196      *   BooleanUtils.toBoolean(0) = false
197      *   BooleanUtils.toBoolean(1) = true
198      *   BooleanUtils.toBoolean(2) = true
199      * </pre>
200      *
201      * @param value  the int to convert
202      * @return <code>true</code> if non-zero, <code>false</code>
203      *  if zero
204      */
205     public static boolean toBoolean(int value) {
206         return (value == 0 ? false : true);
207     }
208 
209     /**
210      * <p>Converts an int to a Boolean using the convention that <code>zero</code>
211      * is <code>false</code>.</p>
212      *
213      * <pre>
214      *   BooleanUtils.toBoolean(0) = Boolean.FALSE
215      *   BooleanUtils.toBoolean(1) = Boolean.TRUE
216      *   BooleanUtils.toBoolean(2) = Boolean.TRUE
217      * </pre>
218      *
219      * @param value  the int to convert
220      * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero,
221      *  <code>null</code> if <code>null</code>
222      */
223     public static Boolean toBooleanObject(int value) {
224         return (value == 0 ? Boolean.FALSE : Boolean.TRUE);
225     }
226 
227     /**
228      * <p>Converts an Integer to a Boolean using the convention that <code>zero</code>
229      * is <code>false</code>.</p>
230      *
231      * <p><code>null</code> will be converted to <code>null</code>.</p>
232      *
233      * <pre>
234      *   BooleanUtils.toBoolean(new Integer(0))    = Boolean.FALSE
235      *   BooleanUtils.toBoolean(new Integer(1))    = Boolean.TRUE
236      *   BooleanUtils.toBoolean(new Integer(null)) = null
237      * </pre>
238      *
239      * @param value  the Integer to convert
240      * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero,
241      *  <code>null</code> if <code>null</code> input
242      */
243     public static Boolean toBooleanObject(Integer value) {
244         if (value == null) {
245             return null;
246         }
247         return (value.intValue() == 0 ? Boolean.FALSE : Boolean.TRUE);
248     }
249 
250     /**
251      * <p>Converts an int to a boolean specifying the conversion values.</p>
252      *
253      * <pre>
254      *   BooleanUtils.toBoolean(0, 1, 0) = false
255      *   BooleanUtils.toBoolean(1, 1, 0) = true
256      *   BooleanUtils.toBoolean(2, 1, 2) = false
257      *   BooleanUtils.toBoolean(2, 2, 0) = true
258      * </pre>
259      *
260      * @param value  the Integer to convert
261      * @param trueValue  the value to match for <code>true</code>
262      * @param falseValue  the value to match for <code>false</code>
263      * @return <code>true</code> or <code>false</code>
264      * @throws IllegalArgumentException if no match
265      */
266     public static boolean toBoolean(int value, int trueValue, int falseValue) {
267         if (value == trueValue) {
268             return true;
269         } else if (value == falseValue) {
270             return false;
271         }
272         // no match
273         throw new IllegalArgumentException("The Integer did not match either specified value");
274     }
275 
276     /**
277      * <p>Converts an Integer to a boolean specifying the conversion values.</p>
278      *
279      * <pre>
280      *   BooleanUtils.toBoolean(new Integer(0), new Integer(1), new Integer(0)) = false
281      *   BooleanUtils.toBoolean(new Integer(1), new Integer(1), new Integer(0)) = true
282      *   BooleanUtils.toBoolean(new Integer(2), new Integer(1), new Integer(2)) = false
283      *   BooleanUtils.toBoolean(new Integer(2), new Integer(2), new Integer(0)) = true
284      *   BooleanUtils.toBoolean(null, null, new Integer(0))                     = true
285      * </pre>
286      *
287      * @param value  the Integer to convert
288      * @param trueValue  the value to match for <code>true</code>,
289      *  may be <code>null</code>
290      * @param falseValue  the value to match for <code>false</code>,
291      *  may be <code>null</code>
292      * @return <code>true</code> or <code>false</code>
293      * @throws IllegalArgumentException if no match
294      */
295     public static boolean toBoolean(Integer value, Integer trueValue, Integer falseValue) {
296         if (value == null) {
297             if (trueValue == null) {
298                 return true;
299             } else if (falseValue == null) {
300                 return false;
301             }
302         } else if (value.equals(trueValue)) {
303             return true;
304         } else if (value.equals(falseValue)) {
305             return false;
306         }
307         // no match
308         throw new IllegalArgumentException("The Integer did not match either specified value");
309     }
310 
311     /**
312      * <p>Converts an int to a Boolean specifying the conversion values.</p>
313      *
314      * <pre>
315      *   BooleanUtils.toBooleanObject(0, 0, 2, 3) = Boolean.TRUE
316      *   BooleanUtils.toBooleanObject(2, 1, 2, 3) = Boolean.FALSE
317      *   BooleanUtils.toBooleanObject(3, 1, 2, 3) = null
318      * </pre>
319      *
320      * @param value  the Integer to convert
321      * @param trueValue  the value to match for <code>true</code>
322      * @param falseValue  the value to match for <code>false</code>
323      * @param nullValue  the value to to match for <code>null</code>
324      * @return Boolean.TRUE, Boolean.FALSE, or <code>null</code>
325      * @throws IllegalArgumentException if no match
326      */
327     public static Boolean toBooleanObject(int value, int trueValue, int falseValue, int nullValue) {
328         if (value == trueValue) {
329             return Boolean.TRUE;
330         } else if (value == falseValue) {
331             return Boolean.FALSE;
332         } else if (value == nullValue) {
333             return null;
334         }
335         // no match
336         throw new IllegalArgumentException("The Integer did not match any specified value");
337     }
338 
339     /**
340      * <p>Converts an Integer to a Boolean specifying the conversion values.</p>
341      *
342      * <pre>
343      *   BooleanUtils.toBooleanObject(new Integer(0), new Integer(0), new Integer(2), new Integer(3)) = Boolean.TRUE
344      *   BooleanUtils.toBooleanObject(new Integer(2), new Integer(1), new Integer(2), new Integer(3)) = Boolean.FALSE
345      *   BooleanUtils.toBooleanObject(new Integer(3), new Integer(1), new Integer(2), new Integer(3)) = null
346      * </pre>
347      *
348      * @param value  the Integer to convert
349      * @param trueValue  the value to match for <code>true</code>,
350      *  may be <code>null</code>
351      * @param falseValue  the value to match for <code>false</code>,
352      *  may be <code>null</code>
353      * @param nullValue  the value to to match for <code>null</code>,
354      *  may be <code>null</code>
355      * @return Boolean.TRUE, Boolean.FALSE, or <code>null</code>
356      * @throws IllegalArgumentException if no match
357      */
358     public static Boolean toBooleanObject(Integer value, Integer trueValue, Integer falseValue, Integer nullValue) {
359         if (value == null) {
360             if (trueValue == null) {
361                 return Boolean.TRUE;
362             } else if (falseValue == null) {
363                 return Boolean.FALSE;
364             } else if (nullValue == null) {
365                 return null;
366             }
367         } else if (value.equals(trueValue)) {
368             return Boolean.TRUE;
369         } else if (value.equals(falseValue)) {
370             return Boolean.FALSE;
371         } else if (value.equals(nullValue)) {
372             return null;
373         }
374         // no match
375         throw new IllegalArgumentException("The Integer did not match any specified value");
376     }
377 
378     // Boolean to Integer methods
379     //-----------------------------------------------------------------------
380     /**
381      * <p>Converts a boolean to an int using the convention that
382      * <code>zero</code> is <code>false</code>.</p>
383      *
384      * <pre>
385      *   BooleanUtils.toInteger(true)  = 1
386      *   BooleanUtils.toInteger(false) = 0
387      * </pre>
388      *
389      * @param bool  the boolean to convert
390      * @return one if <code>true</code>, zero if <code>false</code>
391      */
392     public static int toInteger(boolean bool) {
393         return (bool ? 1 : 0);
394     }
395 
396     /**
397      * <p>Converts a boolean to an Integer using the convention that
398      * <code>zero</code> is <code>false</code>.</p>
399      *
400      * <pre>
401      *   BooleanUtils.toIntegerObject(true)  = new Integer(1)
402      *   BooleanUtils.toIntegerObject(false) = new Integer(0)
403      * </pre>
404      *
405      * @param bool  the boolean to convert
406      * @return one if <code>true</code>, zero if <code>false</code>
407      */
408     public static Integer toIntegerObject(boolean bool) {
409         return (bool ? INTEGER_ONE : INTEGER_ZERO);
410     }
411 
412     /**
413      * <p>Converts a Boolean to a Integer using the convention that
414      * <code>zero</code> is <code>false</code>.</p>
415      *
416      * <p><code>null</code> will be converted to <code>null</code>.</p>
417      *
418      * <pre>
419      *   BooleanUtils.toIntegerObject(Boolean.TRUE)  = new Integer(1)
420      *   BooleanUtils.toIntegerObject(Boolean.FALSE) = new Integer(0)
421      * </pre>
422      *
423      * @param bool  the Boolean to convert
424      * @return one if Boolean.TRUE, zero if Boolean.FALSE, <code>null</code> if <code>null</code>
425      */
426     public static Integer toIntegerObject(Boolean bool) {
427         if (bool == null) {
428             return null;
429         }
430         return (bool.booleanValue() ? INTEGER_ONE : INTEGER_ZERO);
431     }
432 
433     /**
434      * <p>Converts a boolean to an int specifying the conversion values.</p>
435      *
436      * <pre>
437      *   BooleanUtils.toInteger(true, 1, 0)  = 1
438      *   BooleanUtils.toInteger(false, 1, 0) = 0
439      * </pre>
440      *
441      * @param bool  the to convert
442      * @param trueValue  the value to return if <code>true</code>
443      * @param falseValue  the value to return if <code>false</code>
444      * @return the appropriate value
445      */
446     public static int toInteger(boolean bool, int trueValue, int falseValue) {
447         return (bool ? trueValue : falseValue);
448     }
449 
450     /**
451      * <p>Converts a Boolean to an int specifying the conversion values.</p>
452      *
453      * <pre>
454      *   BooleanUtils.toInteger(Boolean.TRUE, 1, 0, 2)  = 1
455      *   BooleanUtils.toInteger(Boolean.FALSE, 1, 0, 2) = 0
456      *   BooleanUtils.toInteger(null, 1, 0, 2)          = 2
457      * </pre>
458      *
459      * @param bool  the Boolean to convert
460      * @param trueValue  the value to return if <code>true</code>
461      * @param falseValue  the value to return if <code>false</code>
462      * @param nullValue  the value to return if <code>null</code>
463      * @return the appropriate value
464      */
465     public static int toInteger(Boolean bool, int trueValue, int falseValue, int nullValue) {
466         if (bool == null) {
467             return nullValue;
468         }
469         return (bool.booleanValue() ? trueValue : falseValue);
470     }
471 
472     /**
473      * <p>Converts a boolean to an Integer specifying the conversion values.</p>
474      *
475      * <pre>
476      *   BooleanUtils.toIntegerObject(true, new Integer(1), new Integer(0))  = new Integer(1)
477      *   BooleanUtils.toIntegerObject(false, new Integer(1), new Integer(0)) = new Integer(0)
478      * </pre>
479      *
480      * @param bool  the to convert
481      * @param trueValue  the value to return if <code>true</code>,
482      *  may be <code>null</code>
483      * @param falseValue  the value to return if <code>false</code>,
484      *  may be <code>null</code>
485      * @return the appropriate value
486      */
487     public static Integer toIntegerObject(boolean bool, Integer trueValue, Integer falseValue) {
488         return (bool ? trueValue : falseValue);
489     }
490 
491     /**
492      * <p>Converts a Boolean to an Integer specifying the conversion values.</p>
493      *
494      * <pre>
495      *   BooleanUtils.toIntegerObject(Boolean.TRUE, new Integer(1), new Integer(0), new Integer(2))  = new Integer(1)
496      *   BooleanUtils.toIntegerObject(Boolean.FALSE, new Integer(1), new Integer(0), new Integer(2)) = new Integer(0)
497      *   BooleanUtils.toIntegerObject(null, new Integer(1), new Integer(0), new Integer(2))          = new Integer(2)
498      * </pre>
499      *
500      * @param bool  the Boolean to convert
501      * @param trueValue  the value to return if <code>true</code>,
502      *  may be <code>null</code>
503      * @param falseValue  the value to return if <code>false</code>,
504      *  may be <code>null</code>
505      * @param nullValue  the value to return if <code>null</code>,
506      *  may be <code>null</code>
507      * @return the appropriate value
508      */
509     public static Integer toIntegerObject(Boolean bool, Integer trueValue, Integer falseValue, Integer nullValue) {
510         if (bool == null) {
511             return nullValue;
512         }
513         return (bool.booleanValue() ? trueValue : falseValue);
514     }
515 
516     // String to Boolean methods
517     //-----------------------------------------------------------------------
518     /**
519      * <p>Converts a String to a Boolean.</p>
520      *
521      * <p><code>'true'</code>, <code>'on'</code> or <code>'yes'</code>
522      * (case insensitive) will return <code>true</code>.
523      * <code>'false'</code>, <code>'off'</code> or <code>'no'</code>
524      * (case insensitive) will return <code>false</code>.
525      * Otherwise, <code>null</code> is returned.</p>
526      *
527      * <pre>
528      *   BooleanUtils.toBooleanObject(null)    = null
529      *   BooleanUtils.toBooleanObject("true")  = Boolean.TRUE
530      *   BooleanUtils.toBooleanObject("false") = Boolean.FALSE
531      *   BooleanUtils.toBooleanObject("on")    = Boolean.TRUE
532      *   BooleanUtils.toBooleanObject("ON")    = Boolean.TRUE
533      *   BooleanUtils.toBooleanObject("off")   = Boolean.FALSE
534      *   BooleanUtils.toBooleanObject("oFf")   = Boolean.FALSE
535      *   BooleanUtils.toBooleanObject("blue")  = null
536      * </pre>
537      *
538      * @param str  the String to check
539      * @return the Boolean value of the string,
540      *  <code>null</code> if no match or <code>null</code> input
541      */
542     public static Boolean toBooleanObject(String str) {
543         if ("true".equalsIgnoreCase(str)) {
544             return Boolean.TRUE;
545         } else if ("false".equalsIgnoreCase(str)) {
546             return Boolean.FALSE;
547         } else if ("on".equalsIgnoreCase(str)) {
548             return Boolean.TRUE;
549         } else if ("off".equalsIgnoreCase(str)) {
550             return Boolean.FALSE;
551         } else if ("yes".equalsIgnoreCase(str)) {
552             return Boolean.TRUE;
553         } else if ("no".equalsIgnoreCase(str)) {
554             return Boolean.FALSE;
555         }
556         // no match
557         return null;
558     }
559 
560     /**
561      * <p>Converts a String to a Boolean throwing an exception if no match.</p>
562      *
563      * <pre>
564      *   BooleanUtils.toBooleanObject("true", "true", "false", "null")  = Boolean.TRUE
565      *   BooleanUtils.toBooleanObject("false", "true", "false", "null") = Boolean.FALSE
566      *   BooleanUtils.toBooleanObject("null", "true", "false", "null")  = null
567      * </pre>
568      *
569      * @param str  the String to check
570      * @param trueString  the String to match for <code>true</code>
571      *  (case sensitive), may be <code>null</code>
572      * @param falseString  the String to match for <code>false</code>
573      *  (case sensitive), may be <code>null</code>
574      * @param nullString  the String to match for <code>null</code>
575      *  (case sensitive), may be <code>null</code>
576      * @return the Boolean value of the string,
577      *  <code>null</code> if no match or <code>null</code> input
578      */
579     public static Boolean toBooleanObject(String str, String trueString, String falseString, String nullString) {
580         if (str == null) {
581             if (trueString == null) {
582                 return Boolean.TRUE;
583             } else if (falseString == null) {
584                 return Boolean.FALSE;
585             } else if (nullString == null) {
586                 return null;
587             }
588         } else if (str.equals(trueString)) {
589             return Boolean.TRUE;
590         } else if (str.equals(falseString)) {
591             return Boolean.FALSE;
592         } else if (str.equals(nullString)) {
593             return null;
594         }
595         // no match
596         throw new IllegalArgumentException("The String did not match any specified value");
597     }
598 
599     // String to boolean methods
600     //-----------------------------------------------------------------------
601     /**
602      * <p>Converts a String to a boolean (optimised for performance).</p>
603      *
604      * <p><code>'true'</code>, <code>'on'</code> or <code>'yes'</code>
605      * (case insensitive) will return <code>true</code>. Otherwise,
606      * <code>false</code> is returned.</p>
607      *
608      * <p>This method performs 4 times faster (JDK1.4) than
609      * <code>Boolean.valueOf(String)</code>. However, this method accepts
610      * 'on' and 'yes' as true values.
611      *
612      * <pre>
613      *   BooleanUtils.toBoolean(null)    = false
614      *   BooleanUtils.toBoolean("true")  = true
615      *   BooleanUtils.toBoolean("TRUE")  = true
616      *   BooleanUtils.toBoolean("tRUe")  = true
617      *   BooleanUtils.toBoolean("on")    = true
618      *   BooleanUtils.toBoolean("yes")   = true
619      *   BooleanUtils.toBoolean("false") = false
620      *   BooleanUtils.toBoolean("x gti") = false
621      * </pre>
622      *
623      * @param str  the String to check
624      * @return the boolean value of the string, <code>false</code> if no match
625      */
626     public static boolean toBoolean(String str) {
627         // Previously used equalsIgnoreCase, which was fast for interned 'true'.
628         // Non interned 'true' matched 15 times slower.
629         //
630         // Optimisation provides same performance as before for interned 'true'.
631         // Similar performance for null, 'false', and other strings not length 2/3/4.
632         // 'true'/'TRUE' match 4 times slower, 'tRUE'/'True' 7 times slower.
633         if (str == "true") {
634             return true;
635         }
636         if (str == null) {
637             return false;
638         }
639         switch (str.length()) {
640             case 2 :
641                 {
642                     char ch0 = str.charAt(0);
643                     char ch1 = str.charAt(1);
644                     return (ch0 == 'o' || ch0 == 'O') && (ch1 == 'n' || ch1 == 'N');
645                 }
646             case 3 :
647                 {
648                     char ch = str.charAt(0);
649                     if (ch == 'y') {
650                         return (str.charAt(1) == 'e' || str.charAt(1) == 'E') && (str.charAt(2) == 's' || str.charAt(2) == 'S');
651                     }
652                     if (ch == 'Y') {
653                         return (str.charAt(1) == 'E' || str.charAt(1) == 'e') && (str.charAt(2) == 'S' || str.charAt(2) == 's');
654                     }
655                 }
656             case 4 :
657                 {
658                     char ch = str.charAt(0);
659                     if (ch == 't') {
660                         return (str.charAt(1) == 'r' || str.charAt(1) == 'R')
661                             && (str.charAt(2) == 'u' || str.charAt(2) == 'U')
662                             && (str.charAt(3) == 'e' || str.charAt(3) == 'E');
663                     }
664                     if (ch == 'T') {
665                         return (str.charAt(1) == 'R' || str.charAt(1) == 'r')
666                             && (str.charAt(2) == 'U' || str.charAt(2) == 'u')
667                             && (str.charAt(3) == 'E' || str.charAt(3) == 'e');
668                     }
669                 }
670         }
671         return false;
672     }
673 
674     /**
675      * Since xulux works with objects and we don't want to figure
676      * out the type beforehand, we have a stub that is calling
677      * the string and boolean toBoolean methods.
678      *
679      * @param object - if it is null it will return false by default.
680      * @return the restult of the conversion.
681      * @throws IllegalArgumentException - when the type has no booleanconverter.
682      */
683     public static boolean toBoolean(Object object) {
684         if (object == null) {
685             return false;
686         }
687         if (object instanceof String) {
688             return toBoolean((String) object);
689         } else if (object instanceof Boolean) {
690             return toBoolean((Boolean) object);
691         } else {
692             throw new IllegalArgumentException("Cannot convert to boolean");
693         }
694     }
695 
696     /**
697      * <p>Converts a String to a Boolean throwing an exception if no match found.</p>
698      *
699      * <p>null is returned if there is no match.</p>
700      *
701      * <pre>
702      *   BooleanUtils.toBoolean("true", "true", "false")  = true
703      *   BooleanUtils.toBoolean("false", "true", "false") = false
704      * </pre>
705      *
706      * @param str  the String to check
707      * @param trueString  the String to match for <code>true</code>
708      *  (case sensitive), may be <code>null</code>
709      * @param falseString  the String to match for <code>false</code>
710      *  (case sensitive), may be <code>null</code>
711      * @return the boolean value of the string
712      * @throws IllegalArgumentException if the String doesn't match
713      */
714     public static boolean toBoolean(String str, String trueString, String falseString) {
715         if (str == null) {
716             if (trueString == null) {
717                 return true;
718             } else if (falseString == null) {
719                 return false;
720             }
721         } else if (str.equals(trueString)) {
722             return true;
723         } else if (str.equals(falseString)) {
724             return false;
725         }
726         // no match
727         throw new IllegalArgumentException("The String did not match either specified value");
728     }
729 
730     // Boolean to String methods
731     //-----------------------------------------------------------------------
732     /**
733      * <p>Converts a Boolean to a String returning <code>'true'</code>,
734      * <code>'false'</code>, or <code>null</code>.</p>
735      *
736      * <pre>
737      *   BooleanUtils.toStringTrueFalse(Boolean.TRUE)  = "true"
738      *   BooleanUtils.toStringTrueFalse(Boolean.FALSE) = "false"
739      *   BooleanUtils.toStringTrueFalse(null)          = null;
740      * </pre>
741      *
742      * @param bool  the Boolean to check
743      * @return <code>'true'</code>, <code>'false'</code>,
744      *  or <code>null</code>
745      */
746     public static String toStringTrueFalse(Boolean bool) {
747         return toString(bool, "true", "false", null);
748     }
749 
750     /**
751      * <p>Converts a Boolean to a String returning <code>'on'</code>,
752      * <code>'off'</code>, or <code>null</code>.</p>
753      *
754      * <pre>
755      *   BooleanUtils.toStringOnOff(Boolean.TRUE)  = "on"
756      *   BooleanUtils.toStringOnOff(Boolean.FALSE) = "off"
757      *   BooleanUtils.toStringOnOff(null)          = null;
758      * </pre>
759      *
760      * @param bool  the Boolean to check
761      * @return <code>'on'</code>, <code>'off'</code>,
762      *  or <code>null</code>
763      */
764     public static String toStringOnOff(Boolean bool) {
765         return toString(bool, "on", "off", null);
766     }
767 
768     /**
769      * <p>Converts a Boolean to a String returning <code>'yes'</code>,
770      * <code>'no'</code>, or <code>null</code>.</p>
771      *
772      * <pre>
773      *   BooleanUtils.toStringYesNo(Boolean.TRUE)  = "yes"
774      *   BooleanUtils.toStringYesNo(Boolean.FALSE) = "no"
775      *   BooleanUtils.toStringYesNo(null)          = null;
776      * </pre>
777      *
778      * @param bool  the Boolean to check
779      * @return <code>'yes'</code>, <code>'no'</code>,
780      *  or <code>null</code>
781      */
782     public static String toStringYesNo(Boolean bool) {
783         return toString(bool, "yes", "no", null);
784     }
785 
786     /**
787      * <p>Converts a Boolean to a String returning one of the input Strings.</p>
788      *
789      * <pre>
790      *   BooleanUtils.toString(Boolean.TRUE, "true", "false", null)   = "true"
791      *   BooleanUtils.toString(Boolean.FALSE, "true", "false", null)  = "false"
792      *   BooleanUtils.toString(null, "true", "false", null)           = null;
793      * </pre>
794      *
795      * @param bool  the Boolean to check
796      * @param trueString  the String to return if <code>true</code>,
797      *  may be <code>null</code>
798      * @param falseString  the String to return if <code>false</code>,
799      *  may be <code>null</code>
800      * @param nullString  the String to return if <code>null</code>,
801      *  may be <code>null</code>
802      * @return one of the three input Strings
803      */
804     public static String toString(Boolean bool, String trueString, String falseString, String nullString) {
805         if (bool == null) {
806             return nullString;
807         }
808         return (bool.booleanValue() ? trueString : falseString);
809     }
810 
811     // boolean to String methods
812     //-----------------------------------------------------------------------
813     /**
814      * <p>Converts a boolean to a String returning <code>'true'</code>
815      * or <code>'false'</code>.</p>
816      *
817      * <pre>
818      *   BooleanUtils.toStringTrueFalse(true)   = "true"
819      *   BooleanUtils.toStringTrueFalse(false)  = "false"
820      * </pre>
821      *
822      * @param bool  the Boolean to check
823      * @return <code>'true'</code>, <code>'false'</code>,
824      *  or <code>null</code>
825      */
826     public static String toStringTrueFalse(boolean bool) {
827         return toString(bool, "true", "false");
828     }
829 
830     /**
831      * <p>Converts a boolean to a String returning <code>'on'</code>
832      * or <code>'off'</code>.</p>
833      *
834      * <pre>
835      *   BooleanUtils.toStringOnOff(true)   = "on"
836      *   BooleanUtils.toStringOnOff(false)  = "off"
837      * </pre>
838      *
839      * @param bool  the Boolean to check
840      * @return <code>'on'</code>, <code>'off'</code>,
841      *  or <code>null</code>
842      */
843     public static String toStringOnOff(boolean bool) {
844         return toString(bool, "on", "off");
845     }
846 
847     /**
848      * <p>Converts a boolean to a String returning <code>'yes'</code>
849      * or <code>'no'</code>.</p>
850      *
851      * <pre>
852      *   BooleanUtils.toStringYesNo(true)   = "yes"
853      *   BooleanUtils.toStringYesNo(false)  = "no"
854      * </pre>
855      *
856      * @param bool  the Boolean to check
857      * @return <code>'yes'</code>, <code>'no'</code>,
858      *  or <code>null</code>
859      */
860     public static String toStringYesNo(boolean bool) {
861         return toString(bool, "yes", "no");
862     }
863 
864     /**
865      * <p>Converts a boolean to a String returning one of the input Strings.</p>
866      *
867      * <pre>
868      *   BooleanUtils.toString(true, "true", "false")   = "true"
869      *   BooleanUtils.toString(false, "true", "false")  = "false"
870      * </pre>
871      *
872      * @param bool  the Boolean to check
873      * @param trueString  the String to return if <code>true</code>,
874      *  may be <code>null</code>
875      * @param falseString  the String to return if <code>false</code>,
876      *  may be <code>null</code>
877      * @return one of the two input Strings
878      */
879     public static String toString(boolean bool, String trueString, String falseString) {
880         return (bool ? trueString : falseString);
881     }
882 
883     // xor methods
884     // ----------------------------------------------------------------------
885     /**
886      * <p>Performs an xor on a set of booleans.</p>
887      *
888      * <pre>
889      *   BooleanUtils.xor(new boolean[] { true, true })   = false
890      *   BooleanUtils.xor(new boolean[] { false, false }) = false
891      *   BooleanUtils.xor(new boolean[] { true, false })  = true
892      * </pre>
893      *
894      * @param array  an array of <code>boolean<code>s
895      * @return <code>true</code> if the xor is successful.
896      * @throws IllegalArgumentException if <code>array</code> is <code>null</code>
897      * @throws IllegalArgumentException if <code>array</code> is empty.
898      */
899     public static boolean xor(boolean[] array) {
900         // Validates input
901         if (array == null) {
902             throw new IllegalArgumentException("The Array must not be null");
903         } else if (array.length == 0) {
904             throw new IllegalArgumentException("Array is empty");
905         }
906 
907         // Loops through array, comparing each item
908         int trueCount = 0;
909         for (int i = 0; i < array.length; i++) {
910             // If item is true, and trueCount is < 1, increments count
911             // Else, xor fails
912             if (array[i]) {
913                 if (trueCount < 1) {
914                     trueCount++;
915                 } else {
916                     return false;
917                 }
918             }
919         }
920 
921         // Returns true if there was exactly 1 true item
922         return trueCount == 1;
923     }
924 }