Skip to content

Commit 202a735

Browse files
committed
refactor: add Utils.isNullOrEmpty()
1 parent 3cae32b commit 202a735

14 files changed

Lines changed: 70 additions & 41 deletions

File tree

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/SoundcloudPlaylistExtractor.java

Lines changed: 3 additions & 1 deletion
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
}

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: 6 additions & 7 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

@@ -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: 2 additions & 1 deletion
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");

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
import javax.annotation.Nullable;
1313

14+
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
15+
1416
public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtractor {
1517

1618
private final JsonObject json;

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_SONGS;
3636
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_VIDEOS;
3737
import static org.schabi.newpipe.extractor.utils.JsonUtils.EMPTY_STRING;
38+
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
3839

3940
public class YoutubeMusicSearchExtractor extends SearchExtractor {
4041
private JsonObject initialData;
@@ -238,7 +239,7 @@ private void collectMusicStreamsFrom(final InfoItemsSearchCollector collector, f
238239
@Override
239240
public String getUrl() throws ParsingException {
240241
final String url = getUrlFromNavigationEndpoint(info.getObject("doubleTapCommand"));
241-
if (url != null && !url.isEmpty()) {
242+
if (!isNullOrEmpty(url)) {
242243
return url;
243244
}
244245
throw new ParsingException("Could not get url");
@@ -248,7 +249,7 @@ public String getUrl() throws ParsingException {
248249
public String getName() throws ParsingException {
249250
final String name = getTextFromObject(info.getArray("flexColumns").getObject(0)
250251
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
251-
if (name != null && !name.isEmpty()) {
252+
if (!isNullOrEmpty(name)) {
252253
return name;
253254
}
254255
throw new ParsingException("Could not get name");
@@ -258,7 +259,7 @@ public String getName() throws ParsingException {
258259
public long getDuration() throws ParsingException {
259260
final String duration = getTextFromObject(info.getArray("flexColumns").getObject(3)
260261
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
261-
if (duration != null && !duration.isEmpty()) {
262+
if (!isNullOrEmpty(duration)) {
262263
return YoutubeParsingHelper.parseDurationString(duration);
263264
}
264265
throw new ParsingException("Could not get duration");
@@ -268,7 +269,7 @@ public long getDuration() throws ParsingException {
268269
public String getUploaderName() throws ParsingException {
269270
final String name = getTextFromObject(info.getArray("flexColumns").getObject(1)
270271
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
271-
if (name != null && !name.isEmpty()) {
272+
if (!isNullOrEmpty(name)) {
272273
return name;
273274
}
274275
throw new ParsingException("Could not get uploader name");
@@ -295,7 +296,7 @@ public String getUploaderUrl() throws ParsingException {
295296

296297
final String url = getUrlFromNavigationEndpoint(navigationEndpointHolder.getObject("navigationEndpoint"));
297298

298-
if (url != null && !url.isEmpty()) {
299+
if (!isNullOrEmpty(url)) {
299300
return url;
300301
}
301302

@@ -320,7 +321,7 @@ public long getViewCount() throws ParsingException {
320321
}
321322
final String viewCount = getTextFromObject(info.getArray("flexColumns").getObject(2)
322323
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
323-
if (viewCount != null && !viewCount.isEmpty()) {
324+
if (!isNullOrEmpty(viewCount)) {
324325
return Utils.mixedNumberWordToLong(viewCount);
325326
}
326327
throw new ParsingException("Could not get view count");
@@ -360,7 +361,7 @@ public String getThumbnailUrl() throws ParsingException {
360361
public String getName() throws ParsingException {
361362
final String name = getTextFromObject(info.getArray("flexColumns").getObject(0)
362363
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
363-
if (name != null && !name.isEmpty()) {
364+
if (!isNullOrEmpty(name)) {
364365
return name;
365366
}
366367
throw new ParsingException("Could not get name");
@@ -369,7 +370,7 @@ public String getName() throws ParsingException {
369370
@Override
370371
public String getUrl() throws ParsingException {
371372
final String url = getUrlFromNavigationEndpoint(info.getObject("navigationEndpoint"));
372-
if (url != null && !url.isEmpty()) {
373+
if (!isNullOrEmpty(url)) {
373374
return url;
374375
}
375376
throw new ParsingException("Could not get url");
@@ -379,7 +380,7 @@ public String getUrl() throws ParsingException {
379380
public long getSubscriberCount() throws ParsingException {
380381
final String viewCount = getTextFromObject(info.getArray("flexColumns").getObject(2)
381382
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
382-
if (viewCount != null && !viewCount.isEmpty()) {
383+
if (!isNullOrEmpty(viewCount)) {
383384
return Utils.mixedNumberWordToLong(viewCount);
384385
}
385386
throw new ParsingException("Could not get subscriber count");
@@ -415,7 +416,7 @@ public String getThumbnailUrl() throws ParsingException {
415416
public String getName() throws ParsingException {
416417
final String name = getTextFromObject(info.getArray("flexColumns").getObject(0)
417418
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
418-
if (name != null && !name.isEmpty()) {
419+
if (!isNullOrEmpty(name)) {
419420
return name;
420421
}
421422
throw new ParsingException("Could not get name");
@@ -424,7 +425,7 @@ public String getName() throws ParsingException {
424425
@Override
425426
public String getUrl() throws ParsingException {
426427
final String url = getUrlFromNavigationEndpoint(info.getObject("doubleTapCommand"));
427-
if (url != null && !url.isEmpty()) {
428+
if (!isNullOrEmpty(url)) {
428429
return url;
429430
}
430431
throw new ParsingException("Could not get url");
@@ -440,7 +441,7 @@ public String getUploaderName() throws ParsingException {
440441
name = getTextFromObject(info.getArray("flexColumns").getObject(1)
441442
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
442443
}
443-
if (name != null && !name.isEmpty()) {
444+
if (!isNullOrEmpty(name)) {
444445
return name;
445446
}
446447
throw new ParsingException("Could not get uploader name");
@@ -453,7 +454,7 @@ public long getStreamCount() throws ParsingException {
453454
}
454455
final String count = getTextFromObject(info.getArray("flexColumns").getObject(2)
455456
.getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text"));
456-
if (count != null && !count.isEmpty()) {
457+
if (!isNullOrEmpty(count)) {
457458
if (count.contains("100+")) {
458459
return ITEM_COUNT_MORE_THAN_100;
459460
} else {

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

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

1919
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.*;
2020
import static org.schabi.newpipe.extractor.utils.JsonUtils.EMPTY_STRING;
21+
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
2122

2223
/*
2324
* Copyright (C) Christian Schabesberger 2016 <chris.schabesberger@mailbox.org>
@@ -93,7 +94,7 @@ public String getUrl() throws ParsingException {
9394
@Override
9495
public String getName() throws ParsingException {
9596
String name = getTextFromObject(videoInfo.getObject("title"));
96-
if (name != null && !name.isEmpty()) return name;
97+
if (!isNullOrEmpty(name)) return name;
9798
throw new ParsingException("Could not get name");
9899
}
99100

@@ -186,7 +187,7 @@ public DateWrapper getUploadDate() throws ParsingException {
186187
}
187188

188189
final String textualUploadDate = getTextualUploadDate();
189-
if (timeAgoParser != null && textualUploadDate != null && !textualUploadDate.isEmpty()) {
190+
if (timeAgoParser != null && !isNullOrEmpty(textualUploadDate)) {
190191
try {
191192
return timeAgoParser.parse(textualUploadDate);
192193
} catch (ParsingException e) {

0 commit comments

Comments
 (0)