1 16 package org.apache.commons.lang; 17 18 import org.apache.commons.lang.math.NumberUtils; 19 20 33 public class BooleanUtils { 34 35 42 public BooleanUtils() { 43 } 44 45 61 public static Boolean negate(Boolean bool) { 62 if (bool == null) { 63 return null; 64 } 65 return (bool.booleanValue() ? Boolean.FALSE : Boolean.TRUE); 66 } 67 68 83 public static boolean isTrue(Boolean bool) { 84 if (bool == null) { 85 return false; 86 } 87 return bool.booleanValue() ? true : false; 88 } 89 90 103 public static boolean isFalse(Boolean bool) { 104 if (bool == null) { 105 return false; 106 } 107 return bool.booleanValue() ? false : true; 108 } 109 110 123 public static Boolean toBooleanObject(boolean bool) { 124 return bool ? Boolean.TRUE : Boolean.FALSE; 125 } 126 127 141 public static boolean toBoolean(Boolean bool) { 142 if (bool == null) { 143 return false; 144 } 145 return bool.booleanValue() ? true : false; 146 } 147 148 161 public static boolean toBooleanDefaultIfNull(Boolean bool, boolean valueIfNull) { 162 if (bool == null) { 163 return valueIfNull; 164 } 165 return bool.booleanValue() ? true : false; 166 } 167 168 184 public static boolean toBoolean(int value) { 185 return value == 0 ? false : true; 186 } 187 188 202 public static Boolean toBooleanObject(int value) { 203 return value == 0 ? Boolean.FALSE : Boolean.TRUE; 204 } 205 206 222 public static Boolean toBooleanObject(Integer value) { 223 if (value == null) { 224 return null; 225 } 226 return value.intValue() == 0 ? Boolean.FALSE : Boolean.TRUE; 227 } 228 229 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 throw new IllegalArgumentException ("The Integer did not match either specified value"); 253 } 254 255 274 public static boolean toBoolean(Integer value, Integer trueValue, Integer 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 throw new IllegalArgumentException ("The Integer did not match either specified value"); 288 } 289 290 306 public static Boolean 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 throw new IllegalArgumentException ("The Integer did not match any specified value"); 316 } 317 318 337 public static Boolean toBooleanObject(Integer value, Integer trueValue, Integer falseValue, Integer 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 throw new IllegalArgumentException ("The Integer did not match any specified value"); 355 } 356 357 371 public static int toInteger(boolean bool) { 372 return bool ? 1 : 0; 373 } 374 375 387 public static Integer toIntegerObject(boolean bool) { 388 return bool ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; 389 } 390 391 405 public static Integer toIntegerObject(Boolean bool) { 406 if (bool == null) { 407 return null; 408 } 409 return bool.booleanValue() ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO; 410 } 411 412 425 public static int toInteger(boolean bool, int trueValue, int falseValue) { 426 return bool ? trueValue : falseValue; 427 } 428 429 444 public static int toInteger(Boolean bool, int trueValue, int falseValue, int nullValue) { 445 if (bool == null) { 446 return nullValue; 447 } 448 return bool.booleanValue() ? trueValue : falseValue; 449 } 450 451 466 public static Integer toIntegerObject(boolean bool, Integer trueValue, Integer falseValue) { 467 return bool ? trueValue : falseValue; 468 } 469 470 488 public static Integer toIntegerObject(Boolean bool, Integer trueValue, Integer falseValue, Integer nullValue) { 489 if (bool == null) { 490 return nullValue; 491 } 492 return bool.booleanValue() ? trueValue : falseValue; 493 } 494 495 521 public static Boolean toBooleanObject(String 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 return null; 537 } 538 539 558 public static Boolean toBooleanObject(String str, String trueString, String falseString, String 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 throw new IllegalArgumentException ("The String did not match any specified value"); 576 } 577 578 605 public static boolean toBoolean(String str) { 606 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 669 687 public static boolean toBoolean(String str, String trueString, String 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 throw new IllegalArgumentException ("The String did not match either specified value"); 701 } 702 703 719 public static String toStringTrueFalse(Boolean bool) { 720 return toString(bool, "true", "false", null); 721 } 722 723 737 public static String toStringOnOff(Boolean bool) { 738 return toString(bool, "on", "off", null); 739 } 740 741 755 public static String toStringYesNo(Boolean bool) { 756 return toString(bool, "yes", "no", null); 757 } 758 759 777 public static String toString(Boolean bool, String trueString, String falseString, String nullString) { 778 if (bool == null) { 779 return nullString; 780 } 781 return bool.booleanValue() ? trueString : falseString; 782 } 783 784 799 public static String toStringTrueFalse(boolean bool) { 800 return toString(bool, "true", "false"); 801 } 802 803 816 public static String toStringOnOff(boolean bool) { 817 return toString(bool, "on", "off"); 818 } 819 820 833 public static String toStringYesNo(boolean bool) { 834 return toString(bool, "yes", "no"); 835 } 836 837 852 public static String toString(boolean bool, String trueString, String falseString) { 853 return bool ? trueString : falseString; 854 } 855 856 872 public static boolean xor(boolean[] array) { 873 if (array == null) { 875 throw new IllegalArgumentException ("The Array must not be null"); 876 } else if (array.length == 0) { 877 throw new IllegalArgumentException ("Array is empty"); 878 } 879 880 int trueCount = 0; 882 for (int i = 0; i < array.length; i++) { 883 if (array[i]) { 886 if (trueCount < 1) { 887 trueCount++; 888 } else { 889 return false; 890 } 891 } 892 } 893 894 return trueCount == 1; 896 } 897 898 913 public static Boolean xor(Boolean [] array) { 914 if (array == null) { 915 throw new IllegalArgumentException ("The Array must not be null"); 916 } else if (array.length == 0) { 917 throw new IllegalArgumentException ("Array is empty"); 918 } 919 boolean[] primitive = null; 920 try { 921 primitive = ArrayUtils.toPrimitive(array); 922 } catch (NullPointerException ex) { 923 throw new IllegalArgumentException ("The array must not contain any null elements"); 924 } 925 return xor(primitive) ? Boolean.TRUE : Boolean.FALSE; 926 } 927 928 } 929 | Popular Tags |