Java Boolean From toBooleanObject(final String str)

Here you can find the source of toBooleanObject(final String str)

Description

to Boolean Object

License

Apache License

Declaration

private static Boolean toBooleanObject(final String str) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    private static Boolean toBooleanObject(final String str) {
        // Previously used equalsIgnoreCase, which was fast for interned 'true'.
        // Non interned 'true' matched 15 times slower.
        ////from  w w w .jav a 2  s  . c  o  m
        // Optimisation provides same performance as before for interned 'true'.
        // Similar performance for null, 'false', and other strings not length 2/3/4.
        // 'true'/'TRUE' match 4 times slower, 'tRUE'/'True' 7 times slower.
        if (str == "true") {
            return Boolean.TRUE;
        }
        if (str == null) {
            return null;
        }
        switch (str.length()) {
        case 1: {
            final char ch0 = str.charAt(0);
            if (ch0 == 'y' || ch0 == 'Y' || ch0 == 't' || ch0 == 'T') {
                return Boolean.TRUE;
            }
            if (ch0 == 'n' || ch0 == 'N' || ch0 == 'f' || ch0 == 'F') {
                return Boolean.FALSE;
            }
            break;
        }
        case 2: {
            final char ch0 = str.charAt(0);
            final char ch1 = str.charAt(1);
            if ((ch0 == 'o' || ch0 == 'O') && (ch1 == 'n' || ch1 == 'N')) {
                return Boolean.TRUE;
            }
            if ((ch0 == 'n' || ch0 == 'N') && (ch1 == 'o' || ch1 == 'O')) {
                return Boolean.FALSE;
            }
            break;
        }
        case 3: {
            final char ch0 = str.charAt(0);
            final char ch1 = str.charAt(1);
            final char ch2 = str.charAt(2);
            if ((ch0 == 'y' || ch0 == 'Y') && (ch1 == 'e' || ch1 == 'E') && (ch2 == 's' || ch2 == 'S')) {
                return Boolean.TRUE;
            }
            if ((ch0 == 'o' || ch0 == 'O') && (ch1 == 'f' || ch1 == 'F') && (ch2 == 'f' || ch2 == 'F')) {
                return Boolean.FALSE;
            }
            break;
        }
        case 4: {
            final char ch0 = str.charAt(0);
            final char ch1 = str.charAt(1);
            final char ch2 = str.charAt(2);
            final char ch3 = str.charAt(3);
            if ((ch0 == 't' || ch0 == 'T') && (ch1 == 'r' || ch1 == 'R') && (ch2 == 'u' || ch2 == 'U')
                    && (ch3 == 'e' || ch3 == 'E')) {
                return Boolean.TRUE;
            }
            break;
        }
        case 5: {
            final char ch0 = str.charAt(0);
            final char ch1 = str.charAt(1);
            final char ch2 = str.charAt(2);
            final char ch3 = str.charAt(3);
            final char ch4 = str.charAt(4);
            if ((ch0 == 'f' || ch0 == 'F') && (ch1 == 'a' || ch1 == 'A') && (ch2 == 'l' || ch2 == 'L')
                    && (ch3 == 's' || ch3 == 'S') && (ch4 == 'e' || ch4 == 'E')) {
                return Boolean.FALSE;
            }
            break;
        }
        default:
            break;
        }

        return null;
    }
}

Related

  1. toBooleanArray(int[] intArray, int n)
  2. toBooleanArray(Object[] vs)
  3. toBooleanDefaultIfNull(Boolean bool, boolean valueIfNull)
  4. toBooleanDefaultIfNull(Boolean bool, boolean valueIfNull)
  5. toBooleanObject(final int val)
  6. toBooleanObject(int value, int trueValue, int falseValue, int nullValue)
  7. toBooleanObject(int value, int trueValue, int falseValue, int nullValue)
  8. toBooleanObject(String str)
  9. toBooleans(byte[] bytes)