mirror of
https://github.com/TeamNewPipe/NewPipeExtractor.git
synced 2024-12-13 22:00:32 +05:30
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.
This commit is contained in:
parent
b9e8ee8450
commit
70814dcfef
@ -274,14 +274,9 @@ public class Utils {
|
||||
/**
|
||||
* Concatenate all non-null, non-empty and strings which are not equal to <code>"null"</code>.
|
||||
*/
|
||||
public static String nonEmptyAndNullJoin(final String delimiter, final String[] elements) {
|
||||
final List<String> 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<String> list = new java.util.ArrayList<>(Arrays.asList(elements));
|
||||
list.removeIf(s -> isNullOrEmpty(s) || s.equals("null"));
|
||||
return join(delimiter, list);
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user