/** * Parse the supplied string as a number. * * @param value the string representation of a integer value * @return the number, or {@code null} if the value is not a number */ public static Number asNumber(String value) { return asNumber(value, null); }
/** * Get the integer value associated with the given key, using the given supplier to obtain a default value if there is no such * key-value pair. * * @param key the key for the configuration property * @param defaultValueSupplier the supplier for the default value; may be null * @return the integer value, or null if the key is null, there is no such key-value pair in the configuration, the * {@code defaultValueSupplier} reference is null, or there is a key-value pair in the configuration but the value * could not be parsed as an integer */ default Number getNumber(String key, Supplier<Number> defaultValueSupplier) { String value = getString(key); return Strings.asNumber(value, defaultValueSupplier); }
@Override public Number asNumber() { if (value.isNumber()) return value.asNumber(); if (value.isString()) { String str = value.asString(); Number number = Strings.asNumber(str); if ( number instanceof Short ) { // Shorts aren't allowed, so just use an integer ... number = Integer.valueOf(number.intValue()); } return number; } return null; }
/** * Get the numeric value associated with the given field, returning the field's default value if there is no such * key-value pair. * * @param field the field * @return the integer value, or null if the key is null, there is no such key-value pair in the configuration and there is * no default value in the field or the default value could not be parsed as a long, or there is a key-value pair in * the configuration but the value could not be parsed as an integer value * @throws NumberFormatException if there is no name-value pair and the field has no default value */ default Number getNumber(Field field) { return getNumber(field.name(), () -> Strings.asNumber(field.defaultValueAsString())); }
/** * Parse the supplied string as a number. * * @param value the string representation of a integer value * @return the number, or {@code null} if the value is not a number */ public static Number asNumber(String value) { return asNumber(value, null); }
/** * Get the integer value associated with the given key, using the given supplier to obtain a default value if there is no such * key-value pair. * * @param key the key for the configuration property * @param defaultValueSupplier the supplier for the default value; may be null * @return the integer value, or null if the key is null, there is no such key-value pair in the configuration, the * {@code defaultValueSupplier} reference is null, or there is a key-value pair in the configuration but the value * could not be parsed as an integer */ default Number getNumber(String key, Supplier<Number> defaultValueSupplier) { String value = getString(key); return Strings.asNumber(value, defaultValueSupplier); }
@Override public Number asNumber() { if (value.isNumber()) return value.asNumber(); if (value.isString()) { String str = value.asString(); Number number = Strings.asNumber(str); if ( number instanceof Short ) { // Shorts aren't allowed, so just use an integer ... number = Integer.valueOf(number.intValue()); } return number; } return null; }
/** * Get the numeric value associated with the given field, returning the field's default value if there is no such * key-value pair. * * @param field the field * @return the integer value, or null if the key is null, there is no such key-value pair in the configuration and there is * no default value in the field or the default value could not be parsed as a long, or there is a key-value pair in * the configuration but the value could not be parsed as an integer value * @throws NumberFormatException if there is no name-value pair and the field has no default value */ default Number getNumber(Field field) { return getNumber(field.name(), () -> Strings.asNumber(field.defaultValueAsString())); }