Beispiel #1
0
  public static float toFloat(Object obj) {
    if (obj.getClass() == float.class) {
      return (Float) obj;
    }

    try {
      if (obj instanceof Float) {
        return (Float) obj;
      } else if (obj instanceof Number) {
        return ((Number) obj).floatValue();
      } else if (obj instanceof CharSequence) {
        try {
          return Float.parseFloat(Str.toString(obj));
        } catch (Exception ex) {
          die(String.format("Unable to convert %s to a float", obj.getClass()));
          return Float.MIN_VALUE;
        }
      } else {
      }
    } catch (Exception ex) {

      log.warning(
          String.format(
              "unable to convert to float and there was an exception %s", ex.getMessage()));
    }

    die(String.format("Unable to convert %s to a float", obj.getClass()));
    return Float.MIN_VALUE;
  }
Beispiel #2
0
  public static long toLong(Object obj, final long longDefault) {

    if (obj instanceof Long) {
      return (Long) obj;
    }

    if (obj instanceof Number) {
      return ((Number) obj).longValue();
    } else if (obj instanceof CharSequence) {
      String str = Str.toString(obj);
      if (Dates.isJsonDate(str)) {
        return Dates.fromJsonDate(str).getTime();
      }

      try {
        return StringScanner.parseLong(str);
      } catch (Exception ex) {
        return longDefault;
      }
    } else if (obj instanceof Date) {
      return ((Date) obj).getTime();
    } else {
      return toInt(obj);
    }
  }
Beispiel #3
0
  public static Currency toCurrency(Object obj) {
    if (obj instanceof Currency) {
      return (Currency) obj;
    }

    if (obj instanceof String) {
      String str = Str.toString(obj);
      return Currency.getInstance(str);
    }

    return null;
  }
Beispiel #4
0
 public static int toInt(Object obj, int defaultValue) {
   if (obj.getClass() == int.class) {
     return int.class.cast(obj);
   }
   if (obj instanceof Number) {
     return ((Number) obj).intValue();
   } else if (obj instanceof Boolean || obj.getClass() == Boolean.class) {
     boolean value = toBoolean(obj);
     return value ? 1 : 0;
   } else if (obj instanceof CharSequence) {
     try {
       return StringScanner.parseInt(Str.toString(obj));
     } catch (Exception ex) {
       return defaultValue;
     }
   }
   return defaultValue;
 }