Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date: Add DateFormatters class that uses java.time #31856

Merged
merged 7 commits into from
Jul 10, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.common.time;

import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.TemporalAccessor;
import java.util.Arrays;
import java.util.stream.Stream;

/**
* wrapper class around java.time.DateTimeFormatter that supports multiple formats for easier parsing
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be more clear the multiple formats has to do with parsing, not printing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated the comment

*/
public class DateFormatter {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be named more transparently, like CompoundDateTimeFormatter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed


private DateTimeFormatter printer;
private DateTimeFormatter[] parsers;

public DateFormatter(DateTimeFormatter printer, DateTimeFormatter ... parsers) {
this.printer = printer;
this.parsers = parsers;
}

public TemporalAccessor parse(String input) {
if (parsers.length > 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you check this in the ctor? If no parsers are passed, then set parsers to a single sized array of printer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

for (int i = 0; i < parsers.length; i++) {
try {
return parsers[i].parse(input);
} catch (DateTimeParseException e) {}
}
}

return printer.parse(input);
}

public DateFormatter withZone(ZoneId zoneId) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be implemented in the same way, but by calling the constructor with the resulting formatters? Then the formatter members can be final.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

formatters are now final

printer = printer.withZone(zoneId);
for (int i = 0; i < parsers.length; i++) {
parsers[i] = parsers[i].withZone(zoneId);
}

return this;
}

public String format(TemporalAccessor accessor) {
return printer.format(accessor);
}

// for internal use only
private DateTimeFormatter[] all() {
return Stream.concat(Arrays.stream(new DateTimeFormatter[] { printer }), Arrays.stream(parsers)).toArray(DateTimeFormatter[]::new);
}

public static DateFormatter merge(DateFormatter ... dateFormatters) {
DateTimeFormatter[] parsers = Arrays.stream(dateFormatters).map(DateFormatter::all)
.flatMap(Arrays::stream).toArray(DateTimeFormatter[]::new);
return new DateFormatter(dateFormatters[0].printer, parsers);
}
}
Loading