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

Optimize saving #7568

Merged
merged 7 commits into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We fixed an issue where Content selector does not seem to work for custom fields. [#6819](https://github.com/JabRef/jabref/issues/6819)
- We fixed an issue in which a linked online file consisting of a web page was saved as an invalid pdf file upon being downloaded. The user is now notified when downloading a linked file results in an HTML file. [#7452](https://github.com/JabRef/jabref/issues/7452)
- We fixed an issue where opening BibTex file (doubleclick) from Folder with spaces not working. [#6487](https://github.com/JabRef/jabref/issues/6487)
- We fixed an issue with saving large `.bib` files [#7265](https://github.com/JabRef/jabref/issues/7265)

### Removed

Expand Down
71 changes: 36 additions & 35 deletions src/main/java/org/jabref/model/entry/Date.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.time.Year;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAccessor;
Expand All @@ -15,6 +16,31 @@

public class Date {

private static final DateTimeFormatter NORMALIZED_DATE_FORMATTER = DateTimeFormatter.ofPattern("uuuu[-MM][-dd]");
private static final DateTimeFormatter SIMPLE_DATE_FORMATS;
static {
List<String> formatStrings = Arrays.asList(
"uuuu-M-d", // covers 2009-1-15
"uuuu-M", // covers 2009-11
"d-M-uuuu", // covers 15-1-2012
"M-uuuu", // covers 1-2012
"M/uuuu", // covers 9/2015 and 09/2015
"M/uu", // covers 9/15
"MMMM d, uuuu", // covers September 1, 2015
"MMMM, uuuu", // covers September, 2015
"d.M.uuuu", // covers 15.1.2015
"uuuu.M.d", // covers 2015.1.15
"uuuu", // covers 2015
"MMM, uuuu"); // covers Jan, 2020

SIMPLE_DATE_FORMATS = formatStrings.stream()
.map(DateTimeFormatter::ofPattern)
.reduce(new DateTimeFormatterBuilder(),
DateTimeFormatterBuilder::appendOptional,
(builder, formatterBuilder) -> builder.append(formatterBuilder.toFormatter()))
.toFormatter(Locale.US);
}

private final TemporalAccessor date;

public Date(int year, int month, int dayOfMonth) {
Expand All @@ -33,43 +59,19 @@ public Date(TemporalAccessor date) {
this.date = date;
}

/**
* Try to parse the following formats
* - "M/y" (covers 9/15, 9/2015, and 09/2015)
* - "MMMM (dd), yyyy" (covers September 1, 2015 and September, 2015)
* - "yyyy-MM-dd" (covers 2009-1-15)
* - "dd-MM-yyyy" (covers 15-1-2009)
* - "d.M.uuuu" (covers 15.1.2015)
* - "uuuu.M.d" (covers 2015.1.15)
* - "MMM, uuuu" (covers Jan, 2020)
* The code is essentially taken from http://stackoverflow.com/questions/4024544/how-to-parse-dates-in-multiple-formats-using-simpledateformat.
*/
public static Optional<Date> parse(String dateString) {
Objects.requireNonNull(dateString);
List<String> formatStrings = Arrays.asList(
"uuuu-M-d",
"uuuu-M",
"d-M-uuuu",
"M-uuuu",
"M/uu",
"M/uuuu",
"MMMM d, uuuu",
"MMMM, uuuu",
"d.M.uuuu",
"uuuu.M.d", "uuuu",
"MMM, uuuu");

for (String formatString : formatStrings) {
try {
// Locale is required for parsing month names correctly. Currently this expects the month names to be in English
TemporalAccessor parsedDate = DateTimeFormatter.ofPattern(formatString).withLocale(Locale.US).parse(dateString);
return Optional.of(new Date(parsedDate));
} catch (DateTimeParseException ignored) {
// Ignored
}

if (dateString.isEmpty()) {
return Optional.empty();
}

return Optional.empty();
try {
TemporalAccessor parsedDate = SIMPLE_DATE_FORMATS.parse(dateString);
return Optional.of(new Date(parsedDate));
} catch (DateTimeParseException ignored) {
return Optional.empty();
}
}

public static Optional<Date> parse(Optional<String> yearValue, Optional<String> monthValue,
Expand Down Expand Up @@ -105,8 +107,7 @@ private static Optional<Integer> convertToInt(String value) {
}

public String getNormalized() {
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("uuuu[-MM][-dd]");
return dateFormatter.format(date);
return NORMALIZED_DATE_FORMATTER.format(date);
}

public Optional<Integer> getYear() {
Expand Down