Skip to content

Commit

Permalink
Make improvements to methods using toArray().
Browse files Browse the repository at this point in the history
  • Loading branch information
Isira-Seneviratne committed Aug 5, 2022
1 parent fc8b5eb commit 82a5768
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -8,13 +10,12 @@
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 {

Expand Down Expand Up @@ -78,10 +79,10 @@ public KioskExtractor getDefaultKioskExtractor(final Page nextPage,
if (!isNullOrEmpty(defaultKiosk)) {
return getExtractorById(defaultKiosk, nextPage, localization);
} else {
if (!kioskList.isEmpty()) {
final String first = kioskList.keySet().stream().findFirst().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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 !!!
Expand Down Expand Up @@ -111,24 +106,4 @@ public static Map<String, String> compatParseMap(@Nonnull final String input)
}
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);
}
}
}

0 comments on commit 82a5768

Please sign in to comment.