// Returns true if a new template is created as opposed to reusing the existing template. private boolean maybeCreateNewTemplate() { // When there are multiple available formats, the formatter uses the first format where a // formatting template could be created. Iterator <NumberFormat> it = possibleFormats.iterator(); while (it.hasNext()) { NumberFormat numberFormat = it.next(); String pattern = numberFormat.getPattern(); if (currentFormattingPattern.equals(pattern)) { return(false); } if (createFormattingTemplate(numberFormat)) { currentFormattingPattern = pattern; shouldAddSpaceAfterNationalPrefix = NATIONAL_PREFIX_SEPARATORS_PATTERN.matcher( numberFormat.getNationalPrefixFormattingRule()).find(); // With a new formatting template, the matched position using the old template needs to be // reset. lastMatchPosition = 0; return(true); } else // Remove the current number format from possibleFormats. { it.remove(); } } ableToFormat = false; return(false); }
private boolean createFormattingTemplate(NumberFormat format) { String numberPattern = format.getPattern(); // The formatter doesn't format numbers when numberPattern contains "|", e.g. // (20|3)\d{4}. In those cases we quickly return. if (numberPattern.indexOf('|') != -1) { return(false); } // Replace anything in the form of [..] with \d numberPattern = CHARACTER_CLASS_PATTERN.matcher(numberPattern).replaceAll("\\\\d"); // Replace any standalone digit (not the one in d{}) with \d numberPattern = STANDALONE_DIGIT_PATTERN.matcher(numberPattern).replaceAll("\\\\d"); formattingTemplate.setLength(0); String tempTemplate = getFormattingTemplate(numberPattern, format.getFormat()); if (tempTemplate.length() > 0) { formattingTemplate.append(tempTemplate); return(true); } return(false); }
private boolean createFormattingTemplate(NumberFormat format) { String numberPattern = format.getPattern(); // The formatter doesn't format numbers when numberPattern contains "|", e.g. // (20|3)\d{4}. In those cases we quickly return. if (numberPattern.indexOf('|') != -1) { return false; } // Replace anything in the form of [..] with \d numberPattern = CHARACTER_CLASS_PATTERN.matcher(numberPattern).replaceAll("\\\\d"); // Replace any standalone digit (not the one in d{}) with \d numberPattern = STANDALONE_DIGIT_PATTERN.matcher(numberPattern).replaceAll("\\\\d"); formattingTemplate.setLength(0); String tempTemplate = getFormattingTemplate(numberPattern, format.getFormat()); if (tempTemplate.length() > 0) { formattingTemplate.append(tempTemplate); return true; } return false; }