Merge pull request #886 from Isira-Seneviratne/toArray_improvements

Make improvements to methods using toArray().
This commit is contained in:
Stypox 2022-08-06 22:34:23 +02:00 committed by GitHub
commit d12003651b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 33 deletions

View File

@ -1,5 +1,7 @@
package org.schabi.newpipe.extractor.kiosk;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.Page;
import org.schabi.newpipe.extractor.StreamingService;
@ -8,13 +10,12 @@ import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
import org.schabi.newpipe.extractor.localization.ContentCountry;
import org.schabi.newpipe.extractor.localization.Localization;
import javax.annotation.Nullable;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
import javax.annotation.Nullable;
public class KioskList {
@ -78,10 +79,10 @@ public class KioskList {
if (!isNullOrEmpty(defaultKiosk)) {
return getExtractorById(defaultKiosk, nextPage, localization);
} else {
if (!kioskList.isEmpty()) {
final String first = kioskList.keySet().stream().findAny().orElse(null);
if (first != null) {
// if not set get any entry
final Object[] keySet = kioskList.keySet().toArray();
return getExtractorById(keySet[0].toString(), nextPage, localization);
return getExtractorById(first, nextPage, localization);
} else {
return null;
}

View File

@ -20,23 +20,18 @@
package org.schabi.newpipe.extractor.utils;
import org.nibor.autolink.LinkExtractor;
import org.nibor.autolink.LinkSpan;
import org.nibor.autolink.LinkType;
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import javax.annotation.Nonnull;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
import javax.annotation.Nonnull;
/**
* Avoid using regex !!!
@ -111,24 +106,4 @@ public final class Parser {
}
return map;
}
@Nonnull
public static String[] getLinksFromString(final String txt) throws ParsingException {
try {
final List<String> links = new ArrayList<>();
final LinkExtractor linkExtractor = LinkExtractor.builder()
.linkTypes(EnumSet.of(LinkType.URL, LinkType.WWW))
.build();
final Iterable<LinkSpan> linkSpans = linkExtractor.extractLinks(txt);
for (final LinkSpan ls : linkSpans) {
links.add(txt.substring(ls.getBeginIndex(), ls.getEndIndex()));
}
String[] linksarray = new String[links.size()];
linksarray = links.toArray(linksarray);
return linksarray;
} catch (final Exception e) {
throw new ParsingException("Could not get links from string", e);
}
}
}