|
@@ -29,6 +29,7 @@ import java.time.format.DateTimeParseException;
|
|
|
import java.time.temporal.ChronoField;
|
|
|
import java.time.temporal.TemporalAccessor;
|
|
|
import java.time.temporal.TemporalField;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Collection;
|
|
|
import java.util.Collections;
|
|
@@ -57,19 +58,33 @@ class JavaDateFormatter implements DateFormatter {
|
|
|
private final String format;
|
|
|
private final DateTimeFormatter printer;
|
|
|
private final List<DateTimeFormatter> parsers;
|
|
|
- private final DateTimeFormatter roundupParser;
|
|
|
+ private final JavaDateFormatter roundupParser;
|
|
|
|
|
|
- private JavaDateFormatter(String format, DateTimeFormatter printer, DateTimeFormatter roundupParser, List<DateTimeFormatter> parsers) {
|
|
|
- this.format = format;
|
|
|
- this.printer = printer;
|
|
|
- this.roundupParser = roundupParser;
|
|
|
- this.parsers = parsers;
|
|
|
+ static class RoundUpFormatter extends JavaDateFormatter{
|
|
|
+
|
|
|
+ RoundUpFormatter(String format, List<DateTimeFormatter> roundUpParsers) {
|
|
|
+ super(format, firstFrom(roundUpParsers),null, roundUpParsers);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static DateTimeFormatter firstFrom(List<DateTimeFormatter> roundUpParsers) {
|
|
|
+ return roundUpParsers.get(0);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ JavaDateFormatter getRoundupParser() {
|
|
|
+ throw new UnsupportedOperationException("RoundUpFormatter does not have another roundUpFormatter");
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
+ // named formatters use default roundUpParser
|
|
|
JavaDateFormatter(String format, DateTimeFormatter printer, DateTimeFormatter... parsers) {
|
|
|
this(format, printer, builder -> ROUND_UP_BASE_FIELDS.forEach(builder::parseDefaulting), parsers);
|
|
|
}
|
|
|
|
|
|
- JavaDateFormatter(String format, DateTimeFormatter printer, Consumer<DateTimeFormatterBuilder> roundupParserConsumer,
|
|
|
+ // subclasses override roundUpParser
|
|
|
+ JavaDateFormatter(String format,
|
|
|
+ DateTimeFormatter printer,
|
|
|
+ Consumer<DateTimeFormatterBuilder> roundupParserConsumer,
|
|
|
DateTimeFormatter... parsers) {
|
|
|
if (printer == null) {
|
|
|
throw new IllegalArgumentException("printer may not be null");
|
|
@@ -90,20 +105,51 @@ class JavaDateFormatter implements DateFormatter {
|
|
|
} else {
|
|
|
this.parsers = Arrays.asList(parsers);
|
|
|
}
|
|
|
+ //this is when the RoundUp Formatter is created. In further merges (with ||) it will only append this one to a list.
|
|
|
+ List<DateTimeFormatter> roundUp = createRoundUpParser(format, roundupParserConsumer);
|
|
|
+ this.roundupParser = new RoundUpFormatter(format, roundUp) ;
|
|
|
+ }
|
|
|
|
|
|
- DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
|
|
|
+ private List<DateTimeFormatter> createRoundUpParser(String format,
|
|
|
+ Consumer<DateTimeFormatterBuilder> roundupParserConsumer) {
|
|
|
if (format.contains("||") == false) {
|
|
|
+ DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
|
|
|
builder.append(this.parsers.get(0));
|
|
|
+ roundupParserConsumer.accept(builder);
|
|
|
+ return Arrays.asList(builder.toFormatter(locale()));
|
|
|
}
|
|
|
- roundupParserConsumer.accept(builder);
|
|
|
- DateTimeFormatter roundupFormatter = builder.toFormatter(locale());
|
|
|
- if (printer.getZone() != null) {
|
|
|
- roundupFormatter = roundupFormatter.withZone(zone());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static DateFormatter combined(String input, List<DateFormatter> formatters) {
|
|
|
+ assert formatters.size() > 0;
|
|
|
+
|
|
|
+ List<DateTimeFormatter> parsers = new ArrayList<>(formatters.size());
|
|
|
+ List<DateTimeFormatter> roundUpParsers = new ArrayList<>(formatters.size());
|
|
|
+
|
|
|
+ DateTimeFormatter printer = null;
|
|
|
+ for (DateFormatter formatter : formatters) {
|
|
|
+ assert formatter instanceof JavaDateFormatter;
|
|
|
+ JavaDateFormatter javaDateFormatter = (JavaDateFormatter) formatter;
|
|
|
+ if (printer == null) {
|
|
|
+ printer = javaDateFormatter.getPrinter();
|
|
|
+ }
|
|
|
+ parsers.addAll(javaDateFormatter.getParsers());
|
|
|
+ roundUpParsers.addAll(javaDateFormatter.getRoundupParser().getParsers());
|
|
|
}
|
|
|
- this.roundupParser = roundupFormatter;
|
|
|
+
|
|
|
+ return new JavaDateFormatter(input, printer, roundUpParsers, parsers);
|
|
|
+ }
|
|
|
+
|
|
|
+ private JavaDateFormatter(String format, DateTimeFormatter printer, List<DateTimeFormatter> roundUpParsers,
|
|
|
+ List<DateTimeFormatter> parsers) {
|
|
|
+ this.format = format;
|
|
|
+ this.printer = printer;
|
|
|
+ this.roundupParser = roundUpParsers != null ? new RoundUpFormatter(format, roundUpParsers ) : null;
|
|
|
+ this.parsers = parsers;
|
|
|
}
|
|
|
|
|
|
- DateTimeFormatter getRoundupParser() {
|
|
|
+ JavaDateFormatter getRoundupParser() {
|
|
|
return roundupParser;
|
|
|
}
|
|
|
|
|
@@ -162,8 +208,12 @@ class JavaDateFormatter implements DateFormatter {
|
|
|
if (zoneId.equals(zone())) {
|
|
|
return this;
|
|
|
}
|
|
|
- return new JavaDateFormatter(format, printer.withZone(zoneId), getRoundupParser().withZone(zoneId),
|
|
|
- parsers.stream().map(p -> p.withZone(zoneId)).collect(Collectors.toList()));
|
|
|
+ List<DateTimeFormatter> parsers = this.parsers.stream().map(p -> p.withZone(zoneId)).collect(Collectors.toList());
|
|
|
+ List<DateTimeFormatter> roundUpParsers = this.roundupParser.getParsers()
|
|
|
+ .stream()
|
|
|
+ .map(p -> p.withZone(zoneId))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ return new JavaDateFormatter(format, printer.withZone(zoneId), roundUpParsers, parsers);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -172,8 +222,12 @@ class JavaDateFormatter implements DateFormatter {
|
|
|
if (locale.equals(locale())) {
|
|
|
return this;
|
|
|
}
|
|
|
- return new JavaDateFormatter(format, printer.withLocale(locale), getRoundupParser().withLocale(locale),
|
|
|
- parsers.stream().map(p -> p.withLocale(locale)).collect(Collectors.toList()));
|
|
|
+ List<DateTimeFormatter> parsers = this.parsers.stream().map(p -> p.withLocale(locale)).collect(Collectors.toList());
|
|
|
+ List<DateTimeFormatter> roundUpParsers = this.roundupParser.getParsers()
|
|
|
+ .stream()
|
|
|
+ .map(p -> p.withLocale(locale))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ return new JavaDateFormatter(format, printer.withLocale(locale), roundUpParsers, parsers);
|
|
|
}
|
|
|
|
|
|
@Override
|