/** * To fix semantic difference of Longs.tryParse() from Long.parseLong (Longs.tryParse() returns null for '+' started value) */ @Nullable public static Long tryParseLong(@Nullable String string) { return Strings.isNullOrEmpty(string) ? null : Longs.tryParse(string.charAt(0) == '+' ? string.substring(1) : string); }
/** * Parses the specified string as a signed decimal long value. The ASCII character {@code '-'} ( * <code>'\u002D'</code>) is recognized as the minus sign. * * <p>Unlike {@link Long#parseLong(String)}, this method returns {@code null} instead of throwing * an exception if parsing fails. Additionally, this method only accepts ASCII digits, and returns * {@code null} if non-ASCII digits are present in the string. * * <p>Note that strings prefixed with ASCII {@code '+'} are rejected, even under JDK 7, despite * the change to {@link Long#parseLong(String)} for that version. * * @param string the string representation of a long value * @return the long value represented by {@code string}, or {@code null} if {@code string} has a * length of zero or cannot be parsed as a long value * @since 14.0 */ @Beta public static @Nullable Long tryParse(String string) { return tryParse(string, 10); }
private static OptionalLong tryParse(String value) { Long number = Longs.tryParse(value); if (number != null) { return OptionalLong.of(number); } return OptionalLong.empty(); }
/** * Parses the specified string as a signed integer value using the specified radix. The ASCII * character {@code '-'} (<code>'\u002D'</code>) is recognized as the minus sign. * * <p>Unlike {@link Integer#parseInt(String, int)}, this method returns {@code null} instead of * throwing an exception if parsing fails. Additionally, this method only accepts ASCII digits, * and returns {@code null} if non-ASCII digits are present in the string. * * <p>Note that strings prefixed with ASCII {@code '+'} are rejected, even under JDK 7, despite * the change to {@link Integer#parseInt(String, int)} for that version. * * @param string the string representation of an integer value * @param radix the radix to use when parsing * @return the integer value represented by {@code string} using {@code radix}, or {@code null} if * {@code string} has a length of zero or cannot be parsed as an integer value * @throws IllegalArgumentException if {@code radix < Character.MIN_RADIX} or {@code radix > * Character.MAX_RADIX} * @since 19.0 */ @Beta public static @Nullable Integer tryParse(String string, int radix) { Long result = Longs.tryParse(string, radix); if (result == null || result.longValue() != result.intValue()) { return null; } else { return result.intValue(); } } }
private static OptionalLong parse(@Nullable String parameterValue) { if (parameterValue == null) { return OptionalLong.empty(); } Long longValue = Longs.tryParse(parameterValue); if (longValue == null || longValue < 0) { return OptionalLong.empty(); } return OptionalLong.of(longValue); }
@Override public Long evaluate(FunctionArgs args, EvaluationContext context) { final Object evaluated = valueParam.required(args, context); final Long defaultValue = defaultParam.optional(args, context).orElse(0L); if (evaluated == null) { return defaultValue; } else if (evaluated instanceof Number) { return ((Number) evaluated).longValue(); } else { final String s = String.valueOf(evaluated); return firstNonNull(tryParse(s), defaultValue); } }
/** * Parses the specified string as a signed decimal long value. The ASCII character {@code '-'} ( * <code>'\u002D'</code>) is recognized as the minus sign. * * <p>Unlike {@link Long#parseLong(String)}, this method returns {@code null} instead of throwing * an exception if parsing fails. Additionally, this method only accepts ASCII digits, and returns * {@code null} if non-ASCII digits are present in the string. * * <p>Note that strings prefixed with ASCII {@code '+'} are rejected, even under JDK 7, despite * the change to {@link Long#parseLong(String)} for that version. * * @param string the string representation of a long value * @return the long value represented by {@code string}, or {@code null} if {@code string} has a * length of zero or cannot be parsed as a long value * @since 14.0 */ @Beta @NullableDecl public static Long tryParse(String string) { return tryParse(string, 10); }
public void testTryParse_radixTooSmall() { try { Longs.tryParse("0", Character.MIN_RADIX - 1); fail(); } catch (IllegalArgumentException expected) { } }
/** * Applies {@link Longs#tryParse(String)} to the given string and asserts that the result is as * expected. */ private static void tryParseAndAssertEquals(Long expected, String value) { assertEquals(expected, Longs.tryParse(value)); }
/** * Encodes the long as a string with given radix, then uses {@link Longs#tryParse(String, int)} to * parse the result. Asserts the result is the same as what we started with. */ private static void radixEncodeParseAndAssertEquals(Long value, int radix) { assertEquals("Radix: " + radix, value, Longs.tryParse(Long.toString(value, radix), radix)); }
public void testTryParse_radixTooBig() { try { Longs.tryParse("0", Character.MAX_RADIX + 1); fail(); } catch (IllegalArgumentException expected) { } }
/** * Parses the specified string as a signed integer value using the specified radix. The ASCII * character {@code '-'} (<code>'\u002D'</code>) is recognized as the minus sign. * * <p>Unlike {@link Integer#parseInt(String, int)}, this method returns {@code null} instead of * throwing an exception if parsing fails. Additionally, this method only accepts ASCII digits, * and returns {@code null} if non-ASCII digits are present in the string. * * <p>Note that strings prefixed with ASCII {@code '+'} are rejected, even under JDK 7, despite * the change to {@link Integer#parseInt(String, int)} for that version. * * @param string the string representation of an integer value * @param radix the radix to use when parsing * @return the integer value represented by {@code string} using {@code radix}, or {@code null} if * {@code string} has a length of zero or cannot be parsed as an integer value * @throws IllegalArgumentException if {@code radix < Character.MIN_RADIX} or {@code radix > * Character.MAX_RADIX} * @since 19.0 */ @Beta @NullableDecl public static Integer tryParse(String string, int radix) { Long result = Longs.tryParse(string, radix); if (result == null || result.longValue() != result.intValue()) { return null; } else { return result.intValue(); } } }
/** * Parses the specified string as a signed decimal long value. The ASCII character {@code '-'} ( * <code>'\u002D'</code>) is recognized as the minus sign. * * <p>Unlike {@link Long#parseLong(String)}, this method returns {@code null} instead of throwing * an exception if parsing fails. Additionally, this method only accepts ASCII digits, and returns * {@code null} if non-ASCII digits are present in the string. * * <p>Note that strings prefixed with ASCII {@code '+'} are rejected, even under JDK 7, despite * the change to {@link Long#parseLong(String)} for that version. * * @param string the string representation of a long value * @return the long value represented by {@code string}, or {@code null} if {@code string} has a * length of zero or cannot be parsed as a long value * @since 14.0 */ @Beta @NullableDecl public static Long tryParse(String string) { return tryParse(string, 10); }
/** * 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; }
public void testTryParse_radix() { for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) { radixEncodeParseAndAssertEquals((long) 0, radix); radixEncodeParseAndAssertEquals((long) 8000, radix); radixEncodeParseAndAssertEquals((long) -8000, radix); radixEncodeParseAndAssertEquals(MAX_VALUE, radix); radixEncodeParseAndAssertEquals(MIN_VALUE, radix); assertNull("Radix: " + radix, Longs.tryParse("999999999999999999999999", radix)); assertNull( "Radix: " + radix, Longs.tryParse(BigInteger.valueOf(MAX_VALUE).add(BigInteger.ONE).toString(), radix)); assertNull( "Radix: " + radix, Longs.tryParse(BigInteger.valueOf(MIN_VALUE).subtract(BigInteger.ONE).toString(), radix)); } assertNull("Hex string and dec parm", Longs.tryParse("FFFF", 10)); assertEquals("Mixed hex case", 65535, Longs.tryParse("ffFF", 16).longValue()); }
@Override public ColumnValueSelector<?> makeColumnValueSelector(String columnName, ColumnSelectorFactory factory) { final String subColumn = VirtualColumns.splitColumnName(columnName).rhs; final Long boxed = subColumn == null ? null : Longs.tryParse(subColumn); final long theLong = boxed == null ? -1 : boxed; return new TestLongColumnSelector() { @Override public long getLong() { return theLong; } @Override public boolean isNull() { return false; } }; }
public void testTryParse() { tryParseAndAssertEquals(0L, "0"); tryParseAndAssertEquals(0L, "-0"); tryParseAndAssertEquals(1L, "1"); tryParseAndAssertEquals(-1L, "-1"); tryParseAndAssertEquals(8900L, "8900"); tryParseAndAssertEquals(-8900L, "-8900"); tryParseAndAssertEquals(MAX_VALUE, Long.toString(MAX_VALUE)); tryParseAndAssertEquals(MIN_VALUE, Long.toString(MIN_VALUE)); assertNull(Longs.tryParse("")); assertNull(Longs.tryParse("-")); assertNull(Longs.tryParse("+1")); assertNull(Longs.tryParse("999999999999999999999999")); assertNull( "Max long + 1", Longs.tryParse(BigInteger.valueOf(MAX_VALUE).add(BigInteger.ONE).toString())); assertNull( "Max long * 10", Longs.tryParse(BigInteger.valueOf(MAX_VALUE).multiply(BigInteger.TEN).toString())); assertNull( "Min long - 1", Longs.tryParse(BigInteger.valueOf(MIN_VALUE).subtract(BigInteger.ONE).toString())); assertNull( "Min long * 10", Longs.tryParse(BigInteger.valueOf(MIN_VALUE).multiply(BigInteger.TEN).toString())); assertNull(Longs.tryParse("\u0662\u06f3")); }
Long v = Longs.tryParse(metricValueString);
@Test public void testParseLong() { Assert.assertNull(Longs.tryParse("+100")); Assert.assertNull(GuavaUtils.tryParseLong("")); Assert.assertNull(GuavaUtils.tryParseLong(null)); Assert.assertNull(GuavaUtils.tryParseLong("+")); Assert.assertNull(GuavaUtils.tryParseLong("++100")); Assert.assertEquals((Object) Long.parseLong("+100"), GuavaUtils.tryParseLong("+100")); Assert.assertEquals((Object) Long.parseLong("-100"), GuavaUtils.tryParseLong("-100")); Assert.assertNotEquals(new Long(100), GuavaUtils.tryParseLong("+101")); }