From 202a73516c55f76c6ef6967ddbfa32ee09e3a5e4 Mon Sep 17 00:00:00 2001 From: bopol Date: Wed, 15 Apr 2020 18:49:58 +0200 Subject: [PATCH] refactor: add Utils.isNullOrEmpty() --- .../newpipe/extractor/ListExtractor.java | 8 +++--- .../schabi/newpipe/extractor/ListInfo.java | 4 ++- .../soundcloud/SoundcloudParsingHelper.java | 6 +++-- .../SoundcloudPlaylistExtractor.java | 4 ++- .../extractors/SoundcloudStreamExtractor.java | 3 ++- .../youtube/YoutubeParsingHelper.java | 13 +++++---- .../extractors/YoutubeChannelExtractor.java | 3 ++- .../YoutubeCommentsInfoItemExtractor.java | 2 ++ .../YoutubeMusicSearchExtractor.java | 27 ++++++++++--------- .../YoutubeStreamInfoItemExtractor.java | 5 ++-- .../extractors/YoutubeTrendingExtractor.java | 3 ++- .../newpipe/extractor/stream/StreamInfo.java | 4 ++- .../schabi/newpipe/extractor/utils/Utils.java | 12 ++++++++- .../extractor/services/DefaultTests.java | 17 +++++++----- 14 files changed, 70 insertions(+), 41 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/ListExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/ListExtractor.java index 0e21f04b8..aae591eb6 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/ListExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/ListExtractor.java @@ -2,6 +2,7 @@ package org.schabi.newpipe.extractor; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler; +import org.schabi.newpipe.extractor.utils.Utils; import java.io.IOException; import java.util.Collections; @@ -9,6 +10,8 @@ import java.util.List; import javax.annotation.Nonnull; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; + /** * Base class to extractors that have a list (e.g. playlists, users). */ @@ -63,8 +66,7 @@ public abstract class ListExtractor extends Extractor { public abstract InfoItemsPage getPage(final String pageUrl) throws IOException, ExtractionException; public boolean hasNextPage() throws IOException, ExtractionException { - final String nextPageUrl = getNextPageUrl(); - return nextPageUrl != null && !nextPageUrl.isEmpty(); + return !isNullOrEmpty(getNextPageUrl()); } @Override @@ -123,7 +125,7 @@ public abstract class ListExtractor extends Extractor { } public boolean hasNextPage() { - return nextPageUrl != null && !nextPageUrl.isEmpty(); + return !isNullOrEmpty(nextPageUrl); } public List getItems() { diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/ListInfo.java b/extractor/src/main/java/org/schabi/newpipe/extractor/ListInfo.java index 1cb42e5da..38177e1e4 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/ListInfo.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/ListInfo.java @@ -4,6 +4,8 @@ import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler; import java.util.List; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; + public abstract class ListInfo extends Info { private List relatedItems; private String nextPageUrl = null; @@ -37,7 +39,7 @@ public abstract class ListInfo extends Info { } public boolean hasNextPage() { - return nextPageUrl != null && !nextPageUrl.isEmpty(); + return !isNullOrEmpty(nextPageUrl); } public String getNextPageUrl() { diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudParsingHelper.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudParsingHelper.java index b24d4bf85..b8dafd834 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudParsingHelper.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudParsingHelper.java @@ -21,6 +21,7 @@ import org.schabi.newpipe.extractor.services.soundcloud.extractors.SoundcloudStr import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector; import org.schabi.newpipe.extractor.utils.Parser; import org.schabi.newpipe.extractor.utils.Parser.RegexException; +import org.schabi.newpipe.extractor.utils.Utils; import javax.annotation.Nonnull; import java.io.IOException; @@ -32,6 +33,7 @@ import java.util.*; import static java.util.Collections.singletonList; import static org.schabi.newpipe.extractor.ServiceList.SoundCloud; import static org.schabi.newpipe.extractor.utils.JsonUtils.EMPTY_STRING; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; import static org.schabi.newpipe.extractor.utils.Utils.replaceHttpWithHttps; public class SoundcloudParsingHelper { @@ -42,7 +44,7 @@ public class SoundcloudParsingHelper { } public static String clientId() throws ExtractionException, IOException { - if (clientId != null && !clientId.isEmpty()) return clientId; + if (!isNullOrEmpty(clientId)) return clientId; Downloader dl = NewPipe.getDownloader(); clientId = HARDCODED_CLIENT_ID; @@ -64,7 +66,7 @@ public class SoundcloudParsingHelper { for (Element element : possibleScripts) { final String srcUrl = element.attr("src"); - if (srcUrl != null && !srcUrl.isEmpty()) { + if (!isNullOrEmpty(srcUrl)) { try { return clientId = Parser.matchGroup1(clientIdPattern, dl.get(srcUrl, headers).responseBody()); } catch (RegexException ignored) { diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistExtractor.java index a529556ee..fc3910a83 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistExtractor.java @@ -21,6 +21,8 @@ import java.io.IOException; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; + @SuppressWarnings("WeakerAccess") public class SoundcloudPlaylistExtractor extends PlaylistExtractor { private static final int streamsPerRequestedPage = 15; @@ -76,7 +78,7 @@ public class SoundcloudPlaylistExtractor extends PlaylistExtractor { for (StreamInfoItem item : infoItems.getItems()) { artworkUrl = item.getThumbnailUrl(); - if (artworkUrl != null && !artworkUrl.isEmpty()) break; + if (!isNullOrEmpty(artworkUrl)) break; } } catch (Exception ignored) { } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudStreamExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudStreamExtractor.java index beee2c3ef..431baff94 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudStreamExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudStreamExtractor.java @@ -36,6 +36,7 @@ import java.util.Locale; import javax.annotation.Nonnull; import static org.schabi.newpipe.extractor.utils.JsonUtils.EMPTY_STRING; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; public class SoundcloudStreamExtractor extends StreamExtractor { private JsonObject track; @@ -191,7 +192,7 @@ public class SoundcloudStreamExtractor extends StreamExtractor { JsonObject t = (JsonObject) transcoding; String url = t.getString("url"); - if (url != null && !url.isEmpty()) { + if (!isNullOrEmpty(url)) { // We can only play the mp3 format, but not handle m3u playlists / streams. // what about Opus? diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelper.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelper.java index a630ae517..02a528fbe 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelper.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelper.java @@ -29,8 +29,7 @@ import java.util.*; import static org.schabi.newpipe.extractor.NewPipe.getDownloader; import static org.schabi.newpipe.extractor.utils.JsonUtils.EMPTY_STRING; -import static org.schabi.newpipe.extractor.utils.Utils.HTTP; -import static org.schabi.newpipe.extractor.utils.Utils.HTTPS; +import static org.schabi.newpipe.extractor.utils.Utils.*; /* * Created by Christian Schabesberger on 02.03.16. @@ -202,7 +201,7 @@ public class YoutubeParsingHelper { * @throws ParsingException */ public static String getClientVersion() throws IOException, ExtractionException { - if (clientVersion != null && !clientVersion.isEmpty()) return clientVersion; + if (!isNullOrEmpty(clientVersion)) return clientVersion; if (isHardcodedClientVersionValid()) return clientVersion = HARDCODED_CLIENT_VERSION; final String url = "https://www.youtube.com/results?search_query=test"; @@ -245,7 +244,7 @@ public class YoutubeParsingHelper { for (String pattern : patterns) { try { contextClientVersion = Parser.matchGroup1(pattern, html); - if (contextClientVersion != null && !contextClientVersion.isEmpty()) { + if (!isNullOrEmpty(contextClientVersion)) { return clientVersion = contextClientVersion; } } catch (Exception ignored) { @@ -365,7 +364,7 @@ public class YoutubeParsingHelper { return "https://www.youtube.com/channel/" + browseId; } - if (canonicalBaseUrl != null && !canonicalBaseUrl.isEmpty()) { + if (!isNullOrEmpty(canonicalBaseUrl)) { return "https://www.youtube.com" + canonicalBaseUrl; } @@ -403,7 +402,7 @@ public class YoutubeParsingHelper { String text = ((JsonObject) textPart).getString("text"); if (html && ((JsonObject) textPart).has("navigationEndpoint")) { String url = getUrlFromNavigationEndpoint(((JsonObject) textPart).getObject("navigationEndpoint")); - if (url != null && !url.isEmpty()) { + if (!isNullOrEmpty(url)) { textBuilder.append("").append(text).append(""); continue; } @@ -497,7 +496,7 @@ public class YoutubeParsingHelper { */ public static void defaultAlertsCheck(final JsonObject initialData) throws ParsingException { final JsonArray alerts = initialData.getArray("alerts"); - if (!alerts.isEmpty()) { + if (!isNullOrEmpty(alerts)) { final JsonObject alertRenderer = alerts.getObject(0).getObject("alertRenderer"); final String alertText = getTextFromObject(alertRenderer.getObject("text")); final String alertType = alertRenderer.getString("type", EMPTY_STRING); diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeChannelExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeChannelExtractor.java index 0e5329425..d9771240f 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeChannelExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeChannelExtractor.java @@ -21,6 +21,7 @@ import java.io.IOException; import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.*; import static org.schabi.newpipe.extractor.utils.JsonUtils.EMPTY_STRING; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; /* * Created by Christian Schabesberger on 25.07.16. @@ -130,7 +131,7 @@ public class YoutubeChannelExtractor extends ChannelExtractor { if (!channelId.isEmpty()) { return channelId; - } else if (redirectedChannelId != null && !redirectedChannelId.isEmpty()) { + } else if (!isNullOrEmpty(redirectedChannelId)) { return redirectedChannelId; } else { throw new ParsingException("Could not get channel id"); diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsInfoItemExtractor.java index b012be7cc..9d659f5d3 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsInfoItemExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsInfoItemExtractor.java @@ -11,6 +11,8 @@ import org.schabi.newpipe.extractor.utils.Utils; import javax.annotation.Nullable; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; + public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtractor { private final JsonObject json; diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeMusicSearchExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeMusicSearchExtractor.java index 7e952c94b..6159ab43a 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeMusicSearchExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeMusicSearchExtractor.java @@ -35,6 +35,7 @@ import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeS import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_SONGS; import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_VIDEOS; import static org.schabi.newpipe.extractor.utils.JsonUtils.EMPTY_STRING; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; public class YoutubeMusicSearchExtractor extends SearchExtractor { private JsonObject initialData; @@ -238,7 +239,7 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor { @Override public String getUrl() throws ParsingException { final String url = getUrlFromNavigationEndpoint(info.getObject("doubleTapCommand")); - if (url != null && !url.isEmpty()) { + if (!isNullOrEmpty(url)) { return url; } throw new ParsingException("Could not get url"); @@ -248,7 +249,7 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor { public String getName() throws ParsingException { final String name = getTextFromObject(info.getArray("flexColumns").getObject(0) .getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text")); - if (name != null && !name.isEmpty()) { + if (!isNullOrEmpty(name)) { return name; } throw new ParsingException("Could not get name"); @@ -258,7 +259,7 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor { public long getDuration() throws ParsingException { final String duration = getTextFromObject(info.getArray("flexColumns").getObject(3) .getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text")); - if (duration != null && !duration.isEmpty()) { + if (!isNullOrEmpty(duration)) { return YoutubeParsingHelper.parseDurationString(duration); } throw new ParsingException("Could not get duration"); @@ -268,7 +269,7 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor { public String getUploaderName() throws ParsingException { final String name = getTextFromObject(info.getArray("flexColumns").getObject(1) .getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text")); - if (name != null && !name.isEmpty()) { + if (!isNullOrEmpty(name)) { return name; } throw new ParsingException("Could not get uploader name"); @@ -295,7 +296,7 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor { final String url = getUrlFromNavigationEndpoint(navigationEndpointHolder.getObject("navigationEndpoint")); - if (url != null && !url.isEmpty()) { + if (!isNullOrEmpty(url)) { return url; } @@ -320,7 +321,7 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor { } final String viewCount = getTextFromObject(info.getArray("flexColumns").getObject(2) .getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text")); - if (viewCount != null && !viewCount.isEmpty()) { + if (!isNullOrEmpty(viewCount)) { return Utils.mixedNumberWordToLong(viewCount); } throw new ParsingException("Could not get view count"); @@ -360,7 +361,7 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor { public String getName() throws ParsingException { final String name = getTextFromObject(info.getArray("flexColumns").getObject(0) .getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text")); - if (name != null && !name.isEmpty()) { + if (!isNullOrEmpty(name)) { return name; } throw new ParsingException("Could not get name"); @@ -369,7 +370,7 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor { @Override public String getUrl() throws ParsingException { final String url = getUrlFromNavigationEndpoint(info.getObject("navigationEndpoint")); - if (url != null && !url.isEmpty()) { + if (!isNullOrEmpty(url)) { return url; } throw new ParsingException("Could not get url"); @@ -379,7 +380,7 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor { public long getSubscriberCount() throws ParsingException { final String viewCount = getTextFromObject(info.getArray("flexColumns").getObject(2) .getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text")); - if (viewCount != null && !viewCount.isEmpty()) { + if (!isNullOrEmpty(viewCount)) { return Utils.mixedNumberWordToLong(viewCount); } throw new ParsingException("Could not get subscriber count"); @@ -415,7 +416,7 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor { public String getName() throws ParsingException { final String name = getTextFromObject(info.getArray("flexColumns").getObject(0) .getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text")); - if (name != null && !name.isEmpty()) { + if (!isNullOrEmpty(name)) { return name; } throw new ParsingException("Could not get name"); @@ -424,7 +425,7 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor { @Override public String getUrl() throws ParsingException { final String url = getUrlFromNavigationEndpoint(info.getObject("doubleTapCommand")); - if (url != null && !url.isEmpty()) { + if (!isNullOrEmpty(url)) { return url; } throw new ParsingException("Could not get url"); @@ -440,7 +441,7 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor { name = getTextFromObject(info.getArray("flexColumns").getObject(1) .getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text")); } - if (name != null && !name.isEmpty()) { + if (!isNullOrEmpty(name)) { return name; } throw new ParsingException("Could not get uploader name"); @@ -453,7 +454,7 @@ public class YoutubeMusicSearchExtractor extends SearchExtractor { } final String count = getTextFromObject(info.getArray("flexColumns").getObject(2) .getObject("musicResponsiveListItemFlexColumnRenderer").getObject("text")); - if (count != null && !count.isEmpty()) { + if (!isNullOrEmpty(count)) { if (count.contains("100+")) { return ITEM_COUNT_MORE_THAN_100; } else { diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamInfoItemExtractor.java index a2d294bb7..d115fdeed 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamInfoItemExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamInfoItemExtractor.java @@ -18,6 +18,7 @@ import java.util.Date; import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.*; import static org.schabi.newpipe.extractor.utils.JsonUtils.EMPTY_STRING; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; /* * Copyright (C) Christian Schabesberger 2016 @@ -93,7 +94,7 @@ public class YoutubeStreamInfoItemExtractor implements StreamInfoItemExtractor { @Override public String getName() throws ParsingException { String name = getTextFromObject(videoInfo.getObject("title")); - if (name != null && !name.isEmpty()) return name; + if (!isNullOrEmpty(name)) return name; throw new ParsingException("Could not get name"); } @@ -186,7 +187,7 @@ public class YoutubeStreamInfoItemExtractor implements StreamInfoItemExtractor { } final String textualUploadDate = getTextualUploadDate(); - if (timeAgoParser != null && textualUploadDate != null && !textualUploadDate.isEmpty()) { + if (timeAgoParser != null && !isNullOrEmpty(textualUploadDate)) { try { return timeAgoParser.parse(textualUploadDate); } catch (ParsingException e) { diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeTrendingExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeTrendingExtractor.java index 87b5561c3..ddd108e87 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeTrendingExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeTrendingExtractor.java @@ -39,6 +39,7 @@ import javax.annotation.Nonnull; import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getJsonResponse; import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; public class YoutubeTrendingExtractor extends KioskExtractor { private JsonObject initialData; @@ -73,7 +74,7 @@ public class YoutubeTrendingExtractor extends KioskExtractor { @Override public String getName() throws ParsingException { String name = getTextFromObject(initialData.getObject("header").getObject("feedTabbedHeaderRenderer").getObject("title")); - if (name != null && !name.isEmpty()) { + if (!isNullOrEmpty(name)) { return name; } throw new ParsingException("Could not get Trending name"); diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfo.java b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfo.java index aa1001b44..6ee60ce8e 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfo.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfo.java @@ -16,6 +16,8 @@ import java.util.ArrayList; import java.util.List; import java.util.Locale; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; + /* * Created by Christian Schabesberger on 26.08.15. * @@ -159,7 +161,7 @@ public class StreamInfo extends Info { streamInfo.setAudioStreams(new ArrayList()); Exception dashMpdError = null; - if (streamInfo.getDashMpdUrl() != null && !streamInfo.getDashMpdUrl().isEmpty()) { + if (!isNullOrEmpty(streamInfo.getDashMpdUrl())) { try { DashMpdParser.ParserResult result = DashMpdParser.getStreams(streamInfo); streamInfo.getVideoOnlyStreams().addAll(result.getVideoOnlyStreams()); diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/utils/Utils.java b/extractor/src/main/java/org/schabi/newpipe/extractor/utils/Utils.java index 76aa2944f..3029fdd25 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/utils/Utils.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/utils/Utils.java @@ -6,6 +6,7 @@ import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLDecoder; +import java.util.Collection; import java.util.List; public class Utils { @@ -48,7 +49,8 @@ public class Utils { String multiplier = ""; try { multiplier = Parser.matchGroup("[\\d]+([\\.,][\\d]+)?([KMBkmb])+", numberWord, 2); - } catch(ParsingException ignored) {} + } catch (ParsingException ignored) { + } double count = Double.parseDouble(Parser.matchGroup1("([\\d]+([\\.,][\\d]+)?)", numberWord) .replace(",", ".")); switch (multiplier.toUpperCase()) { @@ -186,4 +188,12 @@ public class Utils { } return uri.getProtocol() + "://" + uri.getAuthority(); } + + public static boolean isNullOrEmpty(final String str) { + return str == null || str.isEmpty(); + } + + public static boolean isNullOrEmpty(final Collection collection) { + return collection == null || collection.isEmpty(); + } } \ No newline at end of file diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultTests.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultTests.java index a52698415..eaff6acba 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultTests.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultTests.java @@ -18,6 +18,7 @@ import static junit.framework.TestCase.assertFalse; import static org.junit.Assert.*; import static org.schabi.newpipe.extractor.ExtractorAsserts.*; import static org.schabi.newpipe.extractor.StreamingService.LinkType; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; public final class DefaultTests { public static void defaultTestListOfItems(StreamingService expectedService, List itemsList, List errors) throws ParsingException { @@ -27,8 +28,10 @@ public final class DefaultTests { for (InfoItem item : itemsList) { assertIsSecureUrl(item.getUrl()); - if (item.getThumbnailUrl() != null && !item.getThumbnailUrl().isEmpty()) { - assertIsSecureUrl(item.getThumbnailUrl()); + + final String thumbnailUrl = item.getThumbnailUrl(); + if (!isNullOrEmpty(thumbnailUrl)) { + assertIsSecureUrl(thumbnailUrl); } assertNotNull("InfoItem type not set: " + item, item.getInfoType()); assertEquals("Unexpected item service id", expectedService.getServiceId(), item.getServiceId()); @@ -39,15 +42,15 @@ public final class DefaultTests { assertNotEmpty("Uploader name not set: " + item, streamInfoItem.getUploaderName()); // assertNotEmpty("Uploader url not set: " + item, streamInfoItem.getUploaderUrl()); - if (streamInfoItem.getUploaderUrl() != null && !streamInfoItem.getUploaderUrl().isEmpty()) { - assertIsSecureUrl(streamInfoItem.getUploaderUrl()); - assertExpectedLinkType(expectedService, streamInfoItem.getUploaderUrl(), LinkType.CHANNEL); + final String uploaderUrl = streamInfoItem.getUploaderUrl(); + if (!isNullOrEmpty(uploaderUrl)) { + assertIsSecureUrl(uploaderUrl); + assertExpectedLinkType(expectedService, uploaderUrl, LinkType.CHANNEL); } assertExpectedLinkType(expectedService, streamInfoItem.getUrl(), LinkType.STREAM); - final String textualUploadDate = streamInfoItem.getTextualUploadDate(); - if (textualUploadDate != null && !textualUploadDate.isEmpty()) { + if (!isNullOrEmpty(streamInfoItem.getTextualUploadDate())) { final DateWrapper uploadDate = streamInfoItem.getUploadDate(); assertNotNull("No parsed upload date", uploadDate); assertTrue("Upload date not in the past", uploadDate.date().before(Calendar.getInstance()));