Skip to content

Commit 3a858e6

Browse files
authored
Merge pull request #315 from B0pol/isnullorempty
refactor: add Utils.isNullOrEmpty()
2 parents 3cae32b + adaf196 commit 3a858e6

23 files changed

Lines changed: 124 additions & 78 deletions

extractor/src/main/java/org/schabi/newpipe/extractor/ListExtractor.java

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

33
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
44
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
5+
import org.schabi.newpipe.extractor.utils.Utils;
56

67
import java.io.IOException;
78
import java.util.Collections;
89
import java.util.List;
910

1011
import javax.annotation.Nonnull;
1112

13+
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
14+
1215
/**
1316
* Base class to extractors that have a list (e.g. playlists, users).
1417
*/
@@ -63,8 +66,7 @@ public ListExtractor(StreamingService service, ListLinkHandler linkHandler) {
6366
public abstract InfoItemsPage<R> getPage(final String pageUrl) throws IOException, ExtractionException;
6467

6568
public boolean hasNextPage() throws IOException, ExtractionException {
66-
final String nextPageUrl = getNextPageUrl();
67-
return nextPageUrl != null && !nextPageUrl.isEmpty();
69+
return !isNullOrEmpty(getNextPageUrl());
6870
}
6971

7072
@Override
@@ -123,7 +125,7 @@ public InfoItemsPage(List<T> itemsList, String nextPageUrl, List<Throwable> erro
123125
}
124126

125127
public boolean hasNextPage() {
126-
return nextPageUrl != null && !nextPageUrl.isEmpty();
128+
return !isNullOrEmpty(nextPageUrl);
127129
}
128130

129131
public List<T> getItems() {

extractor/src/main/java/org/schabi/newpipe/extractor/ListInfo.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import java.util.List;
66

7+
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
8+
79
public abstract class ListInfo<T extends InfoItem> extends Info {
810
private List<T> relatedItems;
911
private String nextPageUrl = null;
@@ -37,7 +39,7 @@ public void setRelatedItems(List<T> relatedItems) {
3739
}
3840

3941
public boolean hasNextPage() {
40-
return nextPageUrl != null && !nextPageUrl.isEmpty();
42+
return !isNullOrEmpty(nextPageUrl);
4143
}
4244

4345
public String getNextPageUrl() {

extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudParsingHelper.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
2222
import org.schabi.newpipe.extractor.utils.Parser;
2323
import org.schabi.newpipe.extractor.utils.Parser.RegexException;
24+
import org.schabi.newpipe.extractor.utils.Utils;
2425

2526
import javax.annotation.Nonnull;
2627
import java.io.IOException;
@@ -32,6 +33,7 @@
3233
import static java.util.Collections.singletonList;
3334
import static org.schabi.newpipe.extractor.ServiceList.SoundCloud;
3435
import static org.schabi.newpipe.extractor.utils.JsonUtils.EMPTY_STRING;
36+
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
3537
import static org.schabi.newpipe.extractor.utils.Utils.replaceHttpWithHttps;
3638

3739
public class SoundcloudParsingHelper {
@@ -42,7 +44,7 @@ private SoundcloudParsingHelper() {
4244
}
4345

4446
public static String clientId() throws ExtractionException, IOException {
45-
if (clientId != null && !clientId.isEmpty()) return clientId;
47+
if (!isNullOrEmpty(clientId)) return clientId;
4648

4749
Downloader dl = NewPipe.getDownloader();
4850
clientId = HARDCODED_CLIENT_ID;
@@ -64,7 +66,7 @@ public static String clientId() throws ExtractionException, IOException {
6466

6567
for (Element element : possibleScripts) {
6668
final String srcUrl = element.attr("src");
67-
if (srcUrl != null && !srcUrl.isEmpty()) {
69+
if (!isNullOrEmpty(srcUrl)) {
6870
try {
6971
return clientId = Parser.matchGroup1(clientIdPattern, dl.get(srcUrl, headers).responseBody());
7072
} catch (RegexException ignored) {

extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudChannelExtractor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.IOException;
1919

2020
import static org.schabi.newpipe.extractor.utils.JsonUtils.EMPTY_STRING;
21+
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
2122

2223
@SuppressWarnings("WeakerAccess")
2324
public class SoundcloudChannelExtractor extends ChannelExtractor {
@@ -132,7 +133,7 @@ private void computeNextPageAndGetStreams() throws ExtractionException {
132133

133134
@Override
134135
public InfoItemsPage<StreamInfoItem> getPage(final String pageUrl) throws IOException, ExtractionException {
135-
if (pageUrl == null || pageUrl.isEmpty()) {
136+
if (isNullOrEmpty(pageUrl)) {
136137
throw new ExtractionException(new IllegalArgumentException("Page url is empty or null"));
137138
}
138139

extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudChartsExtractor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.io.IOException;
1414

1515
import static org.schabi.newpipe.extractor.ServiceList.SoundCloud;
16+
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
1617

1718
public class SoundcloudChartsExtractor extends KioskExtractor<StreamInfoItem> {
1819
private StreamInfoItemsCollector collector = null;
@@ -36,7 +37,7 @@ public String getName() {
3637

3738
@Override
3839
public InfoItemsPage<StreamInfoItem> getPage(String pageUrl) throws IOException, ExtractionException {
39-
if (pageUrl == null || pageUrl.isEmpty()) {
40+
if (isNullOrEmpty(pageUrl)) {
4041
throw new ExtractionException(new IllegalArgumentException("Page url is empty or null"));
4142
}
4243

extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistExtractor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import javax.annotation.Nonnull;
2222
import javax.annotation.Nullable;
2323

24+
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
25+
2426
@SuppressWarnings("WeakerAccess")
2527
public class SoundcloudPlaylistExtractor extends PlaylistExtractor {
2628
private static final int streamsPerRequestedPage = 15;
@@ -76,7 +78,7 @@ public String getThumbnailUrl() {
7678

7779
for (StreamInfoItem item : infoItems.getItems()) {
7880
artworkUrl = item.getThumbnailUrl();
79-
if (artworkUrl != null && !artworkUrl.isEmpty()) break;
81+
if (!isNullOrEmpty(artworkUrl)) break;
8082
}
8183
} catch (Exception ignored) {
8284
}
@@ -160,7 +162,7 @@ public String getNextPageUrl() throws IOException, ExtractionException {
160162

161163
@Override
162164
public InfoItemsPage<StreamInfoItem> getPage(String pageUrl) throws IOException, ExtractionException {
163-
if (pageUrl == null || pageUrl.isEmpty()) {
165+
if (isNullOrEmpty(pageUrl)) {
164166
throw new ExtractionException(new IllegalArgumentException("Page url is empty or null"));
165167
}
166168

extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudStreamExtractor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import javax.annotation.Nonnull;
3737

3838
import static org.schabi.newpipe.extractor.utils.JsonUtils.EMPTY_STRING;
39+
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
3940

4041
public class SoundcloudStreamExtractor extends StreamExtractor {
4142
private JsonObject track;
@@ -191,7 +192,7 @@ public List<AudioStream> getAudioStreams() throws IOException, ExtractionExcepti
191192
JsonObject t = (JsonObject) transcoding;
192193
String url = t.getString("url");
193194

194-
if (url != null && !url.isEmpty()) {
195+
if (!isNullOrEmpty(url)) {
195196

196197
// We can only play the mp3 format, but not handle m3u playlists / streams.
197198
// what about Opus?

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelper.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929

3030
import static org.schabi.newpipe.extractor.NewPipe.getDownloader;
3131
import static org.schabi.newpipe.extractor.utils.JsonUtils.EMPTY_STRING;
32-
import static org.schabi.newpipe.extractor.utils.Utils.HTTP;
33-
import static org.schabi.newpipe.extractor.utils.Utils.HTTPS;
32+
import static org.schabi.newpipe.extractor.utils.Utils.*;
3433

3534
/*
3635
* Created by Christian Schabesberger on 02.03.16.
@@ -202,7 +201,7 @@ public static boolean isHardcodedClientVersionValid() throws IOException, Extrac
202201
* @throws ParsingException
203202
*/
204203
public static String getClientVersion() throws IOException, ExtractionException {
205-
if (clientVersion != null && !clientVersion.isEmpty()) return clientVersion;
204+
if (!isNullOrEmpty(clientVersion)) return clientVersion;
206205
if (isHardcodedClientVersionValid()) return clientVersion = HARDCODED_CLIENT_VERSION;
207206

208207
final String url = "https://www.youtube.com/results?search_query=test";
@@ -245,7 +244,7 @@ public static String getClientVersion() throws IOException, ExtractionException
245244
for (String pattern : patterns) {
246245
try {
247246
contextClientVersion = Parser.matchGroup1(pattern, html);
248-
if (contextClientVersion != null && !contextClientVersion.isEmpty()) {
247+
if (!isNullOrEmpty(contextClientVersion)) {
249248
return clientVersion = contextClientVersion;
250249
}
251250
} catch (Exception ignored) {
@@ -365,7 +364,7 @@ public static String getUrlFromNavigationEndpoint(JsonObject navigationEndpoint)
365364
return "https://www.youtube.com/channel/" + browseId;
366365
}
367366

368-
if (canonicalBaseUrl != null && !canonicalBaseUrl.isEmpty()) {
367+
if (!isNullOrEmpty(canonicalBaseUrl)) {
369368
return "https://www.youtube.com" + canonicalBaseUrl;
370369
}
371370

@@ -392,7 +391,7 @@ public static String getUrlFromNavigationEndpoint(JsonObject navigationEndpoint)
392391
* @return text in the JSON object or {@code null}
393392
*/
394393
public static String getTextFromObject(JsonObject textObject, boolean html) throws ParsingException {
395-
if (textObject == null || textObject.isEmpty()) return null;
394+
if (isNullOrEmpty(textObject)) return null;
396395

397396
if (textObject.has("simpleText")) return textObject.getString("simpleText");
398397

@@ -403,7 +402,7 @@ public static String getTextFromObject(JsonObject textObject, boolean html) thro
403402
String text = ((JsonObject) textPart).getString("text");
404403
if (html && ((JsonObject) textPart).has("navigationEndpoint")) {
405404
String url = getUrlFromNavigationEndpoint(((JsonObject) textPart).getObject("navigationEndpoint"));
406-
if (url != null && !url.isEmpty()) {
405+
if (!isNullOrEmpty(url)) {
407406
textBuilder.append("<a href=\"").append(url).append("\">").append(text).append("</a>");
408407
continue;
409408
}
@@ -497,7 +496,7 @@ public static JsonArray getJsonResponse(final String url, final Localization loc
497496
*/
498497
public static void defaultAlertsCheck(final JsonObject initialData) throws ParsingException {
499498
final JsonArray alerts = initialData.getArray("alerts");
500-
if (!alerts.isEmpty()) {
499+
if (!isNullOrEmpty(alerts)) {
501500
final JsonObject alertRenderer = alerts.getObject(0).getObject("alertRenderer");
502501
final String alertText = getTextFromObject(alertRenderer.getObject("text"));
503502
final String alertType = alertRenderer.getString("type", EMPTY_STRING);

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeChannelExtractor.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.*;
2323
import static org.schabi.newpipe.extractor.utils.JsonUtils.EMPTY_STRING;
24+
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
2425

2526
/*
2627
* Created by Christian Schabesberger on 25.07.16.
@@ -130,7 +131,7 @@ public String getId() throws ParsingException {
130131

131132
if (!channelId.isEmpty()) {
132133
return channelId;
133-
} else if (redirectedChannelId != null && !redirectedChannelId.isEmpty()) {
134+
} else if (!isNullOrEmpty(redirectedChannelId)) {
134135
return redirectedChannelId;
135136
} else {
136137
throw new ParsingException("Could not get channel id");
@@ -244,7 +245,7 @@ public InfoItemsPage<StreamInfoItem> getInitialPage() throws ExtractionException
244245

245246
@Override
246247
public InfoItemsPage<StreamInfoItem> getPage(String pageUrl) throws IOException, ExtractionException {
247-
if (pageUrl == null || pageUrl.isEmpty()) {
248+
if (isNullOrEmpty(pageUrl)) {
248249
throw new ExtractionException(new IllegalArgumentException("Page url is empty or null"));
249250
}
250251

@@ -265,7 +266,7 @@ public InfoItemsPage<StreamInfoItem> getPage(String pageUrl) throws IOException,
265266

266267

267268
private String getNextPageUrlFrom(JsonArray continuations) {
268-
if (continuations == null || continuations.isEmpty()) {
269+
if (isNullOrEmpty(continuations)) {
269270
return "";
270271
}
271272

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsExtractor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.regex.Pattern;
2828

2929
import static java.util.Collections.singletonList;
30+
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
3031

3132

3233
public class YoutubeCommentsExtractor extends CommentsExtractor {
@@ -91,7 +92,7 @@ private String getNextPageUrl(String continuation) throws ParsingException {
9192

9293
@Override
9394
public InfoItemsPage<CommentsInfoItem> getPage(String pageUrl) throws IOException, ExtractionException {
94-
if (pageUrl == null || pageUrl.isEmpty()) {
95+
if (isNullOrEmpty(pageUrl)) {
9596
throw new ExtractionException(new IllegalArgumentException("Page url is empty or null"));
9697
}
9798
String ajaxResponse = makeAjaxRequest(pageUrl);

0 commit comments

Comments
 (0)