3636import org .opensearch .core .common .Strings ;
3737
3838import java .text .ParsePosition ;
39+ import java .time .DateTimeException ;
3940import java .time .ZoneId ;
4041import java .time .format .DateTimeFormatter ;
4142import java .time .format .DateTimeFormatterBuilder ;
5253import java .util .Locale ;
5354import java .util .Map ;
5455import java .util .Objects ;
55- import java .util .concurrent .CopyOnWriteArrayList ;
5656import java .util .function .BiConsumer ;
5757import java .util .stream .Collectors ;
5858
@@ -70,11 +70,11 @@ class JavaDateFormatter implements DateFormatter {
7070
7171 private final String format ;
7272 private final String printFormat ;
73- private final DateTimeFormatter printer ;
74- private final List <DateTimeFormatter > parsers ;
73+ private final OpenSearchDateTimePrinter printer ;
74+ private final List <OpenSearchDateTimeFormatter > parsers ;
7575 private final JavaDateFormatter roundupParser ;
7676 private final Boolean canCacheLastParsedFormatter ;
77- private volatile DateTimeFormatter lastParsedformatter = null ;
77+ private volatile OpenSearchDateTimeFormatter lastParsedformatter = null ;
7878
7979 /**
8080 * A round up formatter
@@ -83,11 +83,11 @@ class JavaDateFormatter implements DateFormatter {
8383 */
8484 static class RoundUpFormatter extends JavaDateFormatter {
8585
86- RoundUpFormatter (String format , List <DateTimeFormatter > roundUpParsers ) {
86+ RoundUpFormatter (String format , List <OpenSearchDateTimeFormatter > roundUpParsers ) {
8787 super (format , firstFrom (roundUpParsers ), null , roundUpParsers );
8888 }
8989
90- private static DateTimeFormatter firstFrom (List <DateTimeFormatter > roundUpParsers ) {
90+ private static OpenSearchDateTimeFormatter firstFrom (List <OpenSearchDateTimeFormatter > roundUpParsers ) {
9191 return roundUpParsers .get (0 );
9292 }
9393
@@ -101,14 +101,18 @@ JavaDateFormatter getRoundupParser() {
101101 JavaDateFormatter (
102102 String format ,
103103 String printFormat ,
104- DateTimeFormatter printer ,
104+ OpenSearchDateTimePrinter printer ,
105105 Boolean canCacheLastParsedFormatter ,
106- DateTimeFormatter ... parsers
106+ OpenSearchDateTimeFormatter ... parsers
107107 ) {
108108 this (format , printFormat , printer , ROUND_UP_BASE_FIELDS , canCacheLastParsedFormatter , parsers );
109109 }
110110
111111 JavaDateFormatter (String format , DateTimeFormatter printer , DateTimeFormatter ... parsers ) {
112+ this (format , format , wrapFormatter (printer ), false , wrapAllFormatters (parsers ));
113+ }
114+
115+ JavaDateFormatter (String format , OpenSearchDateTimePrinter printer , OpenSearchDateTimeFormatter ... parsers ) {
112116 this (format , format , printer , false , parsers );
113117 }
114118
@@ -127,19 +131,19 @@ JavaDateFormatter getRoundupParser() {
127131 JavaDateFormatter (
128132 String format ,
129133 String printFormat ,
130- DateTimeFormatter printer ,
134+ OpenSearchDateTimePrinter printer ,
131135 BiConsumer <DateTimeFormatterBuilder , DateTimeFormatter > roundupParserConsumer ,
132136 Boolean canCacheLastParsedFormatter ,
133- DateTimeFormatter ... parsers
137+ OpenSearchDateTimeFormatter ... parsers
134138 ) {
135139 if (printer == null ) {
136140 throw new IllegalArgumentException ("printer may not be null" );
137141 }
138- long distinctZones = Arrays .stream (parsers ).map (DateTimeFormatter ::getZone ).distinct ().count ();
142+ long distinctZones = Arrays .stream (parsers ).map (OpenSearchDateTimeFormatter ::getZone ).distinct ().count ();
139143 if (distinctZones > 1 ) {
140144 throw new IllegalArgumentException ("formatters must have the same time zone" );
141145 }
142- long distinctLocales = Arrays .stream (parsers ).map (DateTimeFormatter ::getLocale ).distinct ().count ();
146+ long distinctLocales = Arrays .stream (parsers ).map (OpenSearchDateTimeFormatter ::getLocale ).distinct ().count ();
143147 if (distinctLocales > 1 ) {
144148 throw new IllegalArgumentException ("formatters must have the same locale" );
145149 }
@@ -149,12 +153,12 @@ JavaDateFormatter getRoundupParser() {
149153 this .canCacheLastParsedFormatter = canCacheLastParsedFormatter ;
150154
151155 if (parsers .length == 0 ) {
152- this .parsers = Collections .singletonList (printer );
156+ this .parsers = Collections .singletonList (( OpenSearchDateTimeFormatter ) printer );
153157 } else {
154158 this .parsers = Arrays .asList (parsers );
155159 }
156160 List <DateTimeFormatter > roundUp = createRoundUpParser (format , roundupParserConsumer );
157- this .roundupParser = new RoundUpFormatter (format , roundUp );
161+ this .roundupParser = new RoundUpFormatter (format , wrapAllFormatters ( roundUp ) );
158162 }
159163
160164 JavaDateFormatter (
@@ -163,7 +167,7 @@ JavaDateFormatter getRoundupParser() {
163167 BiConsumer <DateTimeFormatterBuilder , DateTimeFormatter > roundupParserConsumer ,
164168 DateTimeFormatter ... parsers
165169 ) {
166- this (format , format , printer , roundupParserConsumer , false , parsers );
170+ this (format , format , wrapFormatter ( printer ) , roundupParserConsumer , false , wrapAllFormatters ( parsers ) );
167171 }
168172
169173 /**
@@ -181,7 +185,8 @@ private List<DateTimeFormatter> createRoundUpParser(
181185 ) {
182186 if (format .contains ("||" ) == false ) {
183187 List <DateTimeFormatter > roundUpParsers = new ArrayList <>();
184- for (DateTimeFormatter parser : this .parsers ) {
188+ for (OpenSearchDateTimeFormatter customparser : this .parsers ) {
189+ DateTimeFormatter parser = customparser .getFormatter ();
185190 DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder ();
186191 builder .append (parser );
187192 roundupParserConsumer .accept (builder , parser );
@@ -201,12 +206,12 @@ public static DateFormatter combined(
201206 assert formatters .size () > 0 ;
202207 assert printFormatter != null ;
203208
204- List <DateTimeFormatter > parsers = new ArrayList <>(formatters .size ());
205- List <DateTimeFormatter > roundUpParsers = new ArrayList <>(formatters .size ());
209+ List <OpenSearchDateTimeFormatter > parsers = new ArrayList <>(formatters .size ());
210+ List <OpenSearchDateTimeFormatter > roundUpParsers = new ArrayList <>(formatters .size ());
206211
207212 assert printFormatter instanceof JavaDateFormatter ;
208213 JavaDateFormatter javaPrintFormatter = (JavaDateFormatter ) printFormatter ;
209- DateTimeFormatter printer = javaPrintFormatter .getPrinter ();
214+ OpenSearchDateTimePrinter printer = javaPrintFormatter .getPrinter ();
210215 for (DateFormatter formatter : formatters ) {
211216 assert formatter instanceof JavaDateFormatter ;
212217 JavaDateFormatter javaDateFormatter = (JavaDateFormatter ) formatter ;
@@ -227,9 +232,9 @@ public static DateFormatter combined(
227232 private JavaDateFormatter (
228233 String format ,
229234 String printFormat ,
230- DateTimeFormatter printer ,
231- List <DateTimeFormatter > roundUpParsers ,
232- List <DateTimeFormatter > parsers ,
235+ OpenSearchDateTimePrinter printer ,
236+ List <OpenSearchDateTimeFormatter > roundUpParsers ,
237+ List <OpenSearchDateTimeFormatter > parsers ,
233238 Boolean canCacheLastParsedFormatter
234239 ) {
235240 this .format = format ;
@@ -245,6 +250,15 @@ private JavaDateFormatter(
245250 DateTimeFormatter printer ,
246251 List <DateTimeFormatter > roundUpParsers ,
247252 List <DateTimeFormatter > parsers
253+ ) {
254+ this (format , format , wrapFormatter (printer ), wrapAllFormatters (roundUpParsers ), wrapAllFormatters (parsers ), false );
255+ }
256+
257+ private JavaDateFormatter (
258+ String format ,
259+ OpenSearchDateTimePrinter printer ,
260+ List <OpenSearchDateTimeFormatter > roundUpParsers ,
261+ List <OpenSearchDateTimeFormatter > parsers
248262 ) {
249263 this (format , format , printer , roundUpParsers , parsers , false );
250264 }
@@ -253,7 +267,7 @@ JavaDateFormatter getRoundupParser() {
253267 return roundupParser ;
254268 }
255269
256- DateTimeFormatter getPrinter () {
270+ OpenSearchDateTimePrinter getPrinter () {
257271 return printer ;
258272 }
259273
@@ -265,7 +279,7 @@ public TemporalAccessor parse(String input) {
265279
266280 try {
267281 return doParse (input );
268- } catch (DateTimeParseException e ) {
282+ } catch (DateTimeException e ) {
269283 throw new IllegalArgumentException ("failed to parse date field [" + input + "] with format [" + format + "]" , e );
270284 }
271285 }
@@ -289,14 +303,14 @@ private TemporalAccessor doParse(String input) {
289303 Object object = null ;
290304 if (canCacheLastParsedFormatter && lastParsedformatter != null ) {
291305 ParsePosition pos = new ParsePosition (0 );
292- object = lastParsedformatter .toFormat (). parseObject (input , pos );
306+ object = lastParsedformatter .parseObject (input , pos );
293307 if (parsingSucceeded (object , input , pos )) {
294308 return (TemporalAccessor ) object ;
295309 }
296310 }
297- for (DateTimeFormatter formatter : parsers ) {
311+ for (OpenSearchDateTimeFormatter formatter : parsers ) {
298312 ParsePosition pos = new ParsePosition (0 );
299- object = formatter .toFormat (). parseObject (input , pos );
313+ object = formatter .parseObject (input , pos );
300314 if (parsingSucceeded (object , input , pos )) {
301315 lastParsedformatter = formatter ;
302316 return (TemporalAccessor ) object ;
@@ -312,16 +326,28 @@ private boolean parsingSucceeded(Object object, String input, ParsePosition pos)
312326 return object != null && pos .getIndex () == input .length ();
313327 }
314328
329+ private static OpenSearchDateTimeFormatter wrapFormatter (DateTimeFormatter formatter ) {
330+ return new OpenSearchDateTimeFormatter (formatter );
331+ }
332+
333+ private static OpenSearchDateTimeFormatter [] wrapAllFormatters (DateTimeFormatter ... formatters ) {
334+ return Arrays .stream (formatters ).map (JavaDateFormatter ::wrapFormatter ).toArray (OpenSearchDateTimeFormatter []::new );
335+ }
336+
337+ private static List <OpenSearchDateTimeFormatter > wrapAllFormatters (List <DateTimeFormatter > formatters ) {
338+ return formatters .stream ().map (JavaDateFormatter ::wrapFormatter ).collect (Collectors .toList ());
339+ }
340+
315341 @ Override
316342 public DateFormatter withZone (ZoneId zoneId ) {
317343 // shortcurt to not create new objects unnecessarily
318344 if (zoneId .equals (zone ())) {
319345 return this ;
320346 }
321- List <DateTimeFormatter > parsers = new CopyOnWriteArrayList <>(
347+ List <OpenSearchDateTimeFormatter > parsers = new ArrayList <>(
322348 this .parsers .stream ().map (p -> p .withZone (zoneId )).collect (Collectors .toList ())
323349 );
324- List <DateTimeFormatter > roundUpParsers = this .roundupParser .getParsers ()
350+ List <OpenSearchDateTimeFormatter > roundUpParsers = this .roundupParser .getParsers ()
325351 .stream ()
326352 .map (p -> p .withZone (zoneId ))
327353 .collect (Collectors .toList ());
@@ -334,10 +360,10 @@ public DateFormatter withLocale(Locale locale) {
334360 if (locale .equals (locale ())) {
335361 return this ;
336362 }
337- List <DateTimeFormatter > parsers = new CopyOnWriteArrayList <>(
363+ List <OpenSearchDateTimeFormatter > parsers = new ArrayList <>(
338364 this .parsers .stream ().map (p -> p .withLocale (locale )).collect (Collectors .toList ())
339365 );
340- List <DateTimeFormatter > roundUpParsers = this .roundupParser .getParsers ()
366+ List <OpenSearchDateTimeFormatter > roundUpParsers = this .roundupParser .getParsers ()
341367 .stream ()
342368 .map (p -> p .withLocale (locale ))
343369 .collect (Collectors .toList ());
@@ -396,7 +422,7 @@ public String toString() {
396422 return String .format (Locale .ROOT , "format[%s] locale[%s]" , format , locale ());
397423 }
398424
399- Collection <DateTimeFormatter > getParsers () {
425+ Collection <OpenSearchDateTimeFormatter > getParsers () {
400426 return parsers ;
401427 }
402428}
0 commit comments