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

#6057 Improve startup time #7486

Merged
merged 11 commits into from
Mar 10, 2021
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We changed the title of the window "Manage field names and content": to have the same title as the corresponding menu item [#6895](https://github.com/JabRef/jabref/pull/6895)
- We improved the detection of "short" DOIs [6880](https://github.com/JabRef/jabref/issues/6880)
- We improved the duplicate detection when identifiers like DOI or arxiv are semantiaclly the same, but just syntactically differ (e.g. with or without http(s):// prefix). [#6707](https://github.com/JabRef/jabref/issues/6707)
- We improved JabRef start up time [6057](https://github.com/JabRef/jabref/issues/6057)
- We changed in the group interface "Generate groups from keywords in a BibTeX field" by "Generate groups from keywords in the following field". [#6983](https://github.com/JabRef/jabref/issues/6983)
- We changed the name of a group type from "Searching for keywords" to "Searching for a keyword". [6995](https://github.com/JabRef/jabref/pull/6995)
- We changed the way JabRef displays the title of a tab and of the window. [4161](https://github.com/JabRef/jabref/issues/4161)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,48 +13,45 @@ public int compare(String val1, String val2) {
// We start by implementing the comparison in the edge cases (if one of the values is null).
if (val1 == null && val2 == null) {
return 0;
}

if (val1 == null) {
} else if (val1 == null) {
// We assume that "null" is "less than" any other value.
return -1;
} else if (val2 == null) {
return 1;
}

if (val2 == null) {
boolean isVal1Valid = isNumber(val1);
boolean isVal2Valid = isNumber(val2);
if (!isVal1Valid && !isVal2Valid) {
return val1.compareTo(val2);
} else if (!isVal1Valid) {
return -1;
} else if (!isVal2Valid) {
return 1;
}

// Now we start the conversion to integers.
Integer valInt1 = null;
Integer valInt2 = null;
try {
// Trim in case the user added an unnecessary white space (e.g. 1 1 instead of 11).
valInt1 = Integer.parseInt(val1.trim());
} catch (NumberFormatException ignore) {
// do nothing
}
try {
valInt2 = Integer.parseInt(val2.trim());
} catch (NumberFormatException ignore) {
// do nothing
}
Integer valInt1 = Integer.parseInt(val1.trim());
tobiasdiez marked this conversation as resolved.
Show resolved Hide resolved
Integer valInt2 = Integer.parseInt(val2.trim());

if (valInt1 == null && valInt2 == null) {
// None of the values were parsed (i.e both are not numeric)
// so we will use the normal string comparison.
return val1.compareTo(val2);
}
// If we arrive at this stage then both values are actually numeric !
return valInt1 - valInt2;
}

if (valInt1 == null) {
// We assume that strings "are less" than integers
return -1;
private static boolean isNumber(String number) {
if (number.isEmpty()) {
return false;
}

if (valInt2 == null) {
return 1;
if (number.length() == 1 && number.charAt(0) == '-') {
k3KAW8Pnf7mkmdSMPHz27 marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
for (int i = 0; i < number.length(); i++) {
char c = number.charAt(i);
if ((i == 0 && c != '-') && (c < '0' || c > '9')) {
tobiasdiez marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
}

// If we arrive at this stage then both values are actually numeric !
return valInt1 - valInt2;
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,14 @@ public void compareStringWithInteger() {
public void compareIntegerWithString() {
assertEquals(1, comparator.compare("4", "hi"));
}

@Test
public void compareNegativeInteger() {
assertEquals(1, comparator.compare("-4", "-5"));
}

@Test
public void compareWithMinusString() {
assertEquals(-1, comparator.compare("-", "-5"));
}
}