From 70814dcfefbc6eaae9aa88a9190d3fa582b0376b Mon Sep 17 00:00:00 2001 From: TobiGr Date: Sun, 21 Feb 2021 12:51:47 +0100 Subject: [PATCH] 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. --- .../org/schabi/newpipe/extractor/utils/Utils.java | 11 +++-------- .../org/schabi/newpipe/extractor/utils/UtilsTest.java | 1 + 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/utils/Utils.java b/extractor/src/main/java/org/schabi/newpipe/extractor/utils/Utils.java index 09574ab51..82ff58263 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/utils/Utils.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/utils/Utils.java @@ -274,14 +274,9 @@ public class Utils { /** * Concatenate all non-null, non-empty and strings which are not equal to "null". */ - public static String nonEmptyAndNullJoin(final String delimiter, final String[] elements) { - final List list = Arrays.asList(elements); - for (int i = list.size() - 1; i >= 0; i--) { - if (isNullOrEmpty(list.get(i)) || list.get(i).equals("null")) { - list.remove(i); - } - } - + public static String nonEmptyAndNullJoin(final CharSequence delimiter, final String[] elements) { + final List list = new java.util.ArrayList<>(Arrays.asList(elements)); + list.removeIf(s -> isNullOrEmpty(s) || s.equals("null")); return join(delimiter, list); } } diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/utils/UtilsTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/utils/UtilsTest.java index e4a65505b..a81270de0 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/utils/UtilsTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/utils/UtilsTest.java @@ -20,6 +20,7 @@ public class UtilsTest { @Test public void testJoin() { assertEquals("some,random,stuff", Utils.join(",", Arrays.asList("some", "random", "stuff"))); + assertEquals("some,random,not-null,stuff", Utils.nonEmptyAndNullJoin(",", new String[]{"some", "null", "random", "", "not-null", null, "stuff"})); } @Test