Skip to content

Commit

Permalink
changed all == null || isEmpty() to isNullOrEmpty()
Browse files Browse the repository at this point in the history
  • Loading branch information
B0pol committed May 11, 2020
1 parent 202a735 commit de26e00
Show file tree
Hide file tree
Showing 17 changed files with 55 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.IOException;

import static org.schabi.newpipe.extractor.utils.JsonUtils.EMPTY_STRING;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;

@SuppressWarnings("WeakerAccess")
public class SoundcloudChannelExtractor extends ChannelExtractor {
Expand Down Expand Up @@ -132,7 +133,7 @@ private void computeNextPageAndGetStreams() throws ExtractionException {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.io.IOException;

import static org.schabi.newpipe.extractor.ServiceList.SoundCloud;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;

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

@Override
public InfoItemsPage<StreamInfoItem> getPage(String pageUrl) throws IOException, ExtractionException {
if (pageUrl == null || pageUrl.isEmpty()) {
if (isNullOrEmpty(pageUrl)) {
throw new ExtractionException(new IllegalArgumentException("Page url is empty or null"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public String getNextPageUrl() throws IOException, ExtractionException {

@Override
public InfoItemsPage<StreamInfoItem> getPage(String pageUrl) throws IOException, ExtractionException {
if (pageUrl == null || pageUrl.isEmpty()) {
if (isNullOrEmpty(pageUrl)) {
throw new ExtractionException(new IllegalArgumentException("Page url is empty or null"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ public static String getUrlFromNavigationEndpoint(JsonObject navigationEndpoint)
* @return text in the JSON object or {@code null}
*/
public static String getTextFromObject(JsonObject textObject, boolean html) throws ParsingException {
if (textObject == null || textObject.isEmpty()) return null;
if (isNullOrEmpty(textObject)) return null;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public InfoItemsPage<StreamInfoItem> getInitialPage() throws ExtractionException

@Override
public InfoItemsPage<StreamInfoItem> getPage(String pageUrl) throws IOException, ExtractionException {
if (pageUrl == null || pageUrl.isEmpty()) {
if (isNullOrEmpty(pageUrl)) {
throw new ExtractionException(new IllegalArgumentException("Page url is empty or null"));
}

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


private String getNextPageUrlFrom(JsonArray continuations) {
if (continuations == null || continuations.isEmpty()) {
if (isNullOrEmpty(continuations)) {
return "";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.regex.Pattern;

import static java.util.Collections.singletonList;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;


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

@Override
public InfoItemsPage<CommentsInfoItem> getPage(String pageUrl) throws IOException, ExtractionException {
if (pageUrl == null || pageUrl.isEmpty()) {
if (isNullOrEmpty(pageUrl)) {
throw new ExtractionException(new IllegalArgumentException("Page url is empty or null"));
}
String ajaxResponse = makeAjaxRequest(pageUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public String getNextPageUrl() throws ExtractionException, IOException {

@Override
public InfoItemsPage<InfoItem> getPage(final String pageUrl) throws IOException, ExtractionException {
if (pageUrl == null || pageUrl.isEmpty()) {
if (isNullOrEmpty(pageUrl)) {
throw new ExtractionException(new IllegalArgumentException("Page url is empty or null"));
}

Expand Down Expand Up @@ -470,7 +470,7 @@ public long getStreamCount() throws ParsingException {
}

private String getNextPageUrlFrom(final JsonArray continuations) throws ParsingException, IOException, ReCaptchaException {
if (continuations == null || continuations.isEmpty()) {
if (isNullOrEmpty(continuations)) {
return "";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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.services.youtube.YoutubeParsingHelper.getUrlFromNavigationEndpoint;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;

@SuppressWarnings("WeakerAccess")
public class YoutubePlaylistExtractor extends PlaylistExtractor {
Expand Down Expand Up @@ -93,11 +94,11 @@ public String getThumbnailUrl() throws ParsingException {
String url = playlistInfo.getObject("thumbnailRenderer").getObject("playlistVideoThumbnailRenderer")
.getObject("thumbnail").getArray("thumbnails").getObject(0).getString("url");

if (url == null || url.isEmpty()) {
if (isNullOrEmpty(url)) {
url = initialData.getObject("microformat").getObject("microformatDataRenderer").getObject("thumbnail")
.getArray("thumbnails").getObject(0).getString("url");

if (url == null || url.isEmpty()) throw new ParsingException("Could not get playlist thumbnail");
if (isNullOrEmpty(url)) throw new ParsingException("Could not get playlist thumbnail");
}

return fixThumbnailUrl(url);
Expand Down Expand Up @@ -166,7 +167,7 @@ public InfoItemsPage<StreamInfoItem> getInitialPage() {

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

Expand All @@ -182,7 +183,7 @@ public InfoItemsPage<StreamInfoItem> getPage(final String pageUrl) throws IOExce
}

private String getNextPageUrlFrom(JsonArray continuations) {
if (continuations == null || continuations.isEmpty()) {
if (isNullOrEmpty(continuations)) {
return "";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

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;

/*
* Created by Christian Schabesberger on 22.07.2018
Expand Down Expand Up @@ -99,7 +100,7 @@ public String getNextPageUrl() throws ExtractionException {

@Override
public InfoItemsPage<InfoItem> getPage(final String pageUrl) throws IOException, ExtractionException {
if (pageUrl == null || pageUrl.isEmpty()) {
if (isNullOrEmpty(pageUrl)) {
throw new ExtractionException(new IllegalArgumentException("Page url is empty or null"));
}

Expand Down Expand Up @@ -133,7 +134,7 @@ private void collectStreamsFrom(final InfoItemsSearchCollector collector, final
}

private String getNextPageUrlFrom(final JsonArray continuations) throws ParsingException {
if (continuations == null || continuations.isEmpty()) {
if (isNullOrEmpty(continuations)) {
return "";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@

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 06.08.15.
Expand Down Expand Up @@ -116,10 +117,10 @@ public String getName() throws ParsingException {
assertPageFetched();
String title = getTextFromObject(getVideoPrimaryInfoRenderer().getObject("title"));

if (title == null || title.isEmpty()) {
if (isNullOrEmpty(title)) {
title = playerResponse.getObject("videoDetails").getString("title");

if (title == null || title.isEmpty()) throw new ParsingException("Could not get name");
if (isNullOrEmpty(title)) throw new ParsingException("Could not get name");
}

return title;
Expand Down Expand Up @@ -167,7 +168,7 @@ public String getTextualUploadDate() throws ParsingException {
public DateWrapper getUploadDate() throws ParsingException {
final String textualUploadDate = getTextualUploadDate();

if (textualUploadDate == null || textualUploadDate.isEmpty()) {
if (isNullOrEmpty(textualUploadDate)) {
return null;
}

Expand Down Expand Up @@ -204,7 +205,7 @@ public Description getDescription() throws ParsingException {

@Override
public int getAgeLimit() {
if (initialData == null || initialData.isEmpty()) throw new IllegalStateException("initialData is not parsed yet");
if (isNullOrEmpty(initialData)) throw new IllegalStateException("initialData is not parsed yet");

return ageLimit;
}
Expand Down Expand Up @@ -248,10 +249,10 @@ public long getViewCount() throws ParsingException {
String views = getTextFromObject(getVideoPrimaryInfoRenderer().getObject("viewCount")
.getObject("videoViewCountRenderer").getObject("viewCount"));

if (views == null || views.isEmpty()) {
if (isNullOrEmpty(views)) {
views = playerResponse.getObject("videoDetails").getString("viewCount");

if (views == null || views.isEmpty()) throw new ParsingException("Could not get view count");
if (isNullOrEmpty(views)) throw new ParsingException("Could not get view count");
}

if (views.toLowerCase().contains("no views")) return 0;
Expand Down Expand Up @@ -329,10 +330,10 @@ public String getUploaderName() throws ParsingException {
String uploaderName = getTextFromObject(getVideoSecondaryInfoRenderer().getObject("owner")
.getObject("videoOwnerRenderer").getObject("title"));

if (uploaderName == null || uploaderName.isEmpty()) {
if (isNullOrEmpty(uploaderName)) {
uploaderName = playerResponse.getObject("videoDetails").getString("author");

if (uploaderName == null || uploaderName.isEmpty()) throw new ParsingException("Could not get uploader name");
if (isNullOrEmpty(uploaderName)) throw new ParsingException("Could not get uploader name");
}

return uploaderName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ public long getDuration() throws ParsingException {

String duration = getTextFromObject(videoInfo.getObject("lengthText"));

if (duration == null || duration.isEmpty()) {
if (isNullOrEmpty(duration)) {
for (Object thumbnailOverlay : videoInfo.getArray("thumbnailOverlays")) {
if (((JsonObject) thumbnailOverlay).has("thumbnailOverlayTimeStatusRenderer")) {
duration = getTextFromObject(((JsonObject) thumbnailOverlay)
.getObject("thumbnailOverlayTimeStatusRenderer").getObject("text"));
}
}

if (duration == null || duration.isEmpty()) throw new ParsingException("Could not get duration");
if (isNullOrEmpty(duration)) throw new ParsingException("Could not get duration");
}

return YoutubeParsingHelper.parseDurationString(duration);
Expand All @@ -124,13 +124,13 @@ public long getDuration() throws ParsingException {
public String getUploaderName() throws ParsingException {
String name = getTextFromObject(videoInfo.getObject("longBylineText"));

if (name == null || name.isEmpty()) {
if (isNullOrEmpty(name)) {
name = getTextFromObject(videoInfo.getObject("ownerText"));

if (name == null || name.isEmpty()) {
if (isNullOrEmpty(name)) {
name = getTextFromObject(videoInfo.getObject("shortBylineText"));

if (name == null || name.isEmpty()) throw new ParsingException("Could not get uploader name");
if (isNullOrEmpty(name)) throw new ParsingException("Could not get uploader name");
}
}

Expand All @@ -142,15 +142,15 @@ public String getUploaderUrl() throws ParsingException {
String url = getUrlFromNavigationEndpoint(videoInfo.getObject("longBylineText")
.getArray("runs").getObject(0).getObject("navigationEndpoint"));

if (url == null || url.isEmpty()) {
if (isNullOrEmpty(url)) {
url = getUrlFromNavigationEndpoint(videoInfo.getObject("ownerText")
.getArray("runs").getObject(0).getObject("navigationEndpoint"));

if (url == null || url.isEmpty()) {
if (isNullOrEmpty(url)) {
url = getUrlFromNavigationEndpoint(videoInfo.getObject("shortBylineText")
.getArray("runs").getObject(0).getObject("navigationEndpoint"));

if (url == null || url.isEmpty()) throw new ParsingException("Could not get uploader url");
if (isNullOrEmpty(url)) throw new ParsingException("Could not get uploader url");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.io.Serializable;
import java.util.List;

import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;

/**
* Creates a stream object from url, format and optional torrent url
*/
Expand Down Expand Up @@ -61,7 +63,7 @@ public boolean equals(Stream cmp) {
* Check if the list already contains one stream with equals stats
*/
public static boolean containSimilarStream(Stream stream, List<? extends Stream> streamList) {
if (stream == null || streamList == null) return false;
if (isNullOrEmpty(streamList)) return false;
for (Stream cmpStream : streamList) {
if (stream.equalStats(cmpStream)) return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private static StreamInfo extractImportantData(StreamExtractor extractor) throws
String name = extractor.getName();
int ageLimit = extractor.getAgeLimit();

if ((streamType == StreamType.NONE) || (url == null || url.isEmpty()) || (id == null || id.isEmpty())
if ((streamType == StreamType.NONE) || isNullOrEmpty(url) || (isNullOrEmpty(id))
|| (name == null /* streamInfo.title can be empty of course */) || (ageLimit == -1)) {
throw new ExtractionException("Some important stream information was not given.");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.schabi.newpipe.extractor.utils;

import com.grack.nanojson.JsonObject;
import org.schabi.newpipe.extractor.exceptions.ParsingException;

import java.io.UnsupportedEncodingException;
Expand All @@ -8,6 +9,7 @@
import java.net.URLDecoder;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public class Utils {

Expand Down Expand Up @@ -72,7 +74,7 @@ public static long mixedNumberWordToLong(String numberWord) throws NumberFormatE
* @param url the url to be tested
*/
public static void checkUrl(String pattern, String url) throws ParsingException {
if (url == null || url.isEmpty()) {
if (isNullOrEmpty(url)) {
throw new IllegalArgumentException("Url can't be null or empty");
}

Expand Down Expand Up @@ -193,7 +195,13 @@ public static boolean isNullOrEmpty(final String str) {
return str == null || str.isEmpty();
}

// can be used for JsonArrays
public static boolean isNullOrEmpty(final Collection<?> collection) {
return collection == null || collection.isEmpty();
}

// can be used for JsonObjects
public static boolean isNullOrEmpty(final Map map) {
return map == null || map.isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import static org.junit.Assert.assertEquals;
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertEmpty;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;

public abstract class DefaultSearchExtractorTest extends DefaultListExtractorTest<SearchExtractor>
implements BaseSearchExtractorTest {
Expand All @@ -25,7 +26,7 @@ public void testSearchString() throws Exception {
@Override
public void testSearchSuggestion() throws Exception {
final String expectedSearchSuggestion = expectedSearchSuggestion();
if (expectedSearchSuggestion == null || expectedSearchSuggestion.isEmpty()) {
if (isNullOrEmpty(expectedSearchSuggestion)) {
assertEmpty("Suggestion was expected to be empty", extractor().getSearchSuggestion());
} else {
assertEquals(expectedSearchSuggestion, extractor().getSearchSuggestion());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static void assertOnlyContainsType(ListExtractor.InfoItemsPage<? extends
public static <T extends InfoItem> void assertNoMoreItems(ListExtractor<T> extractor) throws Exception {
assertFalse("More items available when it shouldn't", extractor.hasNextPage());
final String nextPageUrl = extractor.getNextPageUrl();
assertTrue("Next page is not empty or null", nextPageUrl == null || nextPageUrl.isEmpty());
assertTrue("Next page is not empty or null", isNullOrEmpty(nextPageUrl));
}

public static void assertNoDuplicatedItems(StreamingService expectedService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.schabi.newpipe.extractor.ServiceList.YouTube;
import static org.schabi.newpipe.extractor.services.DefaultTests.assertNoDuplicatedItems;
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.*;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;

public class YoutubeSearchExtractorTest {
public static class All extends DefaultSearchExtractorTest {
Expand Down Expand Up @@ -170,7 +171,7 @@ public void testMoreRelatedItems() throws Exception {

assertFalse("More items available when it shouldn't", nextEmptyPage.hasNextPage());
final String nextPageUrl = nextEmptyPage.getNextPageUrl();
assertTrue("Next page is not empty or null", nextPageUrl == null || nextPageUrl.isEmpty());
assertTrue("Next page is not empty or null", isNullOrEmpty(nextPageUrl));
}
}

Expand Down

0 comments on commit de26e00

Please sign in to comment.