Skip to content

Commit 2d3cf9d

Browse files
authored
changed Keyword.of to accept List<String> instead of varargs (#13871)
* changed Keyword.of to accept List<String> instead of varargs * changed Keyword.of to accept List<String> instead of varargs
1 parent 4c64b0a commit 2d3cf9d

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

jablib/src/main/java/org/jabref/model/entry/Keyword.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.jabref.model.entry;
22

3+
import java.util.List;
34
import java.util.Objects;
45
import java.util.Set;
56
import java.util.stream.Collectors;
@@ -23,21 +24,23 @@ public Keyword(String keyword) {
2324

2425
/// Connects all the given keywords into one chain and returns its root,
2526
/// e.g. "A", "B", "C" is transformed into "A > B > C".
26-
public static Keyword of(String... keywords) {
27-
if (keywords.length == 0) {
27+
public static Keyword of(List<String> keywords) {
28+
if (keywords.isEmpty()) {
2829
return new Keyword("");
2930
}
3031

31-
Keyword root = new Keyword(keywords[0]);
32-
for (int i = 1; i < keywords.length; i++) {
33-
root.addAtEnd(keywords[i]);
32+
Keyword root = new Keyword(keywords.getFirst());
33+
for (int i = 1; i < keywords.size(); i++) {
34+
root.addAtEnd(keywords.get(i));
3435
}
3536
return root;
3637
}
3738

3839
/// Converts a raw String to a single Keyword
3940
public static Keyword ofHierarchical(String rawString) {
40-
return Keyword.of(rawString.split(Keyword.DEFAULT_HIERARCHICAL_DELIMITER.toString()));
41+
return Keyword.of(Stream.of(rawString.split(Keyword.DEFAULT_HIERARCHICAL_DELIMITER.toString()))
42+
.map(String::trim)
43+
.toList());
4144
}
4245

4346
@Override

jablib/src/test/java/org/jabref/model/entry/KeywordListTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.jabref.model.entry;
22

3+
import java.util.List;
4+
35
import org.junit.jupiter.api.BeforeEach;
46
import org.junit.jupiter.api.Test;
57

@@ -81,15 +83,15 @@ void asStringAddsSpaceAfterDelimiter() {
8183

8284
@Test
8385
void parseHierarchicalChain() {
84-
Keyword expected = Keyword.of("Parent", "Node", "Child");
86+
Keyword expected = Keyword.of(List.of("Parent", "Node", "Child"));
8587

8688
assertEquals(new KeywordList(expected), KeywordList.parse("Parent > Node > Child", ',', '>'));
8789
}
8890

8991
@Test
9092
void parseTwoHierarchicalChains() {
91-
Keyword expectedOne = Keyword.of("Parent1", "Node1", "Child1");
92-
Keyword expectedTwo = Keyword.of("Parent2", "Node2", "Child2");
93+
Keyword expectedOne = Keyword.of(List.of("Parent1", "Node1", "Child1"));
94+
Keyword expectedTwo = Keyword.of(List.of("Parent2", "Node2", "Child2"));
9395

9496
assertEquals(new KeywordList(expectedOne, expectedTwo),
9597
KeywordList.parse("Parent1 > Node1 > Child1, Parent2 > Node2 > Child2", ',', '>'));

jablib/src/test/java/org/jabref/model/entry/KeywordTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.jabref.model.entry;
22

33
import java.util.HashSet;
4+
import java.util.List;
45
import java.util.Set;
56

67
import org.junit.jupiter.api.Test;
@@ -11,13 +12,13 @@ class KeywordTest {
1112

1213
@Test
1314
void getPathFromRootAsStringForSimpleChain() {
14-
Keyword keywordChain = Keyword.of("A", "B", "C");
15+
Keyword keywordChain = Keyword.of(List.of("A", "B", "C"));
1516
assertEquals("A > B", keywordChain.getChild().get().getPathFromRootAsString('>'));
1617
}
1718

1819
@Test
1920
void getAllSubchainsAsStringForSimpleChain() {
20-
Keyword keywordChain = Keyword.of("A", "B", "C");
21+
Keyword keywordChain = Keyword.of(List.of("A", "B", "C"));
2122
Set<String> expected = new HashSet<>();
2223
expected.add("A");
2324
expected.add("A > B");

0 commit comments

Comments
 (0)