refactor ExtractionHelper using lambda expression

This commit is contained in:
Christian Schabesberger 2018-01-20 13:57:31 +01:00
parent 5f26501ddf
commit eb4b3810e9
1 changed files with 79 additions and 88 deletions

View File

@ -57,105 +57,96 @@ public final class ExtractorHelper {
} }
} }
public static Single<SearchResult> searchFor(final int serviceId, final String query, final int pageNumber, final String contentCountry, final SearchEngine.Filter filter) { public static Single<SearchResult> searchFor(final int serviceId,
final String query,
final int pageNumber,
final String contentCountry,
final SearchEngine.Filter filter) {
checkServiceId(serviceId); checkServiceId(serviceId);
return Single.fromCallable(new Callable<SearchResult>() { return Single.fromCallable(() ->
@Override SearchResult.getSearchResult(NewPipe.getService(serviceId).getSearchEngine(),
public SearchResult call() throws Exception { query, pageNumber, contentCountry, filter)
return SearchResult.getSearchResult(NewPipe.getService(serviceId).getSearchEngine(), );
query, pageNumber, contentCountry, filter);
}
});
} }
public static Single<NextItemsResult> getMoreSearchItems(final int serviceId, final String query, final int nextPageNumber, final String searchLanguage, final SearchEngine.Filter filter) { public static Single<NextItemsResult> getMoreSearchItems(final int serviceId,
final String query,
final int nextPageNumber,
final String searchLanguage,
final SearchEngine.Filter filter) {
checkServiceId(serviceId); checkServiceId(serviceId);
return searchFor(serviceId, query, nextPageNumber, searchLanguage, filter) return searchFor(serviceId, query, nextPageNumber, searchLanguage, filter)
.map(new Function<SearchResult, NextItemsResult>() { .map((@NonNull SearchResult searchResult) ->
@Override new NextItemsResult(searchResult.resultList,
public NextItemsResult apply(@NonNull SearchResult searchResult) throws Exception { nextPageNumber + "",
return new NextItemsResult(searchResult.resultList, nextPageNumber + "", searchResult.errors); searchResult.errors));
}
});
} }
public static Single<List<String>> suggestionsFor(final int serviceId, final String query, final String contentCountry) { public static Single<List<String>> suggestionsFor(final int serviceId,
final String query,
final String contentCountry) {
checkServiceId(serviceId); checkServiceId(serviceId);
return Single.fromCallable(new Callable<List<String>>() { return Single.fromCallable(() ->
@Override NewPipe.getService(serviceId)
public List<String> call() throws Exception { .getSuggestionExtractor()
return NewPipe.getService(serviceId).getSuggestionExtractor().suggestionList(query, contentCountry); .suggestionList(query, contentCountry));
}
});
} }
public static Single<StreamInfo> getStreamInfo(final int serviceId, final String url, boolean forceLoad) { public static Single<StreamInfo> getStreamInfo(final int serviceId,
final String url,
boolean forceLoad) {
checkServiceId(serviceId); checkServiceId(serviceId);
return checkCache(forceLoad, serviceId, url, Single.fromCallable(new Callable<StreamInfo>() { return checkCache(forceLoad, serviceId, url, Single.fromCallable(() ->
@Override StreamInfo.getInfo(NewPipe.getService(serviceId), url)));
public StreamInfo call() throws Exception {
return StreamInfo.getInfo(NewPipe.getService(serviceId), url);
}
}));
} }
public static Single<ChannelInfo> getChannelInfo(final int serviceId, final String url, boolean forceLoad) { public static Single<ChannelInfo> getChannelInfo(final int serviceId,
final String url,
boolean forceLoad) {
checkServiceId(serviceId); checkServiceId(serviceId);
return checkCache(forceLoad, serviceId, url, Single.fromCallable(new Callable<ChannelInfo>() { return checkCache(forceLoad, serviceId, url, Single.fromCallable(() ->
@Override ChannelInfo.getInfo(NewPipe.getService(serviceId), url)));
public ChannelInfo call() throws Exception {
return ChannelInfo.getInfo(NewPipe.getService(serviceId), url);
}
}));
} }
public static Single<NextItemsResult> getMoreChannelItems(final int serviceId, final String url, final String nextStreamsUrl) { public static Single<NextItemsResult> getMoreChannelItems(final int serviceId,
final String url,
final String nextStreamsUrl) {
checkServiceId(serviceId); checkServiceId(serviceId);
return Single.fromCallable(new Callable<NextItemsResult>() { return Single.fromCallable(() ->
@Override ChannelInfo.getMoreItems(NewPipe.getService(serviceId), url, nextStreamsUrl));
public NextItemsResult call() throws Exception {
return ChannelInfo.getMoreItems(NewPipe.getService(serviceId), url, nextStreamsUrl);
}
});
} }
public static Single<PlaylistInfo> getPlaylistInfo(final int serviceId, final String url, boolean forceLoad) { public static Single<PlaylistInfo> getPlaylistInfo(final int serviceId,
final String url,
boolean forceLoad) {
checkServiceId(serviceId); checkServiceId(serviceId);
return checkCache(forceLoad, serviceId, url, Single.fromCallable(new Callable<PlaylistInfo>() { return checkCache(forceLoad, serviceId, url, Single.fromCallable(() ->
@Override PlaylistInfo.getInfo(NewPipe.getService(serviceId), url)));
public PlaylistInfo call() throws Exception {
return PlaylistInfo.getInfo(NewPipe.getService(serviceId), url);
}
}));
} }
public static Single<NextItemsResult> getMorePlaylistItems(final int serviceId, final String url, final String nextStreamsUrl) { public static Single<NextItemsResult> getMorePlaylistItems(final int serviceId,
final String url,
final String nextStreamsUrl) {
checkServiceId(serviceId); checkServiceId(serviceId);
return Single.fromCallable(new Callable<NextItemsResult>() { return Single.fromCallable(() ->
@Override PlaylistInfo.getMoreItems(NewPipe.getService(serviceId), url, nextStreamsUrl));
public NextItemsResult call() throws Exception {
return PlaylistInfo.getMoreItems(NewPipe.getService(serviceId), url, nextStreamsUrl);
}
});
} }
public static Single<KioskInfo> getKioskInfo(final int serviceId, final String url, final String contentCountry, boolean forceLoad) { public static Single<KioskInfo> getKioskInfo(final int serviceId,
return checkCache(forceLoad, serviceId, url, Single.fromCallable(new Callable<KioskInfo>() { final String url,
@Override final String contentCountry,
public KioskInfo call() throws Exception { boolean forceLoad) {
Log.e("---------", contentCountry); return checkCache(forceLoad, serviceId, url, Single.fromCallable(() ->
return KioskInfo.getInfo(NewPipe.getService(serviceId), url, contentCountry); KioskInfo.getInfo(NewPipe.getService(serviceId), url, contentCountry)));
}
}));
} }
public static Single<NextItemsResult> getMoreKioskItems(final int serviceId, final String url, final String nextStreamsUrl, final String contentCountry) { public static Single<NextItemsResult> getMoreKioskItems(final int serviceId,
return Single.fromCallable(new Callable<NextItemsResult>() { final String url,
@Override final String nextStreamsUrl,
public NextItemsResult call() throws Exception { final String contentCountry) {
return KioskInfo.getMoreItems(NewPipe.getService(serviceId), url, nextStreamsUrl, 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. * and put the results in the cache.
*/ */
private static <I extends Info> Single<I> checkCache(boolean forceLoad, int serviceId, String url, Single<I> loadFromNetwork) { private static <I extends Info> Single<I> checkCache(boolean forceLoad,
int serviceId,
String url,
Single<I> loadFromNetwork) {
checkServiceId(serviceId); checkServiceId(serviceId);
loadFromNetwork = loadFromNetwork.doOnSuccess(new Consumer<I>() { loadFromNetwork = loadFromNetwork.doOnSuccess((@NonNull I i) -> cache.putInfo(i));
@Override
public void accept(@NonNull I i) throws Exception {
cache.putInfo(i);
}
});
Single<I> load; Single<I> load;
if (forceLoad) { if (forceLoad) {
cache.removeInfo(serviceId, url); cache.removeInfo(serviceId, url);
load = loadFromNetwork; load = loadFromNetwork;
} else { } else {
load = Maybe.concat(ExtractorHelper.<I>loadFromCache(serviceId, url), loadFromNetwork.toMaybe()) load = Maybe.concat(ExtractorHelper.<I>loadFromCache(serviceId, url),
loadFromNetwork.toMaybe())
.firstElement() //Take the first valid .firstElement() //Take the first valid
.toSingle(); .toSingle();
} }
@ -193,9 +184,7 @@ public final class ExtractorHelper {
*/ */
public static <I extends Info> Maybe<I> loadFromCache(final int serviceId, final String url) { public static <I extends Info> Maybe<I> loadFromCache(final int serviceId, final String url) {
checkServiceId(serviceId); checkServiceId(serviceId);
return Maybe.defer(new Callable<MaybeSource<? extends I>>() { return Maybe.defer(() -> {
@Override
public MaybeSource<? extends I> call() throws Exception {
//noinspection unchecked //noinspection unchecked
I info = (I) cache.getFromKey(serviceId, url); I info = (I) cache.getFromKey(serviceId, url);
if (MainActivity.DEBUG) Log.d(TAG, "loadFromCache() called, info > " + info); if (MainActivity.DEBUG) Log.d(TAG, "loadFromCache() called, info > " + info);
@ -206,8 +195,7 @@ public final class ExtractorHelper {
} }
return Maybe.empty(); return Maybe.empty();
} });
});
} }
/** /**
@ -215,7 +203,8 @@ public final class ExtractorHelper {
* *
* @see Class#isAssignableFrom(Class) * @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), // 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 // as it will cause a infinite loop if it is
Throwable cause, getCause = throwable; Throwable cause, getCause = throwable;
@ -270,7 +259,9 @@ public final class ExtractorHelper {
* Check if throwable have Interrupted* exception as one of its causes. * Check if throwable have Interrupted* exception as one of its causes.
*/ */
public static boolean isInterruptedCaused(Throwable throwable) { 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) { public static String toUpperCase(String value) {