private void initDoublePredicate() { if (druidDoublePredicate != null) { return; } synchronized (initLock) { if (druidDoublePredicate != null) { return; } if (value == null) { druidDoublePredicate = DruidDoublePredicate.MATCH_NULL_ONLY; return; } final Double aDouble = Doubles.tryParse(value); if (aDouble == null) { druidDoublePredicate = DruidDoublePredicate.ALWAYS_FALSE; } else { final long bits = Double.doubleToLongBits(aDouble); druidDoublePredicate = input -> Double.doubleToLongBits(input) == bits; } } } }
@Nullable private Number computeNumber() { if (value == null) { return null; } if (numericVal != null) { // Optimization for non-null case. return numericVal; } Number rv; Long v = GuavaUtils.tryParseLong(value); // Do NOT use ternary operator here, because it makes Java to convert Long to Double if (v != null) { rv = v; } else { rv = Doubles.tryParse(value); } numericVal = rv; return rv; }
return Doubles.tryParse(x.toString());
@Override public Double evaluate(FunctionArgs args, EvaluationContext context) { final Object evaluated = valueParam.required(args, context); final Double defaultValue = defaultParam.optional(args, context).orElse(0d); if (evaluated == null) { return defaultValue; } else if (evaluated instanceof Number) { return ((Number) evaluated).doubleValue(); } else { final String s = String.valueOf(evaluated); return firstNonNull(Doubles.tryParse(s), defaultValue); } }
@Nullable public static Double convertObjectToDouble(@Nullable Object valObj, boolean reportParseExceptions) { if (valObj == null) { return null; } if (valObj instanceof Double) { return (Double) valObj; } else if (valObj instanceof Number) { return ((Number) valObj).doubleValue(); } else if (valObj instanceof String) { Double ret = Doubles.tryParse((String) valObj); if (reportParseExceptions && ret == null) { throw new ParseException("could not convert value [%s] to double", valObj); } return ret; } else { throw new ParseException("Unknown type[%s]", valObj.getClass()); } }
/** * Convert the event value taken from the metadata to double (default type). * It falls back to string type if the value is missing or it is non-numeric * is of string or missing * Metadata entries are emitted as distinct events (see {@link MultiPartEvent}) * * @param field {@link GobblinTrackingEvent} metadata key * @param value {@link GobblinTrackingEvent} metadata value * @return The converted event value */ private Object convertValue(String field, String value) { if (value == null) return EMTPY_VALUE; if (METADATA_DURATION.equals(field)) { return convertDuration(TimeUnit.MILLISECONDS.toNanos(Long.parseLong(value))); } else { Double doubleValue = Doubles.tryParse(value); return (doubleValue == null) ? value : doubleValue; } }
/** * 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); }
/** * Attempts to convert the provided string value to a numeric type, * trying Integer, Long and Double in order until successful. */ @Override public Object convert(String value) { if (value == null || value.isEmpty()) { return value; } Object result = Ints.tryParse(value); if (result != null) { return result; } result = Longs.tryParse(value); if (result != null) { return result; } result = Doubles.tryParse(value); if (result != null) { return result; } return value; }
/** * 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; }
@GwtIncompatible // Doubles.tryParse private static void checkTryParse(double expected, String input) { assertEquals(Double.valueOf(expected), Doubles.tryParse(input)); assertThat(input) .matches( Pattern.compile( Doubles.FLOATING_POINT_PATTERN.pattern(), Doubles.FLOATING_POINT_PATTERN.flags())); }
private void initDoubleValues() { if (predicate != null) { return; } synchronized (initLock) { if (predicate != null) { return; } LongArrayList doubleBits = new LongArrayList(values.size()); for (String value : values) { Double doubleValue = Doubles.tryParse(value); if (doubleValue != null) { doubleBits.add(Double.doubleToLongBits((doubleValue))); } } if (doubleBits.size() > NUMERIC_HASHING_THRESHOLD) { final LongOpenHashSet doubleBitsHashSet = new LongOpenHashSet(doubleBits); predicate = input -> doubleBitsHashSet.contains(Double.doubleToLongBits(input)); } else { final long[] doubleBitsArray = doubleBits.toLongArray(); Arrays.sort(doubleBitsArray); predicate = input -> Arrays.binarySearch(doubleBitsArray, Double.doubleToLongBits(input)) >= 0; } } } @Override
@GwtIncompatible // Doubles.tryParse private static void checkTryParse(String input) { Double expected = referenceTryParse(input); assertEquals(expected, Doubles.tryParse(input)); if (expected != null && !Doubles.FLOATING_POINT_PATTERN.matcher(input).matches()) { // TODO(cpovirk): Use SourceCodeEscapers if it is added to Guava. StringBuilder escapedInput = new StringBuilder(); for (char c : input.toCharArray()) { if (c >= 0x20 && c <= 0x7E) { escapedInput.append(c); } else { escapedInput.append(String.format("\\u%04x", (int) c)); } } fail("FLOATING_POINT_PATTERN should have matched valid input <" + escapedInput + ">"); } }
@GwtIncompatible // Doubles.tryParse public void testTryParseFailures() { for (String badInput : BAD_TRY_PARSE_INPUTS) { assertThat(badInput) .doesNotMatch( Pattern.compile( Doubles.FLOATING_POINT_PATTERN.pattern(), Doubles.FLOATING_POINT_PATTERN.flags())); assertEquals(referenceTryParse(badInput), Doubles.tryParse(badInput)); assertNull(Doubles.tryParse(badInput)); } }
Assert.assertEquals( result.toString(), Doubles.tryParse(expectedIndex[count]).doubleValue(), value.getDoubleMetric("index").doubleValue(), value.getDoubleMetric("index").doubleValue() * 1e-6
public Float apply(Object input) { return input instanceof Number ? ((Number) input).floatValue() : Doubles.tryParse(String.valueOf(input)).floatValue(); } });
@Override public boolean matches(@Nonnull String s) { // We use Doubles.tryParse because failures are expected // and Double.parseDouble uses exceptions to indicate failure, // which are expensive. Double d = Doubles.tryParse(s); return d != null && predicate.eval(d, value); } }