Merge pull request #8738 from Isira-Seneviratne/Collectors_joining

Use Collectors.joining().
This commit is contained in:
Stypox 2022-08-06 22:31:31 +02:00 committed by GitHub
commit edd2b110b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 86 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.Request;
import org.schabi.newpipe.extractor.downloader.Response; import org.schabi.newpipe.extractor.downloader.Response;
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException; import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
import org.schabi.newpipe.util.CookieUtils;
import org.schabi.newpipe.util.InfoCache; import org.schabi.newpipe.util.InfoCache;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import okhttp3.RequestBody; import okhttp3.RequestBody;
@ -63,19 +65,15 @@ public final class DownloaderImpl extends Downloader {
} }
public String getCookies(final String url) { public String getCookies(final String url) {
final List<String> resultCookies = new ArrayList<>(); final String youtubeCookie = url.contains(YOUTUBE_DOMAIN)
if (url.contains(YOUTUBE_DOMAIN)) { ? getCookie(YOUTUBE_RESTRICTED_MODE_COOKIE_KEY) : null;
final String youtubeCookie = getCookie(YOUTUBE_RESTRICTED_MODE_COOKIE_KEY);
if (youtubeCookie != null) {
resultCookies.add(youtubeCookie);
}
}
// Recaptcha cookie is always added TODO: not sure if this is necessary // Recaptcha cookie is always added TODO: not sure if this is necessary
final String recaptchaCookie = getCookie(ReCaptchaActivity.RECAPTCHA_COOKIES_KEY); return Stream.of(youtubeCookie, getCookie(ReCaptchaActivity.RECAPTCHA_COOKIES_KEY))
if (recaptchaCookie != null) { .filter(Objects::nonNull)
resultCookies.add(recaptchaCookie); .flatMap(cookies -> Arrays.stream(cookies.split("; *")))
} .distinct()
return CookieUtils.concatCookies(resultCookies); .collect(Collectors.joining("; "));
} }
public String getCookie(final String key) { 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.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Arrays; import java.util.Arrays;
import java.util.stream.Collectors;
/* /*
* Created by Christian Schabesberger on 24.10.15. * Created by Christian Schabesberger on 24.10.15.
@ -182,14 +183,9 @@ public class ErrorActivity extends AppCompatActivity {
} }
private String formErrorText(final String[] el) { private String formErrorText(final String[] el) {
final StringBuilder text = new StringBuilder(); final String separator = "-------------------------------------";
if (el != null) { return Arrays.stream(el)
for (final String e : el) { .collect(Collectors.joining(separator + "\n", separator + "\n", separator));
text.append("-------------------------------------\n").append(e);
}
}
text.append("-------------------------------------");
return text.toString();
} }
/** /**

View File

@ -9,13 +9,13 @@ import androidx.annotation.Nullable;
import androidx.annotation.XmlRes; import androidx.annotation.XmlRes;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import org.schabi.newpipe.util.Localization;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
/** /**
* Parses the corresponding preference-file(s). * Parses the corresponding preference-file(s).
@ -54,7 +54,7 @@ public class PreferenceParser {
if (xpp.getEventType() == XmlPullParser.START_TAG) { if (xpp.getEventType() == XmlPullParser.START_TAG) {
final PreferenceSearchItem result = parseSearchResult( final PreferenceSearchItem result = parseSearchResult(
xpp, xpp,
joinBreadcrumbs(breadcrumbs), Localization.concatenateStrings(" > ", breadcrumbs),
resId resId
); );
@ -82,12 +82,6 @@ public class PreferenceParser {
return results; return results;
} }
private String joinBreadcrumbs(final List<String> breadcrumbs) {
return breadcrumbs.stream()
.filter(crumb -> !TextUtils.isEmpty(crumb))
.collect(Collectors.joining(" > "));
}
private String getAttribute( private String getAttribute(
final XmlPullParser xpp, final XmlPullParser xpp,
@NonNull final String attribute @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.Arrays;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.stream.Collectors;
/* /*
@ -63,26 +64,14 @@ public final class Localization {
@NonNull @NonNull
public static String concatenateStrings(final String... strings) { public static String concatenateStrings(final String... strings) {
return concatenateStrings(Arrays.asList(strings)); return concatenateStrings(DOT_SEPARATOR, Arrays.asList(strings));
} }
@NonNull @NonNull
public static String concatenateStrings(final List<String> strings) { public static String concatenateStrings(final String delimiter, final List<String> strings) {
if (strings.isEmpty()) { return strings.stream()
return ""; .filter(string -> !TextUtils.isEmpty(string))
} .collect(Collectors.joining(delimiter));
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 org.schabi.newpipe.extractor.localization.Localization getPreferredLocalization( public static org.schabi.newpipe.extractor.localization.Localization getPreferredLocalization(
@ -358,19 +347,4 @@ public final class Localization {
private static double round(final double value, final int places) { private static double round(final double value, final int places) {
return new BigDecimal(value).setScale(places, RoundingMode.HALF_UP).doubleValue(); return new BigDecimal(value).setScale(places, RoundingMode.HALF_UP).doubleValue();
} }
/**
* Workaround to match normalized captions like english to English or deutsch to Deutsch.
* @param list the list to search into
* @param toFind the string to look for
* @return whether the string was found or not
*/
public static boolean containsCaseInsensitive(final List<String> list, final String toFind) {
for (final String i : list) {
if (i.equalsIgnoreCase(toFind)) {
return true;
}
}
return false;
}
} }