示例#1
0
  public static boolean toBooleanOrDie(Object obj) {

    if (obj == null) {
      die("Can't convert boolean from a null");
    }

    if (obj instanceof Boolean) {
      return ((Boolean) obj).booleanValue();
    } else if (obj instanceof String || obj instanceof CharSequence) {
      String str = Conversions.toString(obj);
      if (str.length() == 0) {
        return false;
      }
      if (str.equals("false")) {
        return false;
      } else if (str.equals("true")) {
        return true;
      }
      die("Can't convert string", obj, "to boolean ");
      return false;
    } else {
      die("Can't convert", obj, "to boolean ");
      return false;
    }
  }
示例#2
0
  /**
   * Converts the value to boolean, and if it is null, it uses the default value passed.
   *
   * @param obj value to convert to boolean
   * @param defaultValue default value
   * @return obj converted to boolean
   */
  public static boolean toBoolean(Object obj, boolean defaultValue) {

    if (obj == null) {
      return defaultValue;
    }

    if (obj instanceof Boolean) {
      return ((Boolean) obj).booleanValue();
    } else if (obj instanceof Number || obj.getClass().isPrimitive()) {
      int value = toInt(obj);
      return value != 0 ? true : false;
    } else if (obj instanceof String || obj instanceof CharSequence) {
      String str = Conversions.toString(obj);
      if (str.length() == 0) {
        return false;
      }
      if (str.equals("false")) {
        return false;
      } else {
        return true;
      }
    } else if (Boon.isArray(obj)) {
      return Boon.len(obj) > 0;
    } else if (obj instanceof Collection) {

      if (len(obj) > 0) {
        List list = Lists.list((Collection) obj);
        while (list.remove(null)) {}

        return Lists.len(list) > 0;
      } else {
        return false;
      }
    } else {
      return toBoolean(Conversions.toString(obj));
    }
  }