@Override public boolean accept(File dir, String fileName) { return !(Ints.tryParse(fileName) == null); } }
/** * Parses the specified string as a signed decimal integer value. The ASCII character {@code '-'} * (<code>'\u002D'</code>) is recognized as the minus sign. * * <p>Unlike {@link Integer#parseInt(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 Integer#parseInt(String)} for that version. * * @param string the string representation of an integer value * @return the integer value represented by {@code string}, or {@code null} if {@code string} has * a length of zero or cannot be parsed as an integer value * @since 11.0 */ @Beta public static @Nullable Integer tryParse(String string) { return tryParse(string, 10); }
@Override public Object convert(String value) { if (value == null || value.isEmpty()) { return value; } Integer priority = Ints.tryParse(value); if (priority == null) { return value; } return SyslogPriUtilities.levelFromPriority(priority); }
void init() { id = id.toUpperCase(Locale.ROOT); Matcher m = P.matcher(expression); if (m.matches()) { name = m.group(1).toUpperCase(Locale.ROOT); Integer p = m.group(2) != null ? Ints.tryParse(m.group(2)) : null; Integer s = m.group(3) != null ? Ints.tryParse(m.group(3)) : null; defaultPrecision = p != null ? p : -1; defaultScale = s != null ? s : -1; } }
@Override public void set(String text) { Integer val = Ints.tryParse(text); if (val != null && validRange.contains(val)) { // An alternative would be to clamp the value to min/max intBinding.set(val); } } }
@Override public String evaluate(FunctionArgs args, EvaluationContext context) { final String s = String.valueOf(valueParam.required(args, context)); final Integer level = firstNonNull(Ints.tryParse(s), -1); return SyslogUtils.levelToString(level); }
/** * Parses the specified string as a signed decimal integer value. The ASCII character {@code '-'} * (<code>'\u002D'</code>) is recognized as the minus sign. * * <p>Unlike {@link Integer#parseInt(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 Integer#parseInt(String)} for that version. * * @param string the string representation of an integer value * @return the integer value represented by {@code string}, or {@code null} if {@code string} has * a length of zero or cannot be parsed as an integer value * @since 11.0 */ @Beta @NullableDecl public static Integer tryParse(String string) { return tryParse(string, 10); }
@Override public String evaluate(FunctionArgs args, EvaluationContext context) { final String s = String.valueOf(valueParam.required(args, context)); final Integer facility = firstNonNull(Ints.tryParse(s), -1); return SyslogUtils.facilityToString(facility); }
@Override public Object convert(String value) { if (value == null || value.isEmpty()) { return value; } final Integer priority = Ints.tryParse(value); if (priority == null) { return value; } return Tools.syslogFacilityToReadable(SyslogPriUtilities.facilityFromPriority(priority)); }
/** * Applies {@link Ints#tryParse(String)} to the given string and asserts that the result is as * expected. */ private static void tryParseAndAssertEquals(Integer expected, String value) { assertEquals(expected, Ints.tryParse(value)); }
/** * Encodes the an integer as a string with given radix, then uses {@link Ints#tryParse(String, * int)} to parse the result. Asserts the result is the same as what we started with. */ private static void radixEncodeParseAndAssertEquals(Integer value, int radix) { assertEquals("Radix: " + radix, value, Ints.tryParse(Integer.toString(value, radix), radix)); }
public void testTryParse_radixTooBig() { try { Ints.tryParse("0", Character.MAX_RADIX + 1); fail(); } catch (IllegalArgumentException expected) { } }
public void testTryParse_radixTooSmall() { try { Ints.tryParse("0", Character.MIN_RADIX - 1); fail(); } catch (IllegalArgumentException expected) { } }
@Override public Boolean get() { return !nameText.getText().isEmpty() && !addressText.getText().isEmpty() && Ints.tryParse(portText.getText()) != null; } });
private int parseTimeout() { String timeout = request.headers().get(Constants.HTTP_HEAD_READ_TIMEOUT); if (timeout != null) { request.headers().remove(Constants.HTTP_HEAD_READ_TIMEOUT); Integer integer = Ints.tryParse(timeout); if (integer != null && integer > 0) { return integer; } } return TIMEOUT; }
public void testTryParse_radix() { for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) { radixEncodeParseAndAssertEquals(0, radix); radixEncodeParseAndAssertEquals(8000, radix); radixEncodeParseAndAssertEquals(-8000, radix); radixEncodeParseAndAssertEquals(GREATEST, radix); radixEncodeParseAndAssertEquals(LEAST, radix); assertNull("Radix: " + radix, Ints.tryParse("9999999999999999", radix)); assertNull( "Radix: " + radix, Ints.tryParse(Long.toString((long) GREATEST + 1, radix), radix)); assertNull("Radix: " + radix, Ints.tryParse(Long.toString((long) LEAST - 1, radix), radix)); } assertNull("Hex string and dec parm", Ints.tryParse("FFFF", 10)); assertEquals("Mixed hex case", 65535, (int) Ints.tryParse("ffFF", 16)); }
public void testTryParse() { tryParseAndAssertEquals(0, "0"); tryParseAndAssertEquals(0, "-0"); tryParseAndAssertEquals(1, "1"); tryParseAndAssertEquals(-1, "-1"); tryParseAndAssertEquals(8900, "8900"); tryParseAndAssertEquals(-8900, "-8900"); tryParseAndAssertEquals(GREATEST, Integer.toString(GREATEST)); tryParseAndAssertEquals(LEAST, Integer.toString(LEAST)); assertNull(Ints.tryParse("")); assertNull(Ints.tryParse("-")); assertNull(Ints.tryParse("+1")); assertNull(Ints.tryParse("9999999999999999")); assertNull("Max integer + 1", Ints.tryParse(Long.toString(((long) GREATEST) + 1))); assertNull("Max integer * 10", Ints.tryParse(Long.toString(((long) GREATEST) * 10))); assertNull("Min integer - 1", Ints.tryParse(Long.toString(((long) LEAST) - 1))); assertNull("Min integer * 10", Ints.tryParse(Long.toString(((long) LEAST) * 10))); assertNull("Max long", Ints.tryParse(Long.toString(Long.MAX_VALUE))); assertNull("Min long", Ints.tryParse(Long.toString(Long.MIN_VALUE))); assertNull(Ints.tryParse("\u0662\u06f3")); }
private void visitInnerClasses(BuilderEncodedValues.BuilderTypeEncodedValue value, ClassWriter classWriter) { // String name, String outerName, String innerName, int access String internalName = value.getValue(); String fullName = stripName(value.getValue()); String[] parts = fullName.split("\\$", 2); String outerName = parts[0]; String innerName = parts[1]; boolean isAnonymous = innerName.equals("1") || Ints.tryParse(innerName) != null; if (isAnonymous) { innerName = null; } int innerAccess = classManager.getVirtualClass(internalName).getClassDef().getAccessFlags(); classWriter.visitInnerClass(fullName, outerName, innerName, innerAccess); }
@AndroidIncompatible // No getJavaLangAccess in Android (at least not in the version we use). @GwtIncompatible // lazyStackTraceIsLazy() public void testLazyStackTraceWorksInProd() { // TODO(b/64442212): Remove this guard once lazyStackTrace() works in Java 9+. Integer javaVersion = Ints.tryParse(JAVA_SPECIFICATION_VERSION.value()); if (javaVersion != null && javaVersion >= 9) { return; } // Obviously this isn't guaranteed in every environment, but it works well enough for now: assertTrue(lazyStackTraceIsLazy()); }