ParseUtil { public static int parseLastInt(String s, int i) { try { String ls = parseLastString(s); if (ls.toLowerCase().startsWith("0x")) { return Integer.decode(ls); } else { return Integer.parseInt(ls); } } catch (NumberFormatException e) { LOG.trace(DEFAULT_LOG_MSG, s, e); return i; } } private  ParseUtil(); static long parseHertz(String hertz); static int parseLastInt(String s, int i); static long parseLastLong(String s, long li); static double parseLastDouble(String s, double d); static String parseLastString(String s); static String byteArrayToHexString(byte[] bytes); static byte[] hexStringToByteArray(String digits); static byte[] asciiStringToByteArray(String text, int length); static byte[] longToByteArray(long value, int valueSize, int length); static long strToLong(String str, int size); static long byteArrayToLong(byte[] bytes, int size); static float byteArrayToFloat(byte[] bytes, int size, int fpBits); static long unsignedIntToLong(int unsignedValue); static long unsignedLongToSignedLong(long unsignedValue); static String hexStringToString(String hexString); static int parseIntOrDefault(String s, int defaultInt); static long parseLongOrDefault(String s, long defaultLong); static long parseUnsignedLongOrDefault(String s, long defaultLong); static double parseDoubleOrDefault(String s, double defaultDouble); static long parseDHMSOrDefault(String s, long defaultLong); static String parseUuidOrDefault(String s, String defaultStr); static String getSingleQuoteStringValue(String line); static String getDoubleQuoteStringValue(String line); static String getStringBetween(String line, char c); static int getFirstIntValue(String line); static int getNthIntValue(String line, int n); static String removeMatchingString(final String original, final String toRemove); static long[] parseStringToLongArray(String s, int[] indices, int length, char delimiter); static int countStringToLongArray(String s, char delimiter); static String getTextBetweenStrings(String text, String before, String after); static long filetimeToUtcMs(long filetime, boolean local); static String parseMmDdYyyyToYyyyMmDD(String dateString); static OffsetDateTime parseCimDateTimeToOffset(String cimDateTime); static boolean filePathStartsWith(List<String> prefixList, String path); static long parseDecimalMemorySizeToBinary(String size); static Pair<String, String> parsePnPDeviceIdToVendorProductId(String pnpDeviceId); static long parseLshwResourceString(String resources); static Pair<String, String> parseLspciMachineReadable(String line); static long parseLspciMemorySize(String line); static List<Integer> parseHyphenatedIntList(String str); static String parseUtAddrV6toIP(int[] utAddrV6); static long hexStringToLong(String hexString, long defaultValue); static String removeLeadingDots(String dotPrefixedStr); static final Pattern whitespacesColonWhitespace; static final Pattern whitespaces; static final Pattern notDigits; static final Pattern startWithNotDigits; static final Pattern slash; }
@Test public void testParseLastInt() { assertEquals(-1, ParseUtil.parseLastInt("foo : bar", -1)); assertEquals(1, ParseUtil.parseLastInt("foo : 1", 0)); assertEquals(2, ParseUtil.parseLastInt("foo", 2)); assertEquals(3, ParseUtil.parseLastInt("max_int plus one is 2147483648", 3)); assertEquals(255, ParseUtil.parseLastInt("0xff", 4)); assertEquals(-1L, ParseUtil.parseLastLong("foo : bar", -1L)); assertEquals(1L, ParseUtil.parseLastLong("foo : 1", 0L)); assertEquals(2L, ParseUtil.parseLastLong("foo", 2L)); assertEquals(2147483648L, ParseUtil.parseLastLong("max_int plus one is 2147483648", 3L)); assertEquals(255L, ParseUtil.parseLastLong("0xff", 0L)); double epsilon = 1.1102230246251565E-16; assertEquals(-1d, ParseUtil.parseLastDouble("foo : bar", -1d), epsilon); assertEquals(1.0, ParseUtil.parseLastDouble("foo : 1.0", 0d), epsilon); assertEquals(2d, ParseUtil.parseLastDouble("foo", 2d), epsilon); }