diff --git a/app/src/main/java/org/schabi/newpipe/util/ExtractorHelper.java b/app/src/main/java/org/schabi/newpipe/util/ExtractorHelper.java index f31ee0131..0f1c568e7 100644 --- a/app/src/main/java/org/schabi/newpipe/util/ExtractorHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/ExtractorHelper.java @@ -57,105 +57,96 @@ public final class ExtractorHelper { } } - public static Single searchFor(final int serviceId, final String query, final int pageNumber, final String contentCountry, final SearchEngine.Filter filter) { + public static Single searchFor(final int serviceId, + final String query, + final int pageNumber, + final String contentCountry, + final SearchEngine.Filter filter) { checkServiceId(serviceId); - return Single.fromCallable(new Callable() { - @Override - public SearchResult call() throws Exception { - return SearchResult.getSearchResult(NewPipe.getService(serviceId).getSearchEngine(), - query, pageNumber, contentCountry, filter); - } - }); + return Single.fromCallable(() -> + SearchResult.getSearchResult(NewPipe.getService(serviceId).getSearchEngine(), + query, pageNumber, contentCountry, filter) + ); } - public static Single getMoreSearchItems(final int serviceId, final String query, final int nextPageNumber, final String searchLanguage, final SearchEngine.Filter filter) { + public static Single getMoreSearchItems(final int serviceId, + final String query, + final int nextPageNumber, + final String searchLanguage, + final SearchEngine.Filter filter) { checkServiceId(serviceId); return searchFor(serviceId, query, nextPageNumber, searchLanguage, filter) - .map(new Function() { - @Override - public NextItemsResult apply(@NonNull SearchResult searchResult) throws Exception { - return new NextItemsResult(searchResult.resultList, nextPageNumber + "", searchResult.errors); - } - }); + .map((@NonNull SearchResult searchResult) -> + new NextItemsResult(searchResult.resultList, + nextPageNumber + "", + searchResult.errors)); } - public static Single> suggestionsFor(final int serviceId, final String query, final String contentCountry) { + public static Single> suggestionsFor(final int serviceId, + final String query, + final String contentCountry) { checkServiceId(serviceId); - return Single.fromCallable(new Callable>() { - @Override - public List call() throws Exception { - return NewPipe.getService(serviceId).getSuggestionExtractor().suggestionList(query, contentCountry); - } - }); + return Single.fromCallable(() -> + NewPipe.getService(serviceId) + .getSuggestionExtractor() + .suggestionList(query, contentCountry)); } - public static Single getStreamInfo(final int serviceId, final String url, boolean forceLoad) { + public static Single getStreamInfo(final int serviceId, + final String url, + boolean forceLoad) { checkServiceId(serviceId); - return checkCache(forceLoad, serviceId, url, Single.fromCallable(new Callable() { - @Override - public StreamInfo call() throws Exception { - return StreamInfo.getInfo(NewPipe.getService(serviceId), url); - } - })); + return checkCache(forceLoad, serviceId, url, Single.fromCallable(() -> + StreamInfo.getInfo(NewPipe.getService(serviceId), url))); } - public static Single getChannelInfo(final int serviceId, final String url, boolean forceLoad) { + public static Single getChannelInfo(final int serviceId, + final String url, + boolean forceLoad) { checkServiceId(serviceId); - return checkCache(forceLoad, serviceId, url, Single.fromCallable(new Callable() { - @Override - public ChannelInfo call() throws Exception { - return ChannelInfo.getInfo(NewPipe.getService(serviceId), url); - } - })); + return checkCache(forceLoad, serviceId, url, Single.fromCallable(() -> + ChannelInfo.getInfo(NewPipe.getService(serviceId), url))); } - public static Single getMoreChannelItems(final int serviceId, final String url, final String nextStreamsUrl) { + public static Single getMoreChannelItems(final int serviceId, + final String url, + final String nextStreamsUrl) { checkServiceId(serviceId); - return Single.fromCallable(new Callable() { - @Override - public NextItemsResult call() throws Exception { - return ChannelInfo.getMoreItems(NewPipe.getService(serviceId), url, nextStreamsUrl); - } - }); + return Single.fromCallable(() -> + ChannelInfo.getMoreItems(NewPipe.getService(serviceId), url, nextStreamsUrl)); } - public static Single getPlaylistInfo(final int serviceId, final String url, boolean forceLoad) { + public static Single getPlaylistInfo(final int serviceId, + final String url, + boolean forceLoad) { checkServiceId(serviceId); - return checkCache(forceLoad, serviceId, url, Single.fromCallable(new Callable() { - @Override - public PlaylistInfo call() throws Exception { - return PlaylistInfo.getInfo(NewPipe.getService(serviceId), url); - } - })); + return checkCache(forceLoad, serviceId, url, Single.fromCallable(() -> + PlaylistInfo.getInfo(NewPipe.getService(serviceId), url))); } - public static Single getMorePlaylistItems(final int serviceId, final String url, final String nextStreamsUrl) { + public static Single getMorePlaylistItems(final int serviceId, + final String url, + final String nextStreamsUrl) { checkServiceId(serviceId); - return Single.fromCallable(new Callable() { - @Override - public NextItemsResult call() throws Exception { - return PlaylistInfo.getMoreItems(NewPipe.getService(serviceId), url, nextStreamsUrl); - } - }); + return Single.fromCallable(() -> + PlaylistInfo.getMoreItems(NewPipe.getService(serviceId), url, nextStreamsUrl)); } - public static Single getKioskInfo(final int serviceId, final String url, final String contentCountry, boolean forceLoad) { - return checkCache(forceLoad, serviceId, url, Single.fromCallable(new Callable() { - @Override - public KioskInfo call() throws Exception { - Log.e("---------", contentCountry); - return KioskInfo.getInfo(NewPipe.getService(serviceId), url, contentCountry); - } - })); + public static Single getKioskInfo(final int serviceId, + final String url, + final String contentCountry, + boolean forceLoad) { + return checkCache(forceLoad, serviceId, url, Single.fromCallable(() -> + KioskInfo.getInfo(NewPipe.getService(serviceId), url, contentCountry))); } - public static Single getMoreKioskItems(final int serviceId, final String url, final String nextStreamsUrl, final String contentCountry) { - return Single.fromCallable(new Callable() { - @Override - public NextItemsResult call() throws Exception { - return KioskInfo.getMoreItems(NewPipe.getService(serviceId), url, nextStreamsUrl, contentCountry); - } - }); + public static Single getMoreKioskItems(final int serviceId, + final String url, + final String nextStreamsUrl, + final String contentCountry) { + return Single.fromCallable(() -> + KioskInfo.getMoreItems(NewPipe.getService(serviceId), + url, nextStreamsUrl, contentCountry)); } /*////////////////////////////////////////////////////////////////////////// @@ -163,24 +154,24 @@ public final class ExtractorHelper { //////////////////////////////////////////////////////////////////////////*/ /** - * Check if we can load it from the cache (forceLoad parameter), if we can't, load from the network (Single loadFromNetwork) + * Check if we can load it from the cache (forceLoad parameter), if we can't, + * load from the network (Single loadFromNetwork) * and put the results in the cache. */ - private static Single checkCache(boolean forceLoad, int serviceId, String url, Single loadFromNetwork) { + private static Single checkCache(boolean forceLoad, + int serviceId, + String url, + Single loadFromNetwork) { checkServiceId(serviceId); - loadFromNetwork = loadFromNetwork.doOnSuccess(new Consumer() { - @Override - public void accept(@NonNull I i) throws Exception { - cache.putInfo(i); - } - }); + loadFromNetwork = loadFromNetwork.doOnSuccess((@NonNull I i) -> cache.putInfo(i)); Single load; if (forceLoad) { cache.removeInfo(serviceId, url); load = loadFromNetwork; } else { - load = Maybe.concat(ExtractorHelper.loadFromCache(serviceId, url), loadFromNetwork.toMaybe()) + load = Maybe.concat(ExtractorHelper.loadFromCache(serviceId, url), + loadFromNetwork.toMaybe()) .firstElement() //Take the first valid .toSingle(); } @@ -193,9 +184,7 @@ public final class ExtractorHelper { */ public static Maybe loadFromCache(final int serviceId, final String url) { checkServiceId(serviceId); - return Maybe.defer(new Callable>() { - @Override - public MaybeSource call() throws Exception { + return Maybe.defer(() -> { //noinspection unchecked I info = (I) cache.getFromKey(serviceId, url); if (MainActivity.DEBUG) Log.d(TAG, "loadFromCache() called, info > " + info); @@ -206,8 +195,7 @@ public final class ExtractorHelper { } return Maybe.empty(); - } - }); + }); } /** @@ -215,7 +203,8 @@ public final class ExtractorHelper { * * @see Class#isAssignableFrom(Class) */ - public static boolean hasAssignableCauseThrowable(Throwable throwable, Class... causesToCheck) { + public static boolean hasAssignableCauseThrowable(Throwable throwable, + Class... causesToCheck) { // Check if getCause is not the same as cause (the getCause is already the root), // as it will cause a infinite loop if it is Throwable cause, getCause = throwable; @@ -270,7 +259,9 @@ public final class ExtractorHelper { * Check if throwable have Interrupted* exception as one of its causes. */ public static boolean isInterruptedCaused(Throwable throwable) { - return ExtractorHelper.hasExactCauseThrowable(throwable, InterruptedIOException.class, InterruptedException.class); + return ExtractorHelper.hasExactCauseThrowable(throwable, + InterruptedIOException.class, + InterruptedException.class); } public static String toUpperCase(String value) {