Java String Value Of valueOfBoolean(final String s)

Here you can find the source of valueOfBoolean(final String s)

Description

value Of Boolean

License

Open Source License

Parameter

Parameter Description
s a parameter

Declaration

public static final Boolean valueOfBoolean(final String s) 

Method Source Code

//package com.java2s;

public class Main {
    public static final String STR_EMP = "";

    /***//  w w w  . j av  a 2  s. c  om
     * 
     * @param s
     * @return
     */
    public static final Boolean valueOfBoolean(final String s) {
        return Boolean.valueOf(parseBoolean(s));
    }

    /**
     * 
     * @param o
     * @return
     */
    public static final String valueOf(final Object o) {
        return (o instanceof String) ? (String) o : (o == null ? "null" : o.toString());
    }

    /**
     * 
     * @param s
     * @return
     */
    public static final boolean parseBoolean(final String s) {
        if ("true".equalsIgnoreCase(s)) {
            return true;
        }
        if (s == null) {
            return false;
        }
        switch (s.length()) {
        case 2: {
            char ch0 = s.charAt(0);
            char ch1 = s.charAt(1);
            return (ch0 == 'o' || ch0 == 'O') && (ch1 == 'n' || ch1 == 'N');
        }
        case 3: {
            char ch = s.charAt(0);
            return (ch == 'y')
                    ? (s.charAt(1) == 'e' || s.charAt(1) == 'E') && (s.charAt(2) == 's' || s.charAt(2) == 'S')
                    : (s.charAt(1) == 'E' || s.charAt(1) == 'e') && (s.charAt(2) == 'S' || s.charAt(2) == 's');
        }
        case 4: {
            char ch = s.charAt(0);
            return ch == 't'
                    ? (s.charAt(1) == 'r' || s.charAt(1) == 'R') && (s.charAt(2) == 'u' || s.charAt(2) == 'U')
                            && (s.charAt(3) == 'e' || s.charAt(3) == 'E')
                    : (s.charAt(1) == 'R' || s.charAt(1) == 'r') && (s.charAt(2) == 'U' || s.charAt(2) == 'u')
                            && (s.charAt(3) == 'E' || s.charAt(3) == 'e');
        }
        }
        return false;
    }

    /**
     * 
     * @param objs
     * @return
     */
    public static String toString(final Object[] objs) {
        if (objs == null) {
            return STR_EMP;
        }
        final int len = objs.length;
        if (len == 0) {
            return STR_EMP;
        }
        final StringBuilder buf = new StringBuilder(len * 12);
        for (int i = 0; i < len - 1; i++) {
            buf.append(objs[i]).append(", ");
        }
        return buf.append(objs[len - 1]).toString();
    }
}

Related

  1. valueOf(String str)
  2. valueOf(String stringVal, boolean dVal)
  3. valueOf(String value)
  4. valueOf(String value)
  5. valueOf(String value, boolean defaultValue)
  6. valueOfBoolean(String string)
  7. valueOfBooleanStr(String str)
  8. valueOfEnum(Class enumClass, String value, E defaultValue)
  9. valueOfEnum(final Class type, final String value)