/** * Coerce the supplied object to a short number, parse it if necessary. * * @param obj Object to coerce * @return Object as a short, <code>0</code> if the object is null or cannot * be parsed */ public static short toShort(@Nullable Object obj) { if (obj == null) { return 0; } if (obj instanceof Number) { return ((Number) obj).shortValue(); } try { return Short.parseShort(Coerce.sanitiseNumber(obj)); } catch (NumberFormatException e) { return 0; } }
/** * Coerce the supplied object to a long number, parse it if necessary. * * @param obj Object to coerce * @return Object as a long, <code>0</code> if the object is null or cannot * be parsed */ public static long toLong(@Nullable Object obj) { if (obj == null) { return 0; } if (obj instanceof Number) { return ((Number) obj).longValue(); } try { return Long.parseLong(Coerce.sanitiseNumber(obj)); } catch (NumberFormatException e) { return 0; } }
/** * Coerce the supplied object to a byte number, parse it if necessary. * * @param obj Object to coerce * @return Object as a byte, <code>0</code> if the object is null or cannot * be parsed */ public static byte toByte(@Nullable Object obj) { if (obj == null) { return 0; } if (obj instanceof Number) { return ((Number) obj).byteValue(); } try { return Byte.parseByte(Coerce.sanitiseNumber(obj)); } catch (NumberFormatException e) { return 0; } }
/** * Gets the given object as a {@link Long}. * * <p>Note that this does not translate numbers spelled out as strings.</p> * * @param obj The object to translate * @return The long value, if available */ public static Optional<Long> asLong(@Nullable Object obj) { if (obj == null) { // fail fast return Optional.empty(); } if (obj instanceof Number) { return Optional.of(((Number) obj).longValue()); } try { return Optional.ofNullable(Long.parseLong(Coerce.sanitiseNumber(obj))); } catch (NumberFormatException | NullPointerException e) { // do nothing } return Optional.empty(); }
/** * Gets the given object as a {@link Short}. * * <p>Note that this does not translate numbers spelled out as strings.</p> * * @param obj The object to translate * @return The short value, if available */ public static Optional<Short> asShort(@Nullable Object obj) { if (obj == null) { // fail fast return Optional.empty(); } if (obj instanceof Number) { return Optional.of(((Number) obj).shortValue()); } try { return Optional.ofNullable(Short.parseShort(Coerce.sanitiseNumber(obj))); } catch (NumberFormatException | NullPointerException e) { // do nothing } return Optional.empty(); }
/** * Coerce the supplied object to a single-precision floating-point number, * parse it if necessary. * * @param obj Object to coerce * @return Object as a float, <code>0.0</code> if the object is null or * cannot be parsed */ public static float toFloat(@Nullable Object obj) { if (obj == null) { return 0.0f; } if (obj instanceof Number) { return ((Number) obj).floatValue(); } Float parsed = Floats.tryParse(Coerce.sanitiseNumber(obj)); return parsed != null ? parsed : 0.0f; }
/** * Gets the given object as a {@link Byte}. * * <p>Note that this does not translate numbers spelled out as strings.</p> * * @param obj The object to translate * @return The byte value, if available */ public static Optional<Byte> asByte(@Nullable Object obj) { if (obj == null) { // fail fast return Optional.empty(); } if (obj instanceof Number) { return Optional.of(((Number) obj).byteValue()); } try { return Optional.ofNullable(Byte.parseByte(Coerce.sanitiseNumber(obj))); } catch (NumberFormatException | NullPointerException e) { // do nothing } return Optional.empty(); }
/** * Coerce the supplied object to a double-precision floating-point number, * parse it if necessary. * * @param obj Object to coerce * @return Object as a double, <code>0.0</code> if the object is null or * cannot be parsed */ public static double toDouble(@Nullable Object obj) { if (obj == null) { return 0.0; } if (obj instanceof Number) { return ((Number)obj).doubleValue(); } Double parsed = Doubles.tryParse(Coerce.sanitiseNumber(obj)); return parsed != null ? parsed : 0.0; }
/** * Gets the given object as a {@link Float}. * * <p>Note that this does not translate numbers spelled out as strings.</p> * * @param obj The object to translate * @return The float value, if available */ public static Optional<Float> asFloat(@Nullable Object obj) { if (obj == null) { // fail fast return Optional.empty(); } if (obj instanceof Number) { return Optional.of(((Number) obj).floatValue()); } try { return Optional.ofNullable(Float.valueOf(obj.toString())); } catch (NumberFormatException | NullPointerException e) { // do nothing } String strObj = Coerce.sanitiseNumber(obj); Double dParsed = Doubles.tryParse(strObj); return dParsed == null ? Optional.<Float>empty() : Optional.of(dParsed.floatValue()); }
/** * Gets the given object as a {@link Double}. * * <p>Note that this does not translate numbers spelled out as strings.</p> * * @param obj The object to translate * @return The double value, if available */ public static Optional<Double> asDouble(@Nullable Object obj) { if (obj == null) { // fail fast return Optional.empty(); } if (obj instanceof Number) { return Optional.of(((Number) obj).doubleValue()); } try { return Optional.ofNullable(Double.valueOf(obj.toString())); } catch (NumberFormatException | NullPointerException e) { // do nothing } String strObj = Coerce.sanitiseNumber(obj); Double dParsed = Doubles.tryParse(strObj); // try parsing as double now return dParsed == null ? Optional.<Double>empty() : Optional.of(dParsed); }
/** * Sanitise a string containing a common representation of a number to make * it parsable. Strips thousand-separating commas and trims later members * of a comma-separated list. For example the string "(9.5, 10.6, 33.2)" * will be sanitised to "9.5". * * @param obj Object to sanitise * @return Sanitised number-format string to parse */ private static String sanitiseNumber(Object obj) { String string = obj.toString().trim(); if (string.length() < 1) { return "0"; } Matcher candidate = Coerce.listPattern.matcher(string); if (Coerce.listBracketsMatch(candidate)) { string = candidate.group(2).trim(); } int decimal = string.indexOf('.'); int comma = string.indexOf(',', decimal); if (decimal > -1 && comma > -1) { return Coerce.sanitiseNumber(string.substring(0, comma)); } if (string.indexOf('-', 1) != -1) { return "0"; } return string.replace(",", "").split(" ")[0]; }
/** * Gets the given object as a {@link Integer}. * * <p>Note that this does not translate numbers spelled out as strings.</p> * * @param obj The object to translate * @return The integer value, if available */ public static Optional<Integer> asInteger(@Nullable Object obj) { if (obj == null) { // fail fast return Optional.empty(); } if (obj instanceof Number) { return Optional.of(((Number) obj).intValue()); } try { return Optional.ofNullable(Integer.valueOf(obj.toString())); } catch (NumberFormatException | NullPointerException e) { // do nothing } String strObj = Coerce.sanitiseNumber(obj); Integer iParsed = Ints.tryParse(strObj); if (iParsed == null) { Double dParsed = Doubles.tryParse(strObj); // try parsing as double now return dParsed == null ? Optional.<Integer>empty() : Optional.of(dParsed.intValue()); } return Optional.of(iParsed); }
/** * Coerce the supplied object to an integer, parse it if necessary. * * @param obj Object to coerce * @return Object as an integer, <code>0</code> if the object is null or * cannot be parsed */ public static int toInteger(@Nullable Object obj) { if (obj == null) { return 0; } if (obj instanceof Number) { return ((Number)obj).intValue(); } String strObj = Coerce.sanitiseNumber(obj); Integer iParsed = Ints.tryParse(strObj); if (iParsed != null) { return iParsed; } Double dParsed = Doubles.tryParse(strObj); return dParsed != null ? dParsed.intValue() : 0; }
/** * Coerce the supplied object to a byte number, parse it if necessary. * * @param obj Object to coerce * @return Object as a byte, <code>0</code> if the object is null or cannot * be parsed */ public static byte toByte(@Nullable Object obj) { if (obj == null) { return 0; } if (obj instanceof Number) { return ((Number) obj).byteValue(); } try { return Byte.parseByte(Coerce.sanitiseNumber(obj)); } catch (NumberFormatException e) { return 0; } }
/** * Coerce the supplied object to a short number, parse it if necessary. * * @param obj Object to coerce * @return Object as a short, <code>0</code> if the object is null or cannot * be parsed */ public static short toShort(@Nullable Object obj) { if (obj == null) { return 0; } if (obj instanceof Number) { return ((Number) obj).shortValue(); } try { return Short.parseShort(Coerce.sanitiseNumber(obj)); } catch (NumberFormatException e) { return 0; } }
/** * Coerce the supplied object to a long number, parse it if necessary. * * @param obj Object to coerce * @return Object as a long, <code>0</code> if the object is null or cannot * be parsed */ public static long toLong(@Nullable Object obj) { if (obj == null) { return 0; } if (obj instanceof Number) { return ((Number) obj).longValue(); } try { return Long.parseLong(Coerce.sanitiseNumber(obj)); } catch (NumberFormatException e) { return 0; } }
/** * Coerce the supplied object to a double-precision floating-point number, * parse it if necessary. * * @param obj Object to coerce * @return Object as a double, <code>0.0</code> if the object is null or * cannot be parsed */ public static double toDouble(@Nullable Object obj) { if (obj == null) { return 0.0; } if (obj instanceof Number) { return ((Number)obj).doubleValue(); } Double parsed = Doubles.tryParse(Coerce.sanitiseNumber(obj)); return parsed != null ? parsed : 0.0; }
/** * Coerce the supplied object to a single-precision floating-point number, * parse it if necessary. * * @param obj Object to coerce * @return Object as a float, <code>0.0</code> if the object is null or * cannot be parsed */ public static float toFloat(@Nullable Object obj) { if (obj == null) { return 0.0f; } if (obj instanceof Number) { return ((Number) obj).floatValue(); } Float parsed = Floats.tryParse(Coerce.sanitiseNumber(obj)); return parsed != null ? parsed : 0.0f; }
/** * Gets the given object as a {@link Float}. * * <p>Note that this does not translate numbers spelled out as strings.</p> * * @param obj The object to translate * @return The float value, if available */ public static Optional<Float> asFloat(@Nullable Object obj) { if (obj == null) { // fail fast return Optional.empty(); } if (obj instanceof Number) { return Optional.of(((Number) obj).floatValue()); } try { return Optional.ofNullable(Float.valueOf(obj.toString())); } catch (NumberFormatException | NullPointerException e) { // do nothing } String strObj = Coerce.sanitiseNumber(obj); Double dParsed = Doubles.tryParse(strObj); return dParsed == null ? Optional.<Float>empty() : Optional.of(dParsed.floatValue()); }
/** * Coerce the supplied object to an integer, parse it if necessary. * * @param obj Object to coerce * @return Object as an integer, <code>0</code> if the object is null or * cannot be parsed */ public static int toInteger(@Nullable Object obj) { if (obj == null) { return 0; } if (obj instanceof Number) { return ((Number)obj).intValue(); } String strObj = Coerce.sanitiseNumber(obj); Integer iParsed = Ints.tryParse(strObj); if (iParsed != null) { return iParsed; } Double dParsed = Doubles.tryParse(strObj); return dParsed != null ? dParsed.intValue() : 0; }