T - generic type of chronological entitypublic final class MultiFormatParser<T extends ChronoEntity<T>> extends Object implements ChronoParser<T>
Serves for parsing of text input whose format is not yet known at compile time.
User who only need to parse different formats for one locale only might consider the simple alternative to concatenate all format pattern strings into one pattern with the "|"-symbol as separator.
| Modifier and Type | Method and Description |
|---|---|
static <T extends ChronoEntity<T>> |
of(ChronoFormatter<T>... formats)
Creates a new multiple format parser.
|
static <T extends ChronoEntity<T>> |
of(List<ChronoFormatter<T>> formats)
Creates a new multiple format parser.
|
T |
parse(CharSequence text)
Interpretes given text as chronological entity starting at the begin of text.
|
T |
parse(CharSequence text,
ParseLog status)
Interpretes given text as chronological entity starting
at the specified position in parse log.
|
T |
parse(CharSequence text,
ParseLog status,
AttributeQuery attributes)
Interpretes given text starting at the position defined in
parse-log.
|
@SafeVarargs public static <T extends ChronoEntity<T>> MultiFormatParser<T> of(ChronoFormatter<T>... formats)
Creates a new multiple format parser.
T - generic type of chronological entityformats - array of multiple formatspublic static <T extends ChronoEntity<T>> MultiFormatParser<T> of(List<ChronoFormatter<T>> formats)
Creates a new multiple format parser.
T - generic type of chronological entityformats - list of multiple formatspublic T parse(CharSequence text) throws ParseException
Interpretes given text as chronological entity starting at the begin of text.
text - text to be parsedIndexOutOfBoundsException - if the text is emptyParseException - if the text is not parseableparse(CharSequence, ParseLog)public T parse(CharSequence text, ParseLog status)
Interpretes given text as chronological entity starting at the specified position in parse log.
Following example demonstrates best coding practice if used in processing bulk data:
static final MultiFormatParser<PlainDate> MULTI_FORMAT_PARSER;
static {
ChronoFormatter<PlainDate> germanStyle =
ChronoFormatter.ofDatePattern("d. MMMM uuuu", PatternType.CLDR, Locale.GERMAN);
ChronoFormatter<PlainDate> frenchStyle =
ChronoFormatter.ofDatePattern("d. MMMM uuuu", PatternType.CLDR, Locale.FRENCH);
ChronoFormatter<PlainDate> usStyle =
ChronoFormatter.ofDatePattern("MM/dd/uuuu", PatternType.CLDR, Locale.US);
MULTI_FORMAT_PARSER = MultiFormatParser.of(germanStyle, frenchStyle, usStyle);
}
public Collection<PlainDate> parse(Collection<String> data) {
Collection<PlainDate> parsedDates = new ArrayList<>();
ParseLog plog = new ParseLog();
int index = 0;
for (String text : data) {
plog.reset(); // initialization
PlainDate date = MULTI_FORMAT_PARSER.parse(text, plog);
if ((date == null) || plog.isError()) {
// users are encouraged to use any good logging framework here
System.out.println("Wrong entry found: " + text + " at position " + index);
} else {
parsedDates.add(date);
}
index++;
}
return Collections.unmodifiableCollection(parsedDates);
}
text - text to be parsedstatus - parser information (always as new instance)null if parsing does not workIndexOutOfBoundsException - if the start position is at end of text or even behindpublic T parse(CharSequence text, ParseLog status, AttributeQuery attributes)
ChronoParserInterpretes given text starting at the position defined in parse-log.
Implementation note: Any implementation will parse the text first
at the position status.getPosition() and then set the new
position in the parse log if successful. In case of error the
error index in the parse log will be updated instead.
parse in interface ChronoParser<T extends ChronoEntity<T>>text - text to be parsedstatus - parser information (always as new instance)attributes - control attributesnull if parsing does not workCopyright © 2014–2016. All rights reserved.