List of usage examples for com.google.common.primitives Longs tryParse
@Beta @Nullable @CheckForNull public static Long tryParse(String string)
From source file:com.cinchapi.common.base.AnyStrings.java
/** * This method efficiently tries to parse {@code value} into a * {@link Number} object if possible. If the string is not a number, then * the method returns {@code null} as quickly as possible. * //from w ww .j a v a 2 s. c om * @param value * @return a Number object that represents the string or {@code null} if it * is not possible to parse the string into a number */ @Nullable public static Number tryParseNumber(String value) { int size = value.length(); if (value == null || size == 0) { return null; } else if (value.charAt(0) == '0' && size > 1 && value.charAt(1) != '.') { // Do not parse a string as a number if it has a leading 0 that is // not followed by a decimal (i.e. 007) return null; } boolean decimal = false; boolean scientific = false; for (int i = 0; i < size; ++i) { char c = value.charAt(i); if (!Character.isDigit(c)) { if (i == 0 && c == '-' || (scientific && (c == '-' || c == '+'))) { continue; } else if (c == '.') { if (!decimal && size > 1) { decimal = true; } else { // Since we've already seen a decimal, the appearance of // another one suggests this is an IP address instead of // a number return null; } } else if (i == size - 1 && c == 'D' && size > 1) { // Respect the convention to coerce numeric strings to // Double objects by appending a single 'D' character. return Double.valueOf(value.substring(0, i)); } else if ((c == 'E' || c == 'e') && i < size - 1) { // CO-627: Account for valid representations of scientific // notation if (!scientific) { scientific = true; } else { // Since we've already seen a scientific notation // indicator, another one suggests that this is not // really a number return null; } } else { return null; } } } try { if (decimal || scientific) { // Try to return a float (for space compactness) if it is // possible to fit the entire decimal without any loss of // precision. In order to do this, we have to compare the string // output of both the parsed double and the parsed float. This // is kind of inefficient, so substitute for a better way if it // exists. double d = Doubles.tryParse(value); float f = Floats.tryParse(value); if (String.valueOf(d).equals(String.valueOf(f))) { return f; } else { return d; } } else if (value.equals("-")) { // CON-597 return null; } else { return MoreObjects.firstNonNull(Ints.tryParse(value), Longs.tryParse(value)); } } catch (NullPointerException e) { throw new NumberFormatException( format("{} appears to be a number but cannot be parsed as such", value)); } }
From source file:com.google.enterprise.adaptor.opentext.OpentextAdaptor.java
@Override public void getModifiedDocIds(DocIdPusher pusher) throws IOException, InterruptedException { // Log in using CWS and use that token for the search. Authentication authentication = this.soapFactory.newAuthentication(); String authenticationToken = getAuthenticationToken(this.username, this.password); ArrayList<DocId> docIds = new ArrayList<>(); int resultCount = 0; do {// ww w. jav a2s.co m String query = getLastModifiedQuery(); log.log(Level.FINER, "getModifiedDocIds query: {0}", query); Element documentElement = getXmlSearchResults(authenticationToken, query); resultCount = getXmlSearchCount(documentElement); log.log(Level.FINER, "getModifiedDocIds result count: " + resultCount); if (resultCount > 0) { NodeList searchResults = documentElement.getElementsByTagName("SearchResult"); for (int i = 0; i < searchResults.getLength(); i++) { String docId = getXmlSearchDocId((Element) searchResults.item(i)); if (docId != null) { docIds.add(new DocId(docId)); } } // Cache the last object's id/date/time if (docIds.size() > 0) { Long nodeId = Longs.tryParse(docIds.get(docIds.size() - 1).getUniqueId().split(":")[1]); DocumentManagement documentManagement = this.soapFactory .newDocumentManagement(authenticationToken); Node node = getNodeById(documentManagement, nodeId); if (node != null) { XMLGregorianCalendar xmlCalendar = node.getModifyDate(); if (xmlCalendar != null) { Date date = xmlCalendar.toGregorianCalendar().getTime(); this.lastModDocId = nodeId; this.lastModDate = new SimpleDateFormat("yyyyMMdd").format(date); this.lastModTime = new SimpleDateFormat("HHmmss").format(date); } } } } } while (resultCount > 0); log.log(Level.FINER, "Sending modified doc ids: {0}", docIds); pusher.pushDocIds(docIds); }
From source file:com.attribyte.relay.wp.WPSupplier.java
/** * Gets the id of the featured image from post metadata. * @param meta The post metadata.//from w w w . j a v a 2 s . c o m * @return The id of the featured image or {@code 0} if none. */ private static long featuredImageId(final List<Meta> meta) { for (Meta m : meta) { if (m.key.equals("_thumbnail_id")) { Long id = Longs.tryParse(m.value); if (id != null) { return id; } } } return 0; }
From source file:io.prestosql.plugin.hive.metastore.thrift.ThriftMetastoreUtil.java
private static OptionalLong parse(@Nullable String parameterValue) { if (parameterValue == null) { return OptionalLong.empty(); }//w w w . j av a 2s .c o m Long longValue = Longs.tryParse(parameterValue); if (longValue == null || longValue < 0) { return OptionalLong.empty(); } return OptionalLong.of(longValue); }
From source file:com.google.enterprise.adaptor.opentext.OpentextAdaptor.java
@VisibleForTesting List<String> getXmlSearchIds(Element resultElement) { List<String> ids = new ArrayList<>(Splitter.on(" ").omitEmptyStrings().trimResults() .splitToList(getTextContent(resultElement, "OTLocation"))); if (ids.size() == 0) { return ids; }//from w w w . ja va 2 s . com ListIterator<String> iterator = ids.listIterator(); while (iterator.hasNext()) { String id = iterator.next(); Long idn = Longs.tryParse(id); if (idn == null) { log.log(Level.FINER, "Invalid id '{0}' in id list {1}; skipping", new Object[] { id, ids }); return Collections.emptyList(); } else if (idn < 0) { iterator.remove(); // Remove volume ids, e.g. Projects. } } return ids; }