Fix remaining checkstyle issues in utils/ subpackage

This commit is contained in:
Stypox 2022-03-17 15:57:30 +01:00 committed by litetex
parent 1d5f22e41f
commit ca7c63f273
5 changed files with 79 additions and 71 deletions

View File

@ -11,31 +11,36 @@ import org.schabi.newpipe.extractor.stream.StreamInfo;
import java.util.Collections;
import java.util.List;
public class ExtractorHelper {
private ExtractorHelper() {}
public final class ExtractorHelper {
private ExtractorHelper() {
}
public static <T extends InfoItem> InfoItemsPage<T> getItemsPageOrLogError(Info info, ListExtractor<T> extractor) {
public static <T extends InfoItem> InfoItemsPage<T> getItemsPageOrLogError(
final Info info, final ListExtractor<T> extractor) {
try {
InfoItemsPage<T> page = extractor.getInitialPage();
final InfoItemsPage<T> page = extractor.getInitialPage();
info.addAllErrors(page.getErrors());
return page;
} catch (Exception e) {
} catch (final Exception e) {
info.addError(e);
return InfoItemsPage.emptyPage();
}
}
public static List<InfoItem> getRelatedItemsOrLogError(StreamInfo info, StreamExtractor extractor) {
public static List<InfoItem> getRelatedItemsOrLogError(final StreamInfo info,
final StreamExtractor extractor) {
try {
InfoItemsCollector<? extends InfoItem, ?> collector = extractor.getRelatedItems();
if (collector == null) return Collections.emptyList();
final InfoItemsCollector<? extends InfoItem, ?> collector = extractor.getRelatedItems();
if (collector == null) {
return Collections.emptyList();
}
info.addAllErrors(collector.getErrors());
//noinspection unchecked
return (List<InfoItem>) collector.getItems();
} catch (Exception e) {
} catch (final Exception e) {
info.addError(e);
return Collections.emptyList();
}
@ -45,7 +50,8 @@ public class ExtractorHelper {
* @deprecated Use {@link #getRelatedItemsOrLogError(StreamInfo, StreamExtractor)}
*/
@Deprecated
public static List<InfoItem> getRelatedVideosOrLogError(StreamInfo info, StreamExtractor extractor) {
public static List<InfoItem> getRelatedVideosOrLogError(final StreamInfo info,
final StreamExtractor extractor) {
return getRelatedItemsOrLogError(info, extractor);
}

View File

@ -4,7 +4,7 @@ import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.ScriptableObject;
public class JavaScript {
public final class JavaScript {
private JavaScript() {
}

View File

@ -39,60 +39,66 @@ import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
/**
* avoid using regex !!!
*/
public class Parser {
public final class Parser {
private Parser() {
}
public static class RegexException extends ParsingException {
public RegexException(String message) {
public RegexException(final String message) {
super(message);
}
}
public static String matchGroup1(String pattern, String input) throws RegexException {
public static String matchGroup1(final String pattern, final String input)
throws RegexException {
return matchGroup(pattern, input, 1);
}
public static String matchGroup1(Pattern pattern, String input) throws RegexException {
public static String matchGroup1(final Pattern pattern,
final String input) throws RegexException {
return matchGroup(pattern, input, 1);
}
public static String matchGroup(String pattern, String input, int group) throws RegexException {
Pattern pat = Pattern.compile(pattern);
return matchGroup(pat, input, group);
public static String matchGroup(final String pattern,
final String input,
final int group) throws RegexException {
return matchGroup(Pattern.compile(pattern), input, group);
}
public static String matchGroup(Pattern pat, String input, int group) throws RegexException {
Matcher mat = pat.matcher(input);
boolean foundMatch = mat.find();
public static String matchGroup(final Pattern pat, final String input, final int group)
throws RegexException {
final Matcher matcher = pat.matcher(input);
final boolean foundMatch = matcher.find();
if (foundMatch) {
return mat.group(group);
return matcher.group(group);
} else {
// only pass input to exception message when it is not too long
if (input.length() > 1024) {
throw new RegexException("failed to find pattern \"" + pat.pattern() + "\"");
} else {
throw new RegexException("failed to find pattern \"" + pat.pattern() + "\" inside of \"" + input + "\"");
throw new RegexException("failed to find pattern \"" + pat.pattern()
+ "\" inside of \"" + input + "\"");
}
}
}
public static boolean isMatch(String pattern, String input) {
public static boolean isMatch(final String pattern, final String input) {
final Pattern pat = Pattern.compile(pattern);
final Matcher mat = pat.matcher(input);
return mat.find();
}
public static boolean isMatch(Pattern pattern, String input) {
public static boolean isMatch(final Pattern pattern, final String input) {
final Matcher mat = pattern.matcher(input);
return mat.find();
}
public static Map<String, String> compatParseMap(final String input) throws UnsupportedEncodingException {
Map<String, String> map = new HashMap<>();
for (String arg : input.split("&")) {
String[] splitArg = arg.split("=");
public static Map<String, String> compatParseMap(final String input)
throws UnsupportedEncodingException {
final Map<String, String> map = new HashMap<>();
for (final String arg : input.split("&")) {
final String[] splitArg = arg.split("=");
if (splitArg.length > 1) {
map.put(splitArg[0], URLDecoder.decode(splitArg[1], UTF_8));
} else {
@ -104,19 +110,19 @@ public class Parser {
public static String[] getLinksFromString(final String txt) throws ParsingException {
try {
ArrayList<String> links = new ArrayList<>();
LinkExtractor linkExtractor = LinkExtractor.builder()
final ArrayList<String> links = new ArrayList<>();
final LinkExtractor linkExtractor = LinkExtractor.builder()
.linkTypes(EnumSet.of(LinkType.URL, LinkType.WWW))
.build();
Iterable<LinkSpan> linkss = linkExtractor.extractLinks(txt);
for (LinkSpan ls : linkss) {
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 (Exception e) {
} catch (final Exception e) {
throw new ParsingException("Could not get links from string", e);
}
}

View File

@ -2,7 +2,7 @@ package org.schabi.newpipe.extractor.utils;
import javax.annotation.Nonnull;
public class StringUtils {
public final class StringUtils {
private StringUtils() {
}
@ -15,7 +15,8 @@ public class StringUtils {
* or parenthesis could not be matched .
*/
@Nonnull
public static String matchToClosingParenthesis(@Nonnull final String string, @Nonnull final String start) {
public static String matchToClosingParenthesis(@Nonnull final String string,
@Nonnull final String start) {
int startIndex = string.indexOf(start);
if (startIndex < 0) {
throw new IndexOutOfBoundsException();

View File

@ -6,10 +6,15 @@ import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.*;
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.regex.Pattern;
public class Utils {
public final class Utils {
public static final String HTTP = "http://";
public static final String HTTPS = "https://";
@ -46,18 +51,16 @@ public class Utils {
*
* @param numberWord string to be converted to a long
* @return a long
* @throws NumberFormatException
* @throws ParsingException
*/
public static long mixedNumberWordToLong(final String numberWord) throws NumberFormatException,
ParsingException {
String multiplier = "";
try {
multiplier = Parser.matchGroup("[\\d]+([\\.,][\\d]+)?([KMBkmb])+", numberWord, 2);
} catch (ParsingException ignored) {
} catch (final ParsingException ignored) {
}
double count = Double.parseDouble(Parser.matchGroup1("([\\d]+([\\.,][\\d]+)?)", numberWord)
.replace(",", "."));
final double count = Double.parseDouble(
Parser.matchGroup1("([\\d]+([\\.,][\\d]+)?)", numberWord).replace(",", "."));
switch (multiplier.toUpperCase()) {
case "K":
return (long) (count * 1e3);
@ -86,15 +89,10 @@ public class Utils {
}
}
public static void printErrors(List<Throwable> errors) {
for (Throwable e : errors) {
e.printStackTrace();
System.err.println("----------------");
}
}
public static String replaceHttpWithHttps(final String url) {
if (url == null) return null;
if (url == null) {
return null;
}
if (!url.isEmpty() && url.startsWith(HTTP)) {
return HTTPS + url.substring(HTTP.length());
@ -111,19 +109,17 @@ public class Utils {
* @return a string that contains the value of the query parameter or null if nothing was found
*/
public static String getQueryValue(final URL url, final String parameterName) {
String urlQuery = url.getQuery();
final String urlQuery = url.getQuery();
if (urlQuery != null) {
for (String param : urlQuery.split("&")) {
String[] params = param.split("=", 2);
for (final String param : urlQuery.split("&")) {
final String[] params = param.split("=", 2);
String query;
try {
query = URLDecoder.decode(params[0], UTF_8);
} catch (final UnsupportedEncodingException e) {
System.err.println(
"Cannot decode string with UTF-8. using the string without decoding");
e.printStackTrace();
// Cannot decode string with UTF-8, using the string without decoding
query = params[0];
}
@ -131,9 +127,7 @@ public class Utils {
try {
return URLDecoder.decode(params[1], UTF_8);
} catch (final UnsupportedEncodingException e) {
System.err.println(
"Cannot decode string with UTF-8. using the string without decoding");
e.printStackTrace();
// Cannot decode string with UTF-8, using the string without decoding
return params[1];
}
}
@ -153,7 +147,7 @@ public class Utils {
public static URL stringToURL(final String url) throws MalformedURLException {
try {
return new URL(url);
} catch (MalformedURLException e) {
} catch (final MalformedURLException e) {
// if no protocol is given try prepending "https://"
if (e.getMessage().equals("no protocol: " + url)) {
return new URL(HTTPS + url);
@ -165,13 +159,13 @@ public class Utils {
public static boolean isHTTP(final URL url) {
// make sure its http or https
String protocol = url.getProtocol();
final String protocol = url.getProtocol();
if (!protocol.equals("http") && !protocol.equals("https")) {
return false;
}
boolean usesDefaultPort = url.getPort() == url.getDefaultPort();
boolean setsNoPort = url.getPort() == -1;
final boolean usesDefaultPort = url.getPort() == url.getDefaultPort();
final boolean setsNoPort = url.getPort() == -1;
return setsNoPort || usesDefaultPort;
}
@ -186,14 +180,15 @@ public class Utils {
return url;
}
public static String removeUTF8BOM(String s) {
if (s.startsWith("\uFEFF")) {
s = s.substring(1);
public static String removeUTF8BOM(final String s) {
String result = s;
if (result.startsWith("\uFEFF")) {
result = result.substring(1);
}
if (s.endsWith("\uFEFF")) {
s = s.substring(0, s.length() - 1);
if (result.endsWith("\uFEFF")) {
result = result.substring(0, result.length() - 1);
}
return s;
return result;
}
public static String getBaseUrl(final String url) throws ParsingException {
@ -243,7 +238,7 @@ public class Utils {
}
// can be used for JsonObjects
public static boolean isNullOrEmpty(final Map map) {
public static boolean isNullOrEmpty(final Map<?, ?> map) {
return map == null || map.isEmpty();
}