Skip to content

Commit 4f213f7

Browse files
Ali96kzcalixtuskoppor
authored
Optimize saving (#7568)
Co-authored-by: Carl Christian Snethlage <[email protected]> Co-authored-by: Oliver Kopp <[email protected]>
1 parent 8591d84 commit 4f213f7

File tree

2 files changed

+37
-35
lines changed

2 files changed

+37
-35
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
7373
- We fixed an issue where font size of the preferences dialog does not update with the rest of the GUI. [#7416](https://github.com/JabRef/jabref/issues/7416)
7474
- 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)
7575
- We fixed an issue where opening BibTex file (doubleclick) from Folder with spaces not working. [#6487](https://github.com/JabRef/jabref/issues/6487)
76+
- We fixed an issue with saving large `.bib` files [#7265](https://github.com/JabRef/jabref/issues/7265)
7677

7778
### Removed
7879

src/main/java/org/jabref/model/entry/Date.java

+36-35
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.time.Year;
55
import java.time.YearMonth;
66
import java.time.format.DateTimeFormatter;
7+
import java.time.format.DateTimeFormatterBuilder;
78
import java.time.format.DateTimeParseException;
89
import java.time.temporal.ChronoField;
910
import java.time.temporal.TemporalAccessor;
@@ -15,6 +16,31 @@
1516

1617
public class Date {
1718

19+
private static final DateTimeFormatter NORMALIZED_DATE_FORMATTER = DateTimeFormatter.ofPattern("uuuu[-MM][-dd]");
20+
private static final DateTimeFormatter SIMPLE_DATE_FORMATS;
21+
static {
22+
List<String> formatStrings = Arrays.asList(
23+
"uuuu-M-d", // covers 2009-1-15
24+
"uuuu-M", // covers 2009-11
25+
"d-M-uuuu", // covers 15-1-2012
26+
"M-uuuu", // covers 1-2012
27+
"M/uuuu", // covers 9/2015 and 09/2015
28+
"M/uu", // covers 9/15
29+
"MMMM d, uuuu", // covers September 1, 2015
30+
"MMMM, uuuu", // covers September, 2015
31+
"d.M.uuuu", // covers 15.1.2015
32+
"uuuu.M.d", // covers 2015.1.15
33+
"uuuu", // covers 2015
34+
"MMM, uuuu"); // covers Jan, 2020
35+
36+
SIMPLE_DATE_FORMATS = formatStrings.stream()
37+
.map(DateTimeFormatter::ofPattern)
38+
.reduce(new DateTimeFormatterBuilder(),
39+
DateTimeFormatterBuilder::appendOptional,
40+
(builder, formatterBuilder) -> builder.append(formatterBuilder.toFormatter()))
41+
.toFormatter(Locale.US);
42+
}
43+
1844
private final TemporalAccessor date;
1945

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

36-
/**
37-
* Try to parse the following formats
38-
* - "M/y" (covers 9/15, 9/2015, and 09/2015)
39-
* - "MMMM (dd), yyyy" (covers September 1, 2015 and September, 2015)
40-
* - "yyyy-MM-dd" (covers 2009-1-15)
41-
* - "dd-MM-yyyy" (covers 15-1-2009)
42-
* - "d.M.uuuu" (covers 15.1.2015)
43-
* - "uuuu.M.d" (covers 2015.1.15)
44-
* - "MMM, uuuu" (covers Jan, 2020)
45-
* The code is essentially taken from http://stackoverflow.com/questions/4024544/how-to-parse-dates-in-multiple-formats-using-simpledateformat.
46-
*/
4762
public static Optional<Date> parse(String dateString) {
4863
Objects.requireNonNull(dateString);
49-
List<String> formatStrings = Arrays.asList(
50-
"uuuu-M-d",
51-
"uuuu-M",
52-
"d-M-uuuu",
53-
"M-uuuu",
54-
"M/uu",
55-
"M/uuuu",
56-
"MMMM d, uuuu",
57-
"MMMM, uuuu",
58-
"d.M.uuuu",
59-
"uuuu.M.d", "uuuu",
60-
"MMM, uuuu");
61-
62-
for (String formatString : formatStrings) {
63-
try {
64-
// Locale is required for parsing month names correctly. Currently this expects the month names to be in English
65-
TemporalAccessor parsedDate = DateTimeFormatter.ofPattern(formatString).withLocale(Locale.US).parse(dateString);
66-
return Optional.of(new Date(parsedDate));
67-
} catch (DateTimeParseException ignored) {
68-
// Ignored
69-
}
64+
65+
if (dateString.isEmpty()) {
66+
return Optional.empty();
7067
}
7168

72-
return Optional.empty();
69+
try {
70+
TemporalAccessor parsedDate = SIMPLE_DATE_FORMATS.parse(dateString);
71+
return Optional.of(new Date(parsedDate));
72+
} catch (DateTimeParseException ignored) {
73+
return Optional.empty();
74+
}
7375
}
7476

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

107109
public String getNormalized() {
108-
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("uuuu[-MM][-dd]");
109-
return dateFormatter.format(date);
110+
return NORMALIZED_DATE_FORMATTER.format(date);
110111
}
111112

112113
public Optional<Integer> getYear() {

0 commit comments

Comments
 (0)