Java Boolean From toBoolean(Boolean bool)

Here you can find the source of toBoolean(Boolean bool)

Description

to Boolean

License

Mozilla Public License

Declaration

public static boolean toBoolean(Boolean bool) 

Method Source Code

//package com.java2s;
//License from project: Mozilla Public License 

public class Main {
    public static boolean toBoolean(Boolean bool) {
        if (bool == null) {
            return false;
        }//from www.j a  va2  s .c om

        return bool;
    }

    public static boolean toBoolean(int value) {
        return value != 0;
    }

    public static boolean toBoolean(int value, int trueValue, int falseValue) {
        if (value == trueValue) {
            return true;
        } else if (value == falseValue) {
            return false;
        }

        throw new IllegalArgumentException("The Integer did not match either specified value");
    }

    public static boolean toBoolean(Integer value, Integer trueValue, Integer falseValue) {
        if (value == null) {
            if (trueValue == null) {
                return true;
            } else if (falseValue == null) {
                return false;
            }
        } else if (value.equals(trueValue)) {
            return true;
        } else if (value.equals(falseValue)) {
            return false;
        }

        throw new IllegalArgumentException("The Integer did not match either specified value");
    }

    public static boolean toBoolean(String str) {
        if ("true".equals(str)) {
            return true;
        }

        if (str == null) {
            return false;
        }

        switch (str.length()) {
        case 2: {
            char ch0 = str.charAt(0);

            char ch1 = str.charAt(1);

            return (ch0 == 'o' || ch0 == 'O') && (ch1 == 'n' || ch1 == 'N');
        }
        case 3: {
            char ch = str.charAt(0);

            if (ch == 'y') {
                return (str.charAt(1) == 'e' || str.charAt(1) == 'E')
                        && (str.charAt(2) == 's' || str.charAt(2) == 'S');
            }

            if (ch == 'Y') {
                return (str.charAt(1) == 'E' || str.charAt(1) == 'e')
                        && (str.charAt(2) == 'S' || str.charAt(2) == 's');
            }

            return false;
        }
        case 4: {
            char ch = str.charAt(0);

            if (ch == 't') {
                return (str.charAt(1) == 'r' || str.charAt(1) == 'R')
                        && (str.charAt(2) == 'u' || str.charAt(2) == 'U')
                        && (str.charAt(3) == 'e' || str.charAt(3) == 'E');
            }

            if (ch == 'T') {
                return (str.charAt(1) == 'R' || str.charAt(1) == 'r')
                        && (str.charAt(2) == 'U' || str.charAt(2) == 'u')
                        && (str.charAt(3) == 'E' || str.charAt(3) == 'e');
            }
        }
        }

        return false;
    }

    public static boolean toBoolean(String str, String trueString, String falseString) {
        if (str == null) {
            if (trueString == null) {
                return true;
            } else if (falseString == null) {
                return false;
            }
        } else if (str.equals(trueString)) {
            return true;
        } else if (str.equals(falseString)) {
            return false;
        }

        throw new IllegalArgumentException("The String did not match either specified value");
    }
}

Related

  1. toBool(String str)
  2. toBool(String v, boolean def)
  3. toBool(String val)
  4. toBool(String val, boolean def)
  5. toBoolean(boolean b)
  6. toBoolean(boolean bool)
  7. toBoolean(Boolean bool)
  8. toBoolean(Boolean bool, boolean defaultValue)
  9. toBoolean(byte b)