KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > BooleanUtils


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
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 package org.apache.commons.lang;
17
18 import org.apache.commons.lang.math.NumberUtils;
19
20 /**
21  * <p>Operations on boolean primitives and Boolean objects.</p>
22  *
23  * <p>This class tries to handle <code>null</code> input gracefully.
24  * An exception will not be thrown for a <code>null</code> input.
25  * Each method documents its behaviour in more detail.</p>
26  *
27  * @author Stephen Colebourne
28  * @author Matthew Hawthorne
29  * @author Gary Gregory
30  * @since 2.0
31  * @version $Id: BooleanUtils.java 161243 2005-04-14 04:30:28Z ggregory $
32  */

33 public class BooleanUtils {
34
35     /**
36      * <p><code>BooleanUtils</code> instances should NOT be constructed in standard programming.
37      * Instead, the class should be used as <code>BooleanUtils.toBooleanObject(true);</code>.</p>
38      *
39      * <p>This constructor is public to permit tools that require a JavaBean instance
40      * to operate.</p>
41      */

42     public BooleanUtils() {
43     }
44
45     // Boolean utilities
46
//--------------------------------------------------------------------------
47
/**
48      * <p>Negates the specified boolean.</p>
49      *
50      * <p>If <code>null</code> is passed in, <code>null</code> will be returned.</p>
51      *
52      * <pre>
53      * BooleanUtils.negate(Boolean.TRUE) = Boolean.FALSE;
54      * BooleanUtils.negate(Boolean.FALSE) = Boolean.TRUE;
55      * BooleanUtils.negate(null) = null;
56      * </pre>
57      *
58      * @param bool the Boolean to negate, may be null
59      * @return the negated Boolean, or <code>null</code> if <code>null</code> input
60      */

61     public static Boolean JavaDoc negate(Boolean JavaDoc bool) {
62         if (bool == null) {
63             return null;
64         }
65         return (bool.booleanValue() ? Boolean.FALSE : Boolean.TRUE);
66     }
67     
68     // boolean Boolean methods
69
//-----------------------------------------------------------------------
70
/**
71      * <p>Is a Boolean value <code>true</code>, handling <code>null</code>.</p>
72      *
73      * <pre>
74      * BooleanUtils.isTrue(Boolean.TRUE) = true
75      * BooleanUtils.isTrue(Boolean.FALSE) = false
76      * BooleanUtils.isTrue(null) = false
77      * </pre>
78      *
79      * @param bool the boolean to convert
80      * @return <code>true</code> only if the input is non-null and true
81      * @since 2.1
82      */

83     public static boolean isTrue(Boolean JavaDoc bool) {
84         if (bool == null) {
85             return false;
86         }
87         return bool.booleanValue() ? true : false;
88     }
89
90     /**
91      * <p>Is a Boolean value <code>false</code>, handling <code>null</code>.</p>
92      *
93      * <pre>
94      * BooleanUtils.isFalse(Boolean.TRUE) = false
95      * BooleanUtils.isFalse(Boolean.FALSE) = true
96      * BooleanUtils.isFalse(null) = false
97      * </pre>
98      *
99      * @param bool the boolean to convert
100      * @return <code>true</code> only if the input is non-null and false
101      * @since 2.1
102      */

103     public static boolean isFalse(Boolean JavaDoc bool) {
104         if (bool == null) {
105             return false;
106         }
107         return bool.booleanValue() ? false : true;
108     }
109
110     /**
111      * <p>Boolean factory that avoids creating new Boolean objecs all the time.</p>
112      *
113      * <p>This method was added to JDK1.4 but is available here for earlier JDKs.</p>
114      *
115      * <pre>
116      * BooleanUtils.toBooleanObject(false) = Boolean.FALSE
117      * BooleanUtils.toBooleanObject(true) = Boolean.TRUE
118      * </pre>
119      *
120      * @param bool the boolean to convert
121      * @return Boolean.TRUE or Boolean.FALSE as appropriate
122      */

123     public static Boolean JavaDoc toBooleanObject(boolean bool) {
124         return bool ? Boolean.TRUE : Boolean.FALSE;
125     }
126     
127     /**
128      * <p>Converts a Boolean to a boolean handling <code>null</code>
129      * by returning <code>false</code>.</p>
130      *
131      * <pre>
132      * BooleanUtils.toBoolean(Boolean.TRUE) = true
133      * BooleanUtils.toBoolean(Boolean.FALSE) = false
134      * BooleanUtils.toBoolean(null) = false
135      * </pre>
136      *
137      * @param bool the boolean to convert
138      * @return <code>true</code> or <code>false</code>,
139      * <code>null</code> returns <code>false</code>
140      */

141     public static boolean toBoolean(Boolean JavaDoc bool) {
142         if (bool == null) {
143             return false;
144         }
145         return bool.booleanValue() ? true : false;
146     }
147     
148     /**
149      * <p>Converts a Boolean to a boolean handling <code>null</code>.</p>
150      *
151      * <pre>
152      * BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, false) = true
153      * BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, true) = false
154      * BooleanUtils.toBooleanDefaultIfNull(null, true) = true
155      * </pre>
156      *
157      * @param bool the boolean to convert
158      * @param valueIfNull the boolean value to return if <code>null</code>
159      * @return <code>true</code> or <code>false</code>
160      */

161     public static boolean toBooleanDefaultIfNull(Boolean JavaDoc bool, boolean valueIfNull) {
162         if (bool == null) {
163             return valueIfNull;
164         }
165         return bool.booleanValue() ? true : false;
166     }
167     
168     // Integer to Boolean methods
169
//-----------------------------------------------------------------------
170
/**
171      * <p>Converts an int to a boolean using the convention that <code>zero</code>
172      * is <code>false</code>.</p>
173      *
174      * <pre>
175      * BooleanUtils.toBoolean(0) = false
176      * BooleanUtils.toBoolean(1) = true
177      * BooleanUtils.toBoolean(2) = true
178      * </pre>
179      *
180      * @param value the int to convert
181      * @return <code>true</code> if non-zero, <code>false</code>
182      * if zero
183      */

184     public static boolean toBoolean(int value) {
185         return value == 0 ? false : true;
186     }
187     
188     /**
189      * <p>Converts an int to a Boolean using the convention that <code>zero</code>
190      * is <code>false</code>.</p>
191      *
192      * <pre>
193      * BooleanUtils.toBoolean(0) = Boolean.FALSE
194      * BooleanUtils.toBoolean(1) = Boolean.TRUE
195      * BooleanUtils.toBoolean(2) = Boolean.TRUE
196      * </pre>
197      *
198      * @param value the int to convert
199      * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero,
200      * <code>null</code> if <code>null</code>
201      */

202     public static Boolean JavaDoc toBooleanObject(int value) {
203         return value == 0 ? Boolean.FALSE : Boolean.TRUE;
204     }
205     
206     /**
207      * <p>Converts an Integer to a Boolean using the convention that <code>zero</code>
208      * is <code>false</code>.</p>
209      *
210      * <p><code>null</code> will be converted to <code>null</code>.</p>
211      *
212      * <pre>
213      * BooleanUtils.toBoolean(new Integer(0)) = Boolean.FALSE
214      * BooleanUtils.toBoolean(new Integer(1)) = Boolean.TRUE
215      * BooleanUtils.toBoolean(new Integer(null)) = null
216      * </pre>
217      *
218      * @param value the Integer to convert
219      * @return Boolean.TRUE if non-zero, Boolean.FALSE if zero,
220      * <code>null</code> if <code>null</code> input
221      */

222     public static Boolean JavaDoc toBooleanObject(Integer JavaDoc value) {
223         if (value == null) {
224             return null;
225         }
226         return value.intValue() == 0 ? Boolean.FALSE : Boolean.TRUE;
227     }
228     
229     /**
230      * <p>Converts an int to a boolean specifying the conversion values.</p>
231      *
232      * <pre>
233      * BooleanUtils.toBoolean(0, 1, 0) = false
234      * BooleanUtils.toBoolean(1, 1, 0) = true
235      * BooleanUtils.toBoolean(2, 1, 2) = false
236      * BooleanUtils.toBoolean(2, 2, 0) = true
237      * </pre>
238      *
239      * @param value the Integer to convert
240      * @param trueValue the value to match for <code>true</code>
241      * @param falseValue the value to match for <code>false</code>
242      * @return <code>true</code> or <code>false</code>
243      * @throws IllegalArgumentException if no match
244      */

245     public static boolean toBoolean(int value, int trueValue, int falseValue) {
246         if (value == trueValue) {
247             return true;
248         } else if (value == falseValue) {
249             return false;
250         }
251         // no match
252
throw new IllegalArgumentException JavaDoc("The Integer did not match either specified value");
253     }
254     
255     /**
256      * <p>Converts an Integer to a boolean specifying the conversion values.</p>
257      *
258      * <pre>
259      * BooleanUtils.toBoolean(new Integer(0), new Integer(1), new Integer(0)) = false
260      * BooleanUtils.toBoolean(new Integer(1), new Integer(1), new Integer(0)) = true
261      * BooleanUtils.toBoolean(new Integer(2), new Integer(1), new Integer(2)) = false
262      * BooleanUtils.toBoolean(new Integer(2), new Integer(2), new Integer(0)) = true
263      * BooleanUtils.toBoolean(null, null, new Integer(0)) = true
264      * </pre>
265      *
266      * @param value the Integer to convert
267      * @param trueValue the value to match for <code>true</code>,
268      * may be <code>null</code>
269      * @param falseValue the value to match for <code>false</code>,
270      * may be <code>null</code>
271      * @return <code>true</code> or <code>false</code>
272      * @throws IllegalArgumentException if no match
273      */

274     public static boolean toBoolean(Integer JavaDoc value, Integer JavaDoc trueValue, Integer JavaDoc falseValue) {
275         if (value == null) {
276             if (trueValue == null) {
277                 return true;
278             } else if (falseValue == null) {
279                 return false;
280             }
281         } else if (value.equals(trueValue)) {
282             return true;
283         } else if (value.equals(falseValue)) {
284             return false;
285         }
286         // no match
287
throw new IllegalArgumentException JavaDoc("The Integer did not match either specified value");
288     }
289     
290     /**
291      * <p>Converts an int to a Boolean specifying the conversion values.</p>
292      *
293      * <pre>
294      * BooleanUtils.toBooleanObject(0, 0, 2, 3) = Boolean.TRUE
295      * BooleanUtils.toBooleanObject(2, 1, 2, 3) = Boolean.FALSE
296      * BooleanUtils.toBooleanObject(3, 1, 2, 3) = null
297      * </pre>
298      *
299      * @param value the Integer to convert
300      * @param trueValue the value to match for <code>true</code>
301      * @param falseValue the value to match for <code>false</code>
302      * @param nullValue the value to to match for <code>null</code>
303      * @return Boolean.TRUE, Boolean.FALSE, or <code>null</code>
304      * @throws IllegalArgumentException if no match
305      */

306     public static Boolean JavaDoc toBooleanObject(int value, int trueValue, int falseValue, int nullValue) {
307         if (value == trueValue) {
308             return Boolean.TRUE;
309         } else if (value == falseValue) {
310             return Boolean.FALSE;
311         } else if (value == nullValue) {
312             return null;
313         }
314         // no match
315
throw new IllegalArgumentException JavaDoc("The Integer did not match any specified value");
316     }
317     
318     /**
319      * <p>Converts an Integer to a Boolean specifying the conversion values.</p>
320      *
321      * <pre>
322      * BooleanUtils.toBooleanObject(new Integer(0), new Integer(0), new Integer(2), new Integer(3)) = Boolean.TRUE
323      * BooleanUtils.toBooleanObject(new Integer(2), new Integer(1), new Integer(2), new Integer(3)) = Boolean.FALSE
324      * BooleanUtils.toBooleanObject(new Integer(3), new Integer(1), new Integer(2), new Integer(3)) = null
325      * </pre>
326      *
327      * @param value the Integer to convert
328      * @param trueValue the value to match for <code>true</code>,
329      * may be <code>null</code>
330      * @param falseValue the value to match for <code>false</code>,
331      * may be <code>null</code>
332      * @param nullValue the value to to match for <code>null</code>,
333      * may be <code>null</code>
334      * @return Boolean.TRUE, Boolean.FALSE, or <code>null</code>
335      * @throws IllegalArgumentException if no match
336      */

337     public static Boolean JavaDoc toBooleanObject(Integer JavaDoc value, Integer JavaDoc trueValue, Integer JavaDoc falseValue, Integer JavaDoc nullValue) {
338         if (value == null) {
339             if (trueValue == null) {
340                 return Boolean.TRUE;
341             } else if (falseValue == null) {
342                 return Boolean.FALSE;
343             } else if (nullValue == null) {
344                 return null;
345             }
346         } else if (value.equals(trueValue)) {
347             return Boolean.TRUE;
348         } else if (value.equals(falseValue)) {
349             return Boolean.FALSE;
350         } else if (value.equals(nullValue)) {
351             return null;
352         }
353         // no match
354
throw new IllegalArgumentException JavaDoc("The Integer did not match any specified value");
355     }
356     
357     // Boolean to Integer methods
358
//-----------------------------------------------------------------------
359
/**
360      * <p>Converts a boolean to an int using the convention that
361      * <code>zero</code> is <code>false</code>.</p>
362      *
363      * <pre>
364      * BooleanUtils.toInteger(true) = 1
365      * BooleanUtils.toInteger(false) = 0
366      * </pre>
367      *
368      * @param bool the boolean to convert
369      * @return one if <code>true</code>, zero if <code>false</code>
370      */

371     public static int toInteger(boolean bool) {
372         return bool ? 1 : 0;
373     }
374     
375     /**
376      * <p>Converts a boolean to an Integer using the convention that
377      * <code>zero</code> is <code>false</code>.</p>
378      *
379      * <pre>
380      * BooleanUtils.toIntegerObject(true) = new Integer(1)
381      * BooleanUtils.toIntegerObject(false) = new Integer(0)
382      * </pre>
383      *
384      * @param bool the boolean to convert
385      * @return one if <code>true</code>, zero if <code>false</code>
386      */

387     public static Integer JavaDoc toIntegerObject(boolean bool) {
388         return bool ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO;
389     }
390     
391     /**
392      * <p>Converts a Boolean to a Integer using the convention that
393      * <code>zero</code> is <code>false</code>.</p>
394      *
395      * <p><code>null</code> will be converted to <code>null</code>.</p>
396      *
397      * <pre>
398      * BooleanUtils.toIntegerObject(Boolean.TRUE) = new Integer(1)
399      * BooleanUtils.toIntegerObject(Boolean.FALSE) = new Integer(0)
400      * </pre>
401      *
402      * @param bool the Boolean to convert
403      * @return one if Boolean.TRUE, zero if Boolean.FALSE, <code>null</code> if <code>null</code>
404      */

405     public static Integer JavaDoc toIntegerObject(Boolean JavaDoc bool) {
406         if (bool == null) {
407             return null;
408         }
409         return bool.booleanValue() ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO;
410     }
411     
412     /**
413      * <p>Converts a boolean to an int specifying the conversion values.</p>
414      *
415      * <pre>
416      * BooleanUtils.toInteger(true, 1, 0) = 1
417      * BooleanUtils.toInteger(false, 1, 0) = 0
418      * </pre>
419      *
420      * @param bool the to convert
421      * @param trueValue the value to return if <code>true</code>
422      * @param falseValue the value to return if <code>false</code>
423      * @return the appropriate value
424      */

425     public static int toInteger(boolean bool, int trueValue, int falseValue) {
426         return bool ? trueValue : falseValue;
427     }
428     
429     /**
430      * <p>Converts a Boolean to an int specifying the conversion values.</p>
431      *
432      * <pre>
433      * BooleanUtils.toInteger(Boolean.TRUE, 1, 0, 2) = 1
434      * BooleanUtils.toInteger(Boolean.FALSE, 1, 0, 2) = 0
435      * BooleanUtils.toInteger(null, 1, 0, 2) = 2
436      * </pre>
437      *
438      * @param bool the Boolean to convert
439      * @param trueValue the value to return if <code>true</code>
440      * @param falseValue the value to return if <code>false</code>
441      * @param nullValue the value to return if <code>null</code>
442      * @return the appropriate value
443      */

444     public static int toInteger(Boolean JavaDoc bool, int trueValue, int falseValue, int nullValue) {
445         if (bool == null) {
446             return nullValue;
447         }
448         return bool.booleanValue() ? trueValue : falseValue;
449     }
450     
451     /**
452      * <p>Converts a boolean to an Integer specifying the conversion values.</p>
453      *
454      * <pre>
455      * BooleanUtils.toIntegerObject(true, new Integer(1), new Integer(0)) = new Integer(1)
456      * BooleanUtils.toIntegerObject(false, new Integer(1), new Integer(0)) = new Integer(0)
457      * </pre>
458      *
459      * @param bool the to convert
460      * @param trueValue the value to return if <code>true</code>,
461      * may be <code>null</code>
462      * @param falseValue the value to return if <code>false</code>,
463      * may be <code>null</code>
464      * @return the appropriate value
465      */

466     public static Integer JavaDoc toIntegerObject(boolean bool, Integer JavaDoc trueValue, Integer JavaDoc falseValue) {
467         return bool ? trueValue : falseValue;
468     }
469     
470     /**
471      * <p>Converts a Boolean to an Integer specifying the conversion values.</p>
472      *
473      * <pre>
474      * BooleanUtils.toIntegerObject(Boolean.TRUE, new Integer(1), new Integer(0), new Integer(2)) = new Integer(1)
475      * BooleanUtils.toIntegerObject(Boolean.FALSE, new Integer(1), new Integer(0), new Integer(2)) = new Integer(0)
476      * BooleanUtils.toIntegerObject(null, new Integer(1), new Integer(0), new Integer(2)) = new Integer(2)
477      * </pre>
478      *
479      * @param bool the Boolean to convert
480      * @param trueValue the value to return if <code>true</code>,
481      * may be <code>null</code>
482      * @param falseValue the value to return if <code>false</code>,
483      * may be <code>null</code>
484      * @param nullValue the value to return if <code>null</code>,
485      * may be <code>null</code>
486      * @return the appropriate value
487      */

488     public static Integer JavaDoc toIntegerObject(Boolean JavaDoc bool, Integer JavaDoc trueValue, Integer JavaDoc falseValue, Integer JavaDoc nullValue) {
489         if (bool == null) {
490             return nullValue;
491         }
492         return bool.booleanValue() ? trueValue : falseValue;
493     }
494     
495     // String to Boolean methods
496
//-----------------------------------------------------------------------
497
/**
498      * <p>Converts a String to a Boolean.</p>
499      *
500      * <p><code>'true'</code>, <code>'on'</code> or <code>'yes'</code>
501      * (case insensitive) will return <code>true</code>.
502      * <code>'false'</code>, <code>'off'</code> or <code>'no'</code>
503      * (case insensitive) will return <code>false</code>.
504      * Otherwise, <code>null</code> is returned.</p>
505      *
506      * <pre>
507      * BooleanUtils.toBooleanObject(null) = null
508      * BooleanUtils.toBooleanObject("true") = Boolean.TRUE
509      * BooleanUtils.toBooleanObject("false") = Boolean.FALSE
510      * BooleanUtils.toBooleanObject("on") = Boolean.TRUE
511      * BooleanUtils.toBooleanObject("ON") = Boolean.TRUE
512      * BooleanUtils.toBooleanObject("off") = Boolean.FALSE
513      * BooleanUtils.toBooleanObject("oFf") = Boolean.FALSE
514      * BooleanUtils.toBooleanObject("blue") = null
515      * </pre>
516      *
517      * @param str the String to check
518      * @return the Boolean value of the string,
519      * <code>null</code> if no match or <code>null</code> input
520      */

521     public static Boolean JavaDoc toBooleanObject(String JavaDoc str) {
522         if ("true".equalsIgnoreCase(str)) {
523             return Boolean.TRUE;
524         } else if ("false".equalsIgnoreCase(str)) {
525             return Boolean.FALSE;
526         } else if ("on".equalsIgnoreCase(str)) {
527             return Boolean.TRUE;
528         } else if ("off".equalsIgnoreCase(str)) {
529             return Boolean.FALSE;
530         } else if ("yes".equalsIgnoreCase(str)) {
531             return Boolean.TRUE;
532         } else if ("no".equalsIgnoreCase(str)) {
533             return Boolean.FALSE;
534         }
535         // no match
536
return null;
537     }
538
539     /**
540      * <p>Converts a String to a Boolean throwing an exception if no match.</p>
541      *
542      * <pre>
543      * BooleanUtils.toBooleanObject("true", "true", "false", "null") = Boolean.TRUE
544      * BooleanUtils.toBooleanObject("false", "true", "false", "null") = Boolean.FALSE
545      * BooleanUtils.toBooleanObject("null", "true", "false", "null") = null
546      * </pre>
547      *
548      * @param str the String to check
549      * @param trueString the String to match for <code>true</code>
550      * (case sensitive), may be <code>null</code>
551      * @param falseString the String to match for <code>false</code>
552      * (case sensitive), may be <code>null</code>
553      * @param nullString the String to match for <code>null</code>
554      * (case sensitive), may be <code>null</code>
555      * @return the Boolean value of the string,
556      * <code>null</code> if no match or <code>null</code> input
557      */

558     public static Boolean JavaDoc toBooleanObject(String JavaDoc str, String JavaDoc trueString, String JavaDoc falseString, String JavaDoc nullString) {
559         if (str == null) {
560             if (trueString == null) {
561                 return Boolean.TRUE;
562             } else if (falseString == null) {
563                 return Boolean.FALSE;
564             } else if (nullString == null) {
565                 return null;
566             }
567         } else if (str.equals(trueString)) {
568             return Boolean.TRUE;
569         } else if (str.equals(falseString)) {
570             return Boolean.FALSE;
571         } else if (str.equals(nullString)) {
572             return null;
573         }
574         // no match
575
throw new IllegalArgumentException JavaDoc("The String did not match any specified value");
576     }
577
578     // String to boolean methods
579
//-----------------------------------------------------------------------
580
/**
581      * <p>Converts a String to a boolean (optimised for performance).</p>
582      *
583      * <p><code>'true'</code>, <code>'on'</code> or <code>'yes'</code>
584      * (case insensitive) will return <code>true</code>. Otherwise,
585      * <code>false</code> is returned.</p>
586      *
587      * <p>This method performs 4 times faster (JDK1.4) than
588      * <code>Boolean.valueOf(String)</code>. However, this method accepts
589      * 'on' and 'yes' as true values.
590      *
591      * <pre>
592      * BooleanUtils.toBoolean(null) = false
593      * BooleanUtils.toBoolean("true") = true
594      * BooleanUtils.toBoolean("TRUE") = true
595      * BooleanUtils.toBoolean("tRUe") = true
596      * BooleanUtils.toBoolean("on") = true
597      * BooleanUtils.toBoolean("yes") = true
598      * BooleanUtils.toBoolean("false") = false
599      * BooleanUtils.toBoolean("x gti") = false
600      * </pre>
601      *
602      * @param str the String to check
603      * @return the boolean value of the string, <code>false</code> if no match
604      */

605     public static boolean toBoolean(String JavaDoc str) {
606         // Previously used equalsIgnoreCase, which was fast for interned 'true'.
607
// Non interned 'true' matched 15 times slower.
608
//
609
// Optimisation provides same performance as before for interned 'true'.
610
// Similar performance for null, 'false', and other strings not length 2/3/4.
611
// 'true'/'TRUE' match 4 times slower, 'tRUE'/'True' 7 times slower.
612
if (str == "true") {
613             return true;
614         }
615         if (str == null) {
616             return false;
617         }
618         switch (str.length()) {
619             case 2: {
620                 char ch0 = str.charAt(0);
621                 char ch1 = str.charAt(1);
622                 return
623                     (ch0 == 'o' || ch0 == 'O') &&
624                     (ch1 == 'n' || ch1 == 'N');
625             }
626             case 3: {
627                 char ch = str.charAt(0);
628                 if (ch == 'y') {
629                     return
630                         (str.charAt(1) == 'e' || str.charAt(1) == 'E') &&
631                         (str.charAt(2) == 's' || str.charAt(2) == 'S');
632                 }
633                 if (ch == 'Y') {
634                     return
635                         (str.charAt(1) == 'E' || str.charAt(1) == 'e') &&
636                         (str.charAt(2) == 'S' || str.charAt(2) == 's');
637                 }
638             }
639             case 4: {
640                 char ch = str.charAt(0);
641                 if (ch == 't') {
642                     return
643                         (str.charAt(1) == 'r' || str.charAt(1) == 'R') &&
644                         (str.charAt(2) == 'u' || str.charAt(2) == 'U') &&
645                         (str.charAt(3) == 'e' || str.charAt(3) == 'E');
646                 }
647                 if (ch == 'T') {
648                     return
649                         (str.charAt(1) == 'R' || str.charAt(1) == 'r') &&
650                         (str.charAt(2) == 'U' || str.charAt(2) == 'u') &&
651                         (str.charAt(3) == 'E' || str.charAt(3) == 'e');
652                 }
653             }
654         }
655         return false;
656     }
657     
658 // public static void main(String[] args) {
659
// long start = System.currentTimeMillis();
660
// boolean flag = true;
661
// int count = 0;
662
// for (int i = 0; i < 100000000; i++) {
663
// flag = toBoolean("YES");
664
// }
665
// long end = System.currentTimeMillis();
666
// System.out.println((end - start) + " " + flag + " " + count);
667
// }
668

669     /**
670      * <p>Converts a String to a Boolean throwing an exception if no match found.</p>
671      *
672      * <p>null is returned if there is no match.</p>
673      *
674      * <pre>
675      * BooleanUtils.toBoolean("true", "true", "false") = true
676      * BooleanUtils.toBoolean("false", "true", "false") = false
677      * </pre>
678      *
679      * @param str the String to check
680      * @param trueString the String to match for <code>true</code>
681      * (case sensitive), may be <code>null</code>
682      * @param falseString the String to match for <code>false</code>
683      * (case sensitive), may be <code>null</code>
684      * @return the boolean value of the string
685      * @throws IllegalArgumentException if the String doesn't match
686      */

687     public static boolean toBoolean(String JavaDoc str, String JavaDoc trueString, String JavaDoc falseString) {
688         if (str == null) {
689             if (trueString == null) {
690                 return true;
691             } else if (falseString == null) {
692                 return false;
693             }
694         } else if (str.equals(trueString)) {
695             return true;
696         } else if (str.equals(falseString)) {
697             return false;
698         }
699         // no match
700
throw new IllegalArgumentException JavaDoc("The String did not match either specified value");
701     }
702
703     // Boolean to String methods
704
//-----------------------------------------------------------------------
705
/**
706      * <p>Converts a Boolean to a String returning <code>'true'</code>,
707      * <code>'false'</code>, or <code>null</code>.</p>
708      *
709      * <pre>
710      * BooleanUtils.toStringTrueFalse(Boolean.TRUE) = "true"
711      * BooleanUtils.toStringTrueFalse(Boolean.FALSE) = "false"
712      * BooleanUtils.toStringTrueFalse(null) = null;
713      * </pre>
714      *
715      * @param bool the Boolean to check
716      * @return <code>'true'</code>, <code>'false'</code>,
717      * or <code>null</code>
718      */

719     public static String JavaDoc toStringTrueFalse(Boolean JavaDoc bool) {
720         return toString(bool, "true", "false", null);
721     }
722     
723     /**
724      * <p>Converts a Boolean to a String returning <code>'on'</code>,
725      * <code>'off'</code>, or <code>null</code>.</p>
726      *
727      * <pre>
728      * BooleanUtils.toStringOnOff(Boolean.TRUE) = "on"
729      * BooleanUtils.toStringOnOff(Boolean.FALSE) = "off"
730      * BooleanUtils.toStringOnOff(null) = null;
731      * </pre>
732      *
733      * @param bool the Boolean to check
734      * @return <code>'on'</code>, <code>'off'</code>,
735      * or <code>null</code>
736      */

737     public static String JavaDoc toStringOnOff(Boolean JavaDoc bool) {
738         return toString(bool, "on", "off", null);
739     }
740     
741     /**
742      * <p>Converts a Boolean to a String returning <code>'yes'</code>,
743      * <code>'no'</code>, or <code>null</code>.</p>
744      *
745      * <pre>
746      * BooleanUtils.toStringYesNo(Boolean.TRUE) = "yes"
747      * BooleanUtils.toStringYesNo(Boolean.FALSE) = "no"
748      * BooleanUtils.toStringYesNo(null) = null;
749      * </pre>
750      *
751      * @param bool the Boolean to check
752      * @return <code>'yes'</code>, <code>'no'</code>,
753      * or <code>null</code>
754      */

755     public static String JavaDoc toStringYesNo(Boolean JavaDoc bool) {
756         return toString(bool, "yes", "no", null);
757     }
758     
759     /**
760      * <p>Converts a Boolean to a String returning one of the input Strings.</p>
761      *
762      * <pre>
763      * BooleanUtils.toString(Boolean.TRUE, "true", "false", null) = "true"
764      * BooleanUtils.toString(Boolean.FALSE, "true", "false", null) = "false"
765      * BooleanUtils.toString(null, "true", "false", null) = null;
766      * </pre>
767      *
768      * @param bool the Boolean to check
769      * @param trueString the String to return if <code>true</code>,
770      * may be <code>null</code>
771      * @param falseString the String to return if <code>false</code>,
772      * may be <code>null</code>
773      * @param nullString the String to return if <code>null</code>,
774      * may be <code>null</code>
775      * @return one of the three input Strings
776      */

777     public static String JavaDoc toString(Boolean JavaDoc bool, String JavaDoc trueString, String JavaDoc falseString, String JavaDoc nullString) {
778         if (bool == null) {
779             return nullString;
780         }
781         return bool.booleanValue() ? trueString : falseString;
782     }
783     
784     // boolean to String methods
785
//-----------------------------------------------------------------------
786
/**
787      * <p>Converts a boolean to a String returning <code>'true'</code>
788      * or <code>'false'</code>.</p>
789      *
790      * <pre>
791      * BooleanUtils.toStringTrueFalse(true) = "true"
792      * BooleanUtils.toStringTrueFalse(false) = "false"
793      * </pre>
794      *
795      * @param bool the Boolean to check
796      * @return <code>'true'</code>, <code>'false'</code>,
797      * or <code>null</code>
798      */

799     public static String JavaDoc toStringTrueFalse(boolean bool) {
800         return toString(bool, "true", "false");
801     }
802     
803     /**
804      * <p>Converts a boolean to a String returning <code>'on'</code>
805      * or <code>'off'</code>.</p>
806      *
807      * <pre>
808      * BooleanUtils.toStringOnOff(true) = "on"
809      * BooleanUtils.toStringOnOff(false) = "off"
810      * </pre>
811      *
812      * @param bool the Boolean to check
813      * @return <code>'on'</code>, <code>'off'</code>,
814      * or <code>null</code>
815      */

816     public static String JavaDoc toStringOnOff(boolean bool) {
817         return toString(bool, "on", "off");
818     }
819     
820     /**
821      * <p>Converts a boolean to a String returning <code>'yes'</code>
822      * or <code>'no'</code>.</p>
823      *
824      * <pre>
825      * BooleanUtils.toStringYesNo(true) = "yes"
826      * BooleanUtils.toStringYesNo(false) = "no"
827      * </pre>
828      *
829      * @param bool the Boolean to check
830      * @return <code>'yes'</code>, <code>'no'</code>,
831      * or <code>null</code>
832      */

833     public static String JavaDoc toStringYesNo(boolean bool) {
834         return toString(bool, "yes", "no");
835     }
836     
837     /**
838      * <p>Converts a boolean to a String returning one of the input Strings.</p>
839      *
840      * <pre>
841      * BooleanUtils.toString(true, "true", "false") = "true"
842      * BooleanUtils.toString(false, "true", "false") = "false"
843      * </pre>
844      *
845      * @param bool the Boolean to check
846      * @param trueString the String to return if <code>true</code>,
847      * may be <code>null</code>
848      * @param falseString the String to return if <code>false</code>,
849      * may be <code>null</code>
850      * @return one of the two input Strings
851      */

852     public static String JavaDoc toString(boolean bool, String JavaDoc trueString, String JavaDoc falseString) {
853         return bool ? trueString : falseString;
854     }
855     
856     // xor methods
857
// ----------------------------------------------------------------------
858
/**
859      * <p>Performs an xor on a set of booleans.</p>
860      *
861      * <pre>
862      * BooleanUtils.xor(new boolean[] { true, true }) = false
863      * BooleanUtils.xor(new boolean[] { false, false }) = false
864      * BooleanUtils.xor(new boolean[] { true, false }) = true
865      * </pre>
866      *
867      * @param array an array of <code>boolean<code>s
868      * @return <code>true</code> if the xor is successful.
869      * @throws IllegalArgumentException if <code>array</code> is <code>null</code>
870      * @throws IllegalArgumentException if <code>array</code> is empty.
871      */

872     public static boolean xor(boolean[] array) {
873         // Validates input
874
if (array == null) {
875             throw new IllegalArgumentException JavaDoc("The Array must not be null");
876         } else if (array.length == 0) {
877             throw new IllegalArgumentException JavaDoc("Array is empty");
878         }
879
880         // Loops through array, comparing each item
881
int trueCount = 0;
882         for (int i = 0; i < array.length; i++) {
883             // If item is true, and trueCount is < 1, increments count
884
// Else, xor fails
885
if (array[i]) {
886                 if (trueCount < 1) {
887                     trueCount++;
888                 } else {
889                     return false;
890                 }
891             }
892         }
893
894         // Returns true if there was exactly 1 true item
895
return trueCount == 1;
896     }
897
898     /**
899      * <p>Performs an xor on an array of Booleans.</p>
900      *
901      * <pre>
902      * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.TRUE }) = Boolean.FALSE
903      * BooleanUtils.xor(new Boolean[] { Boolean.FALSE, Boolean.FALSE }) = Boolean.FALSE
904      * BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE }) = Boolean.TRUE
905      * </pre>
906      *
907      * @param array an array of <code>Boolean<code>s
908      * @return <code>true</code> if the xor is successful.
909      * @throws IllegalArgumentException if <code>array</code> is <code>null</code>
910      * @throws IllegalArgumentException if <code>array</code> is empty.
911      * @throws IllegalArgumentException if <code>array</code> contains a <code>null</code>
912      */

913     public static Boolean JavaDoc xor(Boolean JavaDoc[] array) {
914         if (array == null) {
915             throw new IllegalArgumentException JavaDoc("The Array must not be null");
916         } else if (array.length == 0) {
917             throw new IllegalArgumentException JavaDoc("Array is empty");
918         }
919         boolean[] primitive = null;
920         try {
921             primitive = ArrayUtils.toPrimitive(array);
922         } catch (NullPointerException JavaDoc ex) {
923             throw new IllegalArgumentException JavaDoc("The array must not contain any null elements");
924         }
925         return xor(primitive) ? Boolean.TRUE : Boolean.FALSE;
926     }
927
928 }
929
Popular Tags