Use Collectors.joining().

This commit is contained in:
Isira Seneviratne 2022-07-29 09:05:50 +05:30
parent 1a8f396e77
commit 7aacaf8c38
5 changed files with 24 additions and 71 deletions

View File

@ -11,15 +11,17 @@ import org.schabi.newpipe.extractor.downloader.Downloader;
import org.schabi.newpipe.extractor.downloader.Request;
import org.schabi.newpipe.extractor.downloader.Response;
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
import org.schabi.newpipe.util.CookieUtils;
import org.schabi.newpipe.util.InfoCache;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
@ -63,19 +65,15 @@ public final class DownloaderImpl extends Downloader {
}
public String getCookies(final String url) {
final List<String> resultCookies = new ArrayList<>();
if (url.contains(YOUTUBE_DOMAIN)) {
final String youtubeCookie = getCookie(YOUTUBE_RESTRICTED_MODE_COOKIE_KEY);
if (youtubeCookie != null) {
resultCookies.add(youtubeCookie);
}
}
final String youtubeCookie = url.contains(YOUTUBE_DOMAIN)
? getCookie(YOUTUBE_RESTRICTED_MODE_COOKIE_KEY) : null;
// Recaptcha cookie is always added TODO: not sure if this is necessary
final String recaptchaCookie = getCookie(ReCaptchaActivity.RECAPTCHA_COOKIES_KEY);
if (recaptchaCookie != null) {
resultCookies.add(recaptchaCookie);
}
return CookieUtils.concatCookies(resultCookies);
return Stream.of(youtubeCookie, getCookie(ReCaptchaActivity.RECAPTCHA_COOKIES_KEY))
.filter(Objects::nonNull)
.flatMap(cookies -> Arrays.stream(cookies.split("; *")))
.distinct()
.collect(Collectors.joining("; "));
}
public String getCookie(final String key) {

View File

@ -31,6 +31,7 @@ import org.schabi.newpipe.util.external_communication.ShareUtils;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.stream.Collectors;
/*
* Created by Christian Schabesberger on 24.10.15.
@ -182,14 +183,9 @@ public class ErrorActivity extends AppCompatActivity {
}
private String formErrorText(final String[] el) {
final StringBuilder text = new StringBuilder();
if (el != null) {
for (final String e : el) {
text.append("-------------------------------------\n").append(e);
}
}
text.append("-------------------------------------");
return text.toString();
final String separator = "-------------------------------------";
return Arrays.stream(el)
.collect(Collectors.joining(separator + "\n", separator + "\n", separator));
}
/**

View File

@ -9,13 +9,13 @@ import androidx.annotation.Nullable;
import androidx.annotation.XmlRes;
import androidx.preference.PreferenceManager;
import org.schabi.newpipe.util.Localization;
import org.xmlpull.v1.XmlPullParser;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Parses the corresponding preference-file(s).
@ -54,7 +54,7 @@ public class PreferenceParser {
if (xpp.getEventType() == XmlPullParser.START_TAG) {
final PreferenceSearchItem result = parseSearchResult(
xpp,
joinBreadcrumbs(breadcrumbs),
Localization.concatenateStrings(" > ", breadcrumbs),
resId
);
@ -82,12 +82,6 @@ public class PreferenceParser {
return results;
}
private String joinBreadcrumbs(final List<String> breadcrumbs) {
return breadcrumbs.stream()
.filter(crumb -> !TextUtils.isEmpty(crumb))
.collect(Collectors.joining(" > "));
}
private String getAttribute(
final XmlPullParser xpp,
@NonNull final String attribute

View File

@ -1,24 +0,0 @@
package org.schabi.newpipe.util;
import android.text.TextUtils;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
public final class CookieUtils {
private CookieUtils() {
}
public static String concatCookies(final Collection<String> cookieStrings) {
final Set<String> cookieSet = new HashSet<>();
for (final String cookies : cookieStrings) {
cookieSet.addAll(splitCookies(cookies));
}
return TextUtils.join("; ", cookieSet).trim();
}
public static Set<String> splitCookies(final String cookies) {
return Set.of(cookies.split("; *"));
}
}

View File

@ -32,6 +32,7 @@ import java.time.format.FormatStyle;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
/*
@ -63,26 +64,14 @@ public final class Localization {
@NonNull
public static String concatenateStrings(final String... strings) {
return concatenateStrings(Arrays.asList(strings));
return concatenateStrings(DOT_SEPARATOR, Arrays.asList(strings));
}
@NonNull
public static String concatenateStrings(final List<String> strings) {
if (strings.isEmpty()) {
return "";
}
final StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(strings.get(0));
for (int i = 1; i < strings.size(); i++) {
final String string = strings.get(i);
if (!TextUtils.isEmpty(string)) {
stringBuilder.append(DOT_SEPARATOR).append(strings.get(i));
}
}
return stringBuilder.toString();
public static String concatenateStrings(final String delimiter, final List<String> strings) {
return strings.stream()
.filter(string -> !TextUtils.isEmpty(string))
.collect(Collectors.joining(delimiter));
}
public static org.schabi.newpipe.extractor.localization.Localization getPreferredLocalization(