Skip to content

Commit ca7c63f

Browse files
Stypoxlitetex
authored andcommitted
Fix remaining checkstyle issues in utils/ subpackage
1 parent 1d5f22e commit ca7c63f

5 files changed

Lines changed: 79 additions & 71 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/utils/ExtractorHelper.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,36 @@
1111
import java.util.Collections;
1212
import java.util.List;
1313

14-
public class ExtractorHelper {
15-
private ExtractorHelper() {}
14+
public final class ExtractorHelper {
15+
private ExtractorHelper() {
16+
}
1617

17-
public static <T extends InfoItem> InfoItemsPage<T> getItemsPageOrLogError(Info info, ListExtractor<T> extractor) {
18+
public static <T extends InfoItem> InfoItemsPage<T> getItemsPageOrLogError(
19+
final Info info, final ListExtractor<T> extractor) {
1820
try {
19-
InfoItemsPage<T> page = extractor.getInitialPage();
21+
final InfoItemsPage<T> page = extractor.getInitialPage();
2022
info.addAllErrors(page.getErrors());
2123

2224
return page;
23-
} catch (Exception e) {
25+
} catch (final Exception e) {
2426
info.addError(e);
2527
return InfoItemsPage.emptyPage();
2628
}
2729
}
2830

2931

30-
public static List<InfoItem> getRelatedItemsOrLogError(StreamInfo info, StreamExtractor extractor) {
32+
public static List<InfoItem> getRelatedItemsOrLogError(final StreamInfo info,
33+
final StreamExtractor extractor) {
3134
try {
32-
InfoItemsCollector<? extends InfoItem, ?> collector = extractor.getRelatedItems();
33-
if (collector == null) return Collections.emptyList();
35+
final InfoItemsCollector<? extends InfoItem, ?> collector = extractor.getRelatedItems();
36+
if (collector == null) {
37+
return Collections.emptyList();
38+
}
3439
info.addAllErrors(collector.getErrors());
3540

3641
//noinspection unchecked
3742
return (List<InfoItem>) collector.getItems();
38-
} catch (Exception e) {
43+
} catch (final Exception e) {
3944
info.addError(e);
4045
return Collections.emptyList();
4146
}
@@ -45,7 +50,8 @@ public static List<InfoItem> getRelatedItemsOrLogError(StreamInfo info, StreamEx
4550
* @deprecated Use {@link #getRelatedItemsOrLogError(StreamInfo, StreamExtractor)}
4651
*/
4752
@Deprecated
48-
public static List<InfoItem> getRelatedVideosOrLogError(StreamInfo info, StreamExtractor extractor) {
53+
public static List<InfoItem> getRelatedVideosOrLogError(final StreamInfo info,
54+
final StreamExtractor extractor) {
4955
return getRelatedItemsOrLogError(info, extractor);
5056
}
5157

extractor/src/main/java/org/schabi/newpipe/extractor/utils/JavaScript.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import org.mozilla.javascript.Function;
55
import org.mozilla.javascript.ScriptableObject;
66

7-
public class JavaScript {
7+
public final class JavaScript {
88

99
private JavaScript() {
1010
}

extractor/src/main/java/org/schabi/newpipe/extractor/utils/Parser.java

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,60 +39,66 @@
3939
/**
4040
* avoid using regex !!!
4141
*/
42-
public class Parser {
42+
public final class Parser {
4343

4444
private Parser() {
4545
}
4646

4747
public static class RegexException extends ParsingException {
48-
public RegexException(String message) {
48+
public RegexException(final String message) {
4949
super(message);
5050
}
5151
}
5252

53-
public static String matchGroup1(String pattern, String input) throws RegexException {
53+
public static String matchGroup1(final String pattern, final String input)
54+
throws RegexException {
5455
return matchGroup(pattern, input, 1);
5556
}
5657

57-
public static String matchGroup1(Pattern pattern, String input) throws RegexException {
58+
public static String matchGroup1(final Pattern pattern,
59+
final String input) throws RegexException {
5860
return matchGroup(pattern, input, 1);
5961
}
6062

61-
public static String matchGroup(String pattern, String input, int group) throws RegexException {
62-
Pattern pat = Pattern.compile(pattern);
63-
return matchGroup(pat, input, group);
63+
public static String matchGroup(final String pattern,
64+
final String input,
65+
final int group) throws RegexException {
66+
return matchGroup(Pattern.compile(pattern), input, group);
6467
}
6568

66-
public static String matchGroup(Pattern pat, String input, int group) throws RegexException {
67-
Matcher mat = pat.matcher(input);
68-
boolean foundMatch = mat.find();
69+
public static String matchGroup(final Pattern pat, final String input, final int group)
70+
throws RegexException {
71+
final Matcher matcher = pat.matcher(input);
72+
final boolean foundMatch = matcher.find();
6973
if (foundMatch) {
70-
return mat.group(group);
74+
return matcher.group(group);
7175
} else {
7276
// only pass input to exception message when it is not too long
7377
if (input.length() > 1024) {
7478
throw new RegexException("failed to find pattern \"" + pat.pattern() + "\"");
7579
} else {
76-
throw new RegexException("failed to find pattern \"" + pat.pattern() + "\" inside of \"" + input + "\"");
80+
throw new RegexException("failed to find pattern \"" + pat.pattern()
81+
+ "\" inside of \"" + input + "\"");
7782
}
7883
}
7984
}
8085

81-
public static boolean isMatch(String pattern, String input) {
86+
public static boolean isMatch(final String pattern, final String input) {
8287
final Pattern pat = Pattern.compile(pattern);
8388
final Matcher mat = pat.matcher(input);
8489
return mat.find();
8590
}
8691

87-
public static boolean isMatch(Pattern pattern, String input) {
92+
public static boolean isMatch(final Pattern pattern, final String input) {
8893
final Matcher mat = pattern.matcher(input);
8994
return mat.find();
9095
}
9196

92-
public static Map<String, String> compatParseMap(final String input) throws UnsupportedEncodingException {
93-
Map<String, String> map = new HashMap<>();
94-
for (String arg : input.split("&")) {
95-
String[] splitArg = arg.split("=");
97+
public static Map<String, String> compatParseMap(final String input)
98+
throws UnsupportedEncodingException {
99+
final Map<String, String> map = new HashMap<>();
100+
for (final String arg : input.split("&")) {
101+
final String[] splitArg = arg.split("=");
96102
if (splitArg.length > 1) {
97103
map.put(splitArg[0], URLDecoder.decode(splitArg[1], UTF_8));
98104
} else {
@@ -104,19 +110,19 @@ public static Map<String, String> compatParseMap(final String input) throws Unsu
104110

105111
public static String[] getLinksFromString(final String txt) throws ParsingException {
106112
try {
107-
ArrayList<String> links = new ArrayList<>();
108-
LinkExtractor linkExtractor = LinkExtractor.builder()
113+
final ArrayList<String> links = new ArrayList<>();
114+
final LinkExtractor linkExtractor = LinkExtractor.builder()
109115
.linkTypes(EnumSet.of(LinkType.URL, LinkType.WWW))
110116
.build();
111-
Iterable<LinkSpan> linkss = linkExtractor.extractLinks(txt);
112-
for (LinkSpan ls : linkss) {
117+
final Iterable<LinkSpan> linkSpans = linkExtractor.extractLinks(txt);
118+
for (final LinkSpan ls : linkSpans) {
113119
links.add(txt.substring(ls.getBeginIndex(), ls.getEndIndex()));
114120
}
115121

116122
String[] linksarray = new String[links.size()];
117123
linksarray = links.toArray(linksarray);
118124
return linksarray;
119-
} catch (Exception e) {
125+
} catch (final Exception e) {
120126
throw new ParsingException("Could not get links from string", e);
121127
}
122128
}

extractor/src/main/java/org/schabi/newpipe/extractor/utils/StringUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import javax.annotation.Nonnull;
44

5-
public class StringUtils {
5+
public final class StringUtils {
66

77
private StringUtils() {
88
}
@@ -15,7 +15,8 @@ private StringUtils() {
1515
* or parenthesis could not be matched .
1616
*/
1717
@Nonnull
18-
public static String matchToClosingParenthesis(@Nonnull final String string, @Nonnull final String start) {
18+
public static String matchToClosingParenthesis(@Nonnull final String string,
19+
@Nonnull final String start) {
1920
int startIndex = string.indexOf(start);
2021
if (startIndex < 0) {
2122
throw new IndexOutOfBoundsException();

extractor/src/main/java/org/schabi/newpipe/extractor/utils/Utils.java

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66
import java.net.MalformedURLException;
77
import java.net.URL;
88
import java.net.URLDecoder;
9-
import java.util.*;
9+
import java.util.Arrays;
10+
import java.util.Collection;
11+
import java.util.Iterator;
12+
import java.util.LinkedList;
13+
import java.util.List;
14+
import java.util.Map;
1015
import java.util.regex.Pattern;
1116

12-
public class Utils {
17+
public final class Utils {
1318

1419
public static final String HTTP = "http://";
1520
public static final String HTTPS = "https://";
@@ -46,18 +51,16 @@ public static String removeNonDigitCharacters(final String toRemove) {
4651
*
4752
* @param numberWord string to be converted to a long
4853
* @return a long
49-
* @throws NumberFormatException
50-
* @throws ParsingException
5154
*/
5255
public static long mixedNumberWordToLong(final String numberWord) throws NumberFormatException,
5356
ParsingException {
5457
String multiplier = "";
5558
try {
5659
multiplier = Parser.matchGroup("[\\d]+([\\.,][\\d]+)?([KMBkmb])+", numberWord, 2);
57-
} catch (ParsingException ignored) {
60+
} catch (final ParsingException ignored) {
5861
}
59-
double count = Double.parseDouble(Parser.matchGroup1("([\\d]+([\\.,][\\d]+)?)", numberWord)
60-
.replace(",", "."));
62+
final double count = Double.parseDouble(
63+
Parser.matchGroup1("([\\d]+([\\.,][\\d]+)?)", numberWord).replace(",", "."));
6164
switch (multiplier.toUpperCase()) {
6265
case "K":
6366
return (long) (count * 1e3);
@@ -86,15 +89,10 @@ public static void checkUrl(final String pattern, final String url) throws Parsi
8689
}
8790
}
8891

89-
public static void printErrors(List<Throwable> errors) {
90-
for (Throwable e : errors) {
91-
e.printStackTrace();
92-
System.err.println("----------------");
93-
}
94-
}
95-
9692
public static String replaceHttpWithHttps(final String url) {
97-
if (url == null) return null;
93+
if (url == null) {
94+
return null;
95+
}
9896

9997
if (!url.isEmpty() && url.startsWith(HTTP)) {
10098
return HTTPS + url.substring(HTTP.length());
@@ -111,29 +109,25 @@ public static String replaceHttpWithHttps(final String url) {
111109
* @return a string that contains the value of the query parameter or null if nothing was found
112110
*/
113111
public static String getQueryValue(final URL url, final String parameterName) {
114-
String urlQuery = url.getQuery();
112+
final String urlQuery = url.getQuery();
115113

116114
if (urlQuery != null) {
117-
for (String param : urlQuery.split("&")) {
118-
String[] params = param.split("=", 2);
115+
for (final String param : urlQuery.split("&")) {
116+
final String[] params = param.split("=", 2);
119117

120118
String query;
121119
try {
122120
query = URLDecoder.decode(params[0], UTF_8);
123121
} catch (final UnsupportedEncodingException e) {
124-
System.err.println(
125-
"Cannot decode string with UTF-8. using the string without decoding");
126-
e.printStackTrace();
122+
// Cannot decode string with UTF-8, using the string without decoding
127123
query = params[0];
128124
}
129125

130126
if (query.equals(parameterName)) {
131127
try {
132128
return URLDecoder.decode(params[1], UTF_8);
133129
} catch (final UnsupportedEncodingException e) {
134-
System.err.println(
135-
"Cannot decode string with UTF-8. using the string without decoding");
136-
e.printStackTrace();
130+
// Cannot decode string with UTF-8, using the string without decoding
137131
return params[1];
138132
}
139133
}
@@ -153,7 +147,7 @@ public static String getQueryValue(final URL url, final String parameterName) {
153147
public static URL stringToURL(final String url) throws MalformedURLException {
154148
try {
155149
return new URL(url);
156-
} catch (MalformedURLException e) {
150+
} catch (final MalformedURLException e) {
157151
// if no protocol is given try prepending "https://"
158152
if (e.getMessage().equals("no protocol: " + url)) {
159153
return new URL(HTTPS + url);
@@ -165,13 +159,13 @@ public static URL stringToURL(final String url) throws MalformedURLException {
165159

166160
public static boolean isHTTP(final URL url) {
167161
// make sure its http or https
168-
String protocol = url.getProtocol();
162+
final String protocol = url.getProtocol();
169163
if (!protocol.equals("http") && !protocol.equals("https")) {
170164
return false;
171165
}
172166

173-
boolean usesDefaultPort = url.getPort() == url.getDefaultPort();
174-
boolean setsNoPort = url.getPort() == -1;
167+
final boolean usesDefaultPort = url.getPort() == url.getDefaultPort();
168+
final boolean setsNoPort = url.getPort() == -1;
175169

176170
return setsNoPort || usesDefaultPort;
177171
}
@@ -186,14 +180,15 @@ public static String removeMAndWWWFromUrl(final String url) {
186180
return url;
187181
}
188182

189-
public static String removeUTF8BOM(String s) {
190-
if (s.startsWith("\uFEFF")) {
191-
s = s.substring(1);
183+
public static String removeUTF8BOM(final String s) {
184+
String result = s;
185+
if (result.startsWith("\uFEFF")) {
186+
result = result.substring(1);
192187
}
193-
if (s.endsWith("\uFEFF")) {
194-
s = s.substring(0, s.length() - 1);
188+
if (result.endsWith("\uFEFF")) {
189+
result = result.substring(0, result.length() - 1);
195190
}
196-
return s;
191+
return result;
197192
}
198193

199194
public static String getBaseUrl(final String url) throws ParsingException {
@@ -243,7 +238,7 @@ public static boolean isNullOrEmpty(final Collection<?> collection) {
243238
}
244239

245240
// can be used for JsonObjects
246-
public static boolean isNullOrEmpty(final Map map) {
241+
public static boolean isNullOrEmpty(final Map<?, ?> map) {
247242
return map == null || map.isEmpty();
248243
}
249244

0 commit comments

Comments
 (0)