/** * Creates a new instance. See the factory methods in {@link PhoneNumberUtil} on how to obtain a * new instance. * * @param util the phone number util to use * @param text the character sequence that we will search, null for no text * @param country the country to assume for phone numbers not written in international format * (with a leading plus, or with the international dialing prefix of the * specified region). May be null or "ZZ" if only numbers with a * leading plus should be considered. * @param leniency the leniency to use when evaluating candidate phone numbers * @param maxTries the maximum number of invalid numbers to try before giving up on the text. * This is to cover degenerate cases where the text has a lot of false positives * in it. Must be {@code >= 0}. */ internal PhoneNumberMatcher(PhoneNumberUtil util, CharSequence text, String country, Leniency leniency, long maxTries) { if ((util == null) || (leniency == null)) { throw new NullPointerException(); } if (maxTries < 0) { throw new IllegalArgumentException(); } this.phoneUtil = util; this.text = (text != null) ? text : (String)""; this.preferredRegion = country; this.leniency = leniency; this.maxTries = maxTries; }
private void doTestNumberNonMatchesForLeniency(List<NumberTest> testCases, Leniency leniency) { int matchFoundCount = 0; foreach(NumberTest test in testCases) { Iterator<PhoneNumberMatch> iterator = findNumbersForLeniency(test.rawString, test.region, leniency); PhoneNumberMatch match = iterator.hasNext() ? iterator.next() : null; if (match != null) { matchFoundCount++; System.Diagnostics.Debug.WriteLine("Match found in " + test.toString() + " for leniency: " + leniency); } } assertEquals(0, matchFoundCount); }
private void doTestNumberMatchesForLeniency(List<NumberTest> testCases, Leniency leniency) { int noMatchFoundCount = 0; int wrongMatchFoundCount = 0; foreach(NumberTest test in testCases) { Iterator<PhoneNumberMatch> iterator = findNumbersForLeniency(test.rawString, test.region, leniency); PhoneNumberMatch match = iterator.hasNext() ? iterator.next() : null; if (match == null) { noMatchFoundCount++; System.Diagnostics.Debug.WriteLine("No match found in " + test.toString() + " for leniency: " + leniency); } else { if (!test.rawString.equals(match.rawString())) { wrongMatchFoundCount++; System.Diagnostics.Debug.WriteLine("Found wrong match in test " + test.toString() + ". Found " + match.rawString()); } } } assertEquals(0, noMatchFoundCount); assertEquals(0, wrongMatchFoundCount); }
private void doTestInContext(String number, String defaultCountry, List<NumberContext> contextPairs, Leniency leniency) { foreach(NumberContext context in contextPairs) { String prefix = context.leadingText; String text = prefix + number + context.trailingText; int start = prefix.length(); int end = start + number.length(); Iterator<PhoneNumberMatch> iterator = phoneUtil.findNumbers(text, defaultCountry, leniency, Long.MAX_VALUE).iterator(); PhoneNumberMatch match = iterator.hasNext() ? iterator.next() : null; assertNotNull("Did not find a number in '" + text + "'; expected '" + number + "'", match); CharSequence extracted = text.subSequence(match.start(), match.end()); assertTrue("Unexpected phone region in '" + text + "'; extracted '" + extracted + "'", start == match.start() && end == match.end()); assertTrue(number.contentEquals(extracted)); assertTrue(match.rawString().contentEquals(extracted)); ensureTermination(text, defaultCountry, leniency); } }
private Iterator<PhoneNumberMatch> findNumbersForLeniency( String text, String defaultCountry, Leniency leniency) { return phoneUtil.findNumbers(text, defaultCountry, leniency, Long.MAX_VALUE).iterator(); }
/** * Exhaustively searches for phone numbers from each index within {@code text} to test that * finding matches always terminates. */ private void ensureTermination(String text, String defaultCountry, Leniency leniency) { for (int index = 0; index <= text.length(); index++) { String sub = text.substring(index); StringBuilder matches = new StringBuilder(); // Iterates over all matches. foreach(PhoneNumberMatch match in phoneUtil.findNumbers(sub, defaultCountry, leniency, Long.MAX_VALUE)) { matches.append(", ").append(match.toString()); } } }