Merge pull request #881 from Isira-Seneviratne/String_join

Use String.join() and Collectors.joining().
This commit is contained in:
Stypox 2022-08-04 12:09:33 +02:00 committed by GitHub
commit 2906be22af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 42 deletions

View File

@ -136,13 +136,8 @@ public class BandcampStreamExtractor extends StreamExtractor {
@Nonnull
@Override
public Description getDescription() {
final String s = Utils.nonEmptyAndNullJoin(
"\n\n",
new String[] {
current.getString("about"),
current.getString("lyrics"),
current.getString("credits")
});
final String s = Utils.nonEmptyAndNullJoin("\n\n", current.getString("about"),
current.getString("lyrics"), current.getString("credits"));
return new Description(s, Description.PLAIN_TEXT);
}

View File

@ -16,7 +16,6 @@ import org.schabi.newpipe.extractor.playlist.PlaylistExtractor;
import org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper;
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
import org.schabi.newpipe.extractor.utils.Utils;
import java.io.IOException;
import java.util.ArrayList;
@ -165,7 +164,7 @@ public class SoundcloudPlaylistExtractor extends PlaylistExtractor {
}
final String currentPageUrl = SOUNDCLOUD_API_V2_URL + "tracks?client_id="
+ SoundcloudParsingHelper.clientId() + "&ids=" + Utils.join(",", currentIds);
+ SoundcloudParsingHelper.clientId() + "&ids=" + String.join(",", currentIds);
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
final String response = NewPipe.getDownloader().get(currentPageUrl,

View File

@ -6,15 +6,12 @@ import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@ -310,31 +307,14 @@ public final class Utils {
return true;
}
@Nonnull
public static String join(final CharSequence delimiter,
@Nonnull final Iterable<? extends CharSequence> elements) {
final StringBuilder stringBuilder = new StringBuilder();
final Iterator<? extends CharSequence> iterator = elements.iterator();
while (iterator.hasNext()) {
stringBuilder.append(iterator.next());
if (iterator.hasNext()) {
stringBuilder.append(delimiter);
}
}
return stringBuilder.toString();
}
@Nonnull
public static String join(
final String delimiter,
final String mapJoin,
@Nonnull final Map<? extends CharSequence, ? extends CharSequence> elements) {
final List<String> list = new LinkedList<>();
for (final Map.Entry<? extends CharSequence, ? extends CharSequence> entry
: elements.entrySet()) {
list.add(entry.getKey() + mapJoin + entry.getValue());
}
return join(delimiter, list);
return elements.entrySet().stream()
.map(entry -> entry.getKey() + mapJoin + entry.getValue())
.collect(Collectors.joining(delimiter));
}
/**
@ -342,10 +322,10 @@ public final class Utils {
*/
@Nonnull
public static String nonEmptyAndNullJoin(final CharSequence delimiter,
final String[] elements) {
final List<String> list = new ArrayList<>(Arrays.asList(elements));
list.removeIf(s -> isNullOrEmpty(s) || s.equals("null"));
return join(delimiter, list);
final String... elements) {
return Arrays.stream(elements)
.filter(s -> !isNullOrEmpty(s) && !s.equals("null"))
.collect(Collectors.joining(delimiter));
}
/**

View File

@ -3,9 +3,6 @@ package org.schabi.newpipe.extractor.utils;
import org.junit.jupiter.api.Test;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import javax.annotation.Nonnull;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.assertEquals;
class UtilsTest {
@ -20,8 +17,8 @@ class UtilsTest {
@Test
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"}));
assertEquals("some,random,not-null,stuff", Utils.nonEmptyAndNullJoin(",",
"some", "null", "random", "", "not-null", null, "stuff"));
}
@Test