@@ -2447,34 +2447,7 @@ private DateTimeFormatter toFormatter(Locale locale, ResolverStyle resolverStyle
24472447 }
24482448
24492449 //-----------------------------------------------------------------------
2450- /**
2451- * Strategy for formatting/parsing date-time information.
2452- * <p>
2453- * The printer may format any part, or the whole, of the input date-time object.
2454- * Typically, a complete format is constructed from a number of smaller
2455- * units, each outputting a single field.
2456- * <p>
2457- * The parser may parse any piece of text from the input, storing the result
2458- * in the context. Typically, each individual parser will just parse one
2459- * field, such as the day-of-month, storing the value in the context.
2460- * Once the parse is complete, the caller will then resolve the parsed values
2461- * to create the desired object, such as a {@code LocalDate}.
2462- * <p>
2463- * The parse position will be updated during the parse. Parsing will start at
2464- * the specified index and the return value specifies the new parse position
2465- * for the next parser. If an error occurs, the returned index will be negative
2466- * and will have the error position encoded using the complement operator.
2467- *
2468- * @implSpec
2469- * This interface must be implemented with care to ensure other classes operate correctly.
2470- * All implementations that can be instantiated must be final, immutable and thread-safe.
2471- * <p>
2472- * The context is not a thread-safe object and a new instance will be created
2473- * for each format that occurs. The context must not be stored in an instance
2474- * variable or shared with any other threads.
2475- */
2476- interface DateTimePrinterParser {
2477-
2450+ interface DateTimePrinter {
24782451 /**
24792452 * Prints the date-time object to the buffer.
24802453 * <p>
@@ -2494,6 +2467,9 @@ interface DateTimePrinterParser {
24942467 * @throws DateTimeException if the date-time cannot be printed successfully
24952468 */
24962469 boolean format (DateTimePrintContext context , StringBuilder buf , boolean optional );
2470+ }
2471+
2472+ interface DateTimeParser {
24972473
24982474 /**
24992475 * Parses text into date-time information.
@@ -2512,13 +2488,44 @@ interface DateTimePrinterParser {
25122488 int parse (DateTimeParseContext context , CharSequence text , int position );
25132489 }
25142490
2491+ /**
2492+ * Strategy for formatting/parsing date-time information.
2493+ * <p>
2494+ * The printer may format any part, or the whole, of the input date-time object.
2495+ * Typically, a complete format is constructed from a number of smaller
2496+ * units, each outputting a single field.
2497+ * <p>
2498+ * The parser may parse any piece of text from the input, storing the result
2499+ * in the context. Typically, each individual parser will just parse one
2500+ * field, such as the day-of-month, storing the value in the context.
2501+ * Once the parse is complete, the caller will then resolve the parsed values
2502+ * to create the desired object, such as a {@code LocalDate}.
2503+ * <p>
2504+ * The parse position will be updated during the parse. Parsing will start at
2505+ * the specified index and the return value specifies the new parse position
2506+ * for the next parser. If an error occurs, the returned index will be negative
2507+ * and will have the error position encoded using the complement operator.
2508+ *
2509+ * @implSpec
2510+ * This interface must be implemented with care to ensure other classes operate correctly.
2511+ * All implementations that can be instantiated must be final, immutable and thread-safe.
2512+ * <p>
2513+ * The context is not a thread-safe object and a new instance will be created
2514+ * for each format that occurs. The context must not be stored in an instance
2515+ * variable or shared with any other threads.
2516+ */
2517+ interface DateTimePrinterParser extends DateTimePrinter , DateTimeParser {
2518+ }
2519+
25152520 //-----------------------------------------------------------------------
25162521 /**
25172522 * Composite printer and parser.
25182523 */
25192524 static final class CompositePrinterParser implements DateTimePrinterParser {
25202525 private final DateTimePrinterParser [] printerParsers ;
25212526 private final boolean optional ;
2527+ private final DateTimePrinter formatter ;
2528+ private final DateTimeParser parser ;
25222529
25232530 private CompositePrinterParser (List <DateTimePrinterParser > printerParsers , boolean optional ) {
25242531 this (printerParsers .toArray (new DateTimePrinterParser [0 ]), optional );
@@ -2527,6 +2534,8 @@ private CompositePrinterParser(List<DateTimePrinterParser> printerParsers, boole
25272534 private CompositePrinterParser (DateTimePrinterParser [] printerParsers , boolean optional ) {
25282535 this .printerParsers = printerParsers ;
25292536 this .optional = optional ;
2537+ this .formatter = DateTimePrinterParserFactory .createFormatter (printerParsers );
2538+ this .parser = DateTimePrinterParserFactory .createParser (printerParsers , optional );
25302539 }
25312540
25322541 /**
@@ -2546,38 +2555,16 @@ public CompositePrinterParser withOptional(boolean optional) {
25462555 public boolean format (DateTimePrintContext context , StringBuilder buf , boolean optional ) {
25472556 int length = buf .length ();
25482557 boolean effectiveOptional = optional | this .optional ;
2549- for (DateTimePrinterParser pp : printerParsers ) {
2550- if (!pp .format (context , buf , effectiveOptional )) {
2551- buf .setLength (length ); // reset buffer
2552- return true ;
2553- }
2558+ if (!formatter .format (context , buf , effectiveOptional )) {
2559+ buf .setLength (length ); // reset buffer
2560+ return true ;
25542561 }
25552562 return true ;
25562563 }
25572564
25582565 @ Override
25592566 public int parse (DateTimeParseContext context , CharSequence text , int position ) {
2560- if (optional ) {
2561- context .startOptional ();
2562- int pos = position ;
2563- for (DateTimePrinterParser pp : printerParsers ) {
2564- pos = pp .parse (context , text , pos );
2565- if (pos < 0 ) {
2566- context .endOptional (false );
2567- return position ; // return original position
2568- }
2569- }
2570- context .endOptional (true );
2571- return pos ;
2572- } else {
2573- for (DateTimePrinterParser pp : printerParsers ) {
2574- position = pp .parse (context , text , position );
2575- if (position < 0 ) {
2576- break ;
2577- }
2578- }
2579- return position ;
2580- }
2567+ return parser .parse (context , text , position );
25812568 }
25822569
25832570 @ Override
0 commit comments