Skip to content

Commit 70814dc

Browse files
committed
Fix Utils.nonEmptyAndNullJoin
When using the index here, it the index needs to be decremented once an element is removed. To cirecumvent this, the native Collections.removeIf() method is used.
1 parent b9e8ee8 commit 70814dc

2 files changed

Lines changed: 4 additions & 8 deletions

File tree

  • extractor/src

extractor/src/main/java/org/schabi/newpipe/extractor/utils/Utils.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -274,14 +274,9 @@ public static String join(final String delimiter, final String mapJoin,
274274
/**
275275
* Concatenate all non-null, non-empty and strings which are not equal to <code>"null"</code>.
276276
*/
277-
public static String nonEmptyAndNullJoin(final String delimiter, final String[] elements) {
278-
final List<String> list = Arrays.asList(elements);
279-
for (int i = list.size() - 1; i >= 0; i--) {
280-
if (isNullOrEmpty(list.get(i)) || list.get(i).equals("null")) {
281-
list.remove(i);
282-
}
283-
}
284-
277+
public static String nonEmptyAndNullJoin(final CharSequence delimiter, final String[] elements) {
278+
final List<String> list = new java.util.ArrayList<>(Arrays.asList(elements));
279+
list.removeIf(s -> isNullOrEmpty(s) || s.equals("null"));
285280
return join(delimiter, list);
286281
}
287282
}

extractor/src/test/java/org/schabi/newpipe/extractor/utils/UtilsTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public void testMixedNumberWordToLong() throws ParsingException {
2020
@Test
2121
public void testJoin() {
2222
assertEquals("some,random,stuff", Utils.join(",", Arrays.asList("some", "random", "stuff")));
23+
assertEquals("some,random,not-null,stuff", Utils.nonEmptyAndNullJoin(",", new String[]{"some", "null", "random", "", "not-null", null, "stuff"}));
2324
}
2425

2526
@Test

0 commit comments

Comments
 (0)