/** * 支持true/false, on/off, y/n, yes/no的转换, str为空或无法分析时返回null */ public static Boolean parseGeneralString(String str) { return BooleanUtils.toBooleanObject(str); }
private static Boolean interpret(String arg) { return BooleanUtils.toBooleanObject(arg); }
return toBooleanObject(str) == Boolean.TRUE;
/** * 支持true/false,on/off, y/n, yes/no的转换, str为空或无法分析时返回defaultValue */ public static Boolean parseGeneralString(String str, Boolean defaultValue) { return BooleanUtils.toBooleanDefaultIfNull(BooleanUtils.toBooleanObject(str), defaultValue); }
@Test(expected = IllegalArgumentException.class) public void test_toBooleanObject_String_String_String_String_nullValue() { BooleanUtils.toBooleanObject(null, "Y", "N", "U"); }
@Test(expected = IllegalArgumentException.class) public void test_toBooleanObject_int_int_int_noMatch() { BooleanUtils.toBooleanObject(9, 6, 7, 8); }
@Test(expected = IllegalArgumentException.class) public void test_toBooleanObject_String_String_String_String_noMatch() { BooleanUtils.toBooleanObject("X", "Y", "N", "U"); }
@Test(expected = IllegalArgumentException.class) public void test_toBooleanObject_Integer_Integer_Integer_Integer_nullValue() { BooleanUtils.toBooleanObject(null, Integer.valueOf(6), Integer.valueOf(7), Integer.valueOf(8)); }
@Test(expected = IllegalArgumentException.class) public void test_toBooleanObject_Integer_Integer_Integer_Integer_noMatch() { BooleanUtils.toBooleanObject(Integer.valueOf(9), Integer.valueOf(6), Integer.valueOf(7), Integer.valueOf(8)); }
@Test public void test_toBooleanObject_Integer_Integer_Integer_Integer() { final Integer six = Integer.valueOf(6); final Integer seven = Integer.valueOf(7); final Integer eight = Integer.valueOf(8); assertSame(Boolean.TRUE, BooleanUtils.toBooleanObject(null, null, seven, eight)); assertSame(Boolean.FALSE, BooleanUtils.toBooleanObject(null, six, null, eight)); assertSame(null, BooleanUtils.toBooleanObject(null, six, seven, null)); assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject(Integer.valueOf(6), six, seven, eight)); assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject(Integer.valueOf(7), six, seven, eight)); assertNull(BooleanUtils.toBooleanObject(Integer.valueOf(8), six, seven, eight)); }
@Test public void test_toBooleanObject_String_String_String_String() { assertSame(Boolean.TRUE, BooleanUtils.toBooleanObject(null, null, "N", "U")); assertSame(Boolean.FALSE, BooleanUtils.toBooleanObject(null, "Y", null, "U")); assertSame(null, BooleanUtils.toBooleanObject(null, "Y", "N", null)); assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("Y", "Y", "N", "U")); assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("N", "Y", "N", "U")); assertNull(BooleanUtils.toBooleanObject("U", "Y", "N", "U")); }
@Test public void test_toBooleanObject_Integer() { assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject(Integer.valueOf(1))); assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject(Integer.valueOf(-1))); assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject(Integer.valueOf(0))); assertNull(BooleanUtils.toBooleanObject((Integer) null)); }
@Test public void test_toBooleanObject_int_int_int() { assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject(6, 6, 7, 8)); assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject(7, 6, 7, 8)); assertNull(BooleanUtils.toBooleanObject(8, 6, 7, 8)); }
@Test public void test_toBooleanObject_int() { assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject(1)); assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject(-1)); assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject(0)); }
@Test public void test_toBooleanObject_String() { assertNull(BooleanUtils.toBooleanObject((String) null)); assertNull(BooleanUtils.toBooleanObject("")); assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("false")); assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("no")); assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("off")); assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("FALSE")); assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("NO")); assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("OFF")); assertNull(BooleanUtils.toBooleanObject("oof")); assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("true")); assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("yes")); assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("on")); assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("TRUE")); assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("ON")); assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("YES")); assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("TruE")); assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("TruE")); assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("y")); // yes assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("Y")); assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("t")); // true assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("T")); assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("f")); // false assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("F")); assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("n")); // No assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("N")); assertNull(BooleanUtils.toBooleanObject("z"));
@Override public Boolean getBooleanProperty(String key, Boolean defaultValue) { String value = getProperty(key); if (value == null) { return defaultValue; } return ObjectUtils.defaultIfNull(BooleanUtils.toBooleanObject(value), defaultValue); }
final Boolean b = BooleanUtils.toBooleanObject((String) value); if (b == null)
public boolean isOverwrite() { Boolean booleanObject = BooleanUtils.toBooleanObject(System.getProperty(OVERWRITE)); if (booleanObject == null) { booleanObject = Boolean.FALSE; } return booleanObject; } }
/** * Behaves much like org.apache.commons.lang3.BooleanUtils but returns the defaultValue if * the input string is null, empty, or unrecognized. */ public static boolean toBoolean(String s, boolean defaultValue) { final Boolean b = BooleanUtils.toBooleanObject(s); return b == null ? defaultValue : b.booleanValue(); }
private Boolean parseBoolValue(Object object) { if (object instanceof Boolean) { return (Boolean) object; } else if (object instanceof Number) { Byte num = ((Number) object).byteValue(); return num > 0; } return BooleanUtils.toBooleanObject(String.valueOf(object)); }