本文整理汇总了Java中io.reactivex.functions.Function类的典型用法代码示例。如果您正苦于以下问题:Java Function类的具体用法?Java Function怎么用?Java Function使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Function类属于io.reactivex.functions包,在下文中一共展示了Function类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: deleteAll
import io.reactivex.functions.Function; //导入依赖的package包/类
@Override
public Flowable<Integer> deleteAll() {
if(shouldThrowError){
shouldThrowError = false; // special case because the StoreService needs to call again getAll()
return Flowable.error(new Exception("deleteAll.error"));
}
return getAll(null, null)
.delay(1, TimeUnit.SECONDS)
.flatMap(new Function<Optional<List<TestModel>>, Flowable<Integer>>() {
@Override
public Flowable<Integer> apply(Optional<List<TestModel>> ts) throws Exception {
return Flowable.just(ts.get().size());
}
});
}
示例2: initView
import io.reactivex.functions.Function; //导入依赖的package包/类
@Override public void initView(View view) {
requestBaseInit(getPageTitle());
mAdapter = new AllAppInfoAdapter(this, mAppInfos);
mList.setLayoutManager(new LinearLayoutManager(getActivity()));
mList.setAdapter(mAdapter);
Observable.just("2")
.subscribeOn(Schedulers.io())
.map(new Function<String, List<AppUtils.AppInfo>>() {
@Override public List<AppUtils.AppInfo> apply(@NonNull String s) throws Exception {
return AppUtils.getAppsInfo();
}
})
.observeOn(PausedHandlerScheduler.from(getHandler()))
.compose(mLifecycleProvider.<List<AppUtils.AppInfo>>bindUntilEvent(FragmentEvent.DESTROY))
.subscribe(new Consumer<List<AppUtils.AppInfo>>() {
@Override public void accept(@NonNull List<AppUtils.AppInfo> appInfos) throws Exception {
mAdapter.resetData(appInfos);
}
});
}
示例3: applyOperation
import io.reactivex.functions.Function; //导入依赖的package包/类
void applyOperation(final Function<List<T>, Update<T>> operation)
{
synchronized (_batchingLock) {
if (_batchedOperations != null) {
_batchedOperations.add(operation);
return;
}
}
applyUpdate(new Function<List<T>, Update<T>>() {
@Override
public Update<T> apply(List<T> list) throws Exception
{
list = new ArrayList<>(list);
return operation.apply(list);
}
});
}
示例4: getAllClients
import io.reactivex.functions.Function; //导入依赖的package包/类
public static Observable<List<Client>> getAllClients() {
ClientsService service = ServiceGenerator.createService(ClientsService.class);
return service.getAllClients(UrlManager.getAllClientsURL())
.flatMap(new Function<JsonElement, Observable<List<Client>>>() {
@Override
public Observable<List<Client>> apply(JsonElement jsonElement) throws Exception {
if(jsonElement != null) {
Log.i("Get All Clients" , "JSON: "+jsonElement.toString());
if(jsonElement.isJsonArray()) {
List<Client> clients = Client.ClientsListParser.fromJsonArray(jsonElement.getAsJsonArray());
return Observable.just(clients);
} else {
return Observable.error(new Exception("Expected a JSON Array"));
}
} else {
return Observable.just((List<Client>) new ArrayList<Client>());
}
}
}).observeOn(AndroidSchedulers.mainThread());
}
示例5: put
import io.reactivex.functions.Function; //导入依赖的package包/类
@Override
public Observable<StorageResult> put(Observable<Entry> entries) {
return entries.flatMap((Function<Entry, ObservableSource<StorageResult>>) entry -> {
final String insert =
"INSERT OR REPLACE INTO TILES(zoom_level, tile_column, tile_row, tile_data)"
+ " values (?, ?, ?, ?);";
byte[] compressedMvt;
try {
compressedMvt = CompressUtil.getCompressedAsGzip(entry.getVector());
} catch (final IOException ex) {
throw Exceptions.propagate(ex);
}
Observable<Object> params = Observable.<Object>just(entry.getZoomLevel(), entry.getColumn(),
flipY(entry.getRow(), entry.getZoomLevel()), compressedMvt);
return dataSource.update(insert)
.parameterStream(params.toFlowable(BackpressureStrategy.BUFFER)).counts()
.map(integer -> new StorageResult(entry))
.onErrorReturn(throwable -> new StorageResult(entry, new Exception(throwable)))
.toObservable();
});
}
示例6: registerEvent
import io.reactivex.functions.Function; //导入依赖的package包/类
void registerEvent() {
addSubscribe(RxBus.getDefault().toFlowable(NightModeEvent.class)
.compose(RxUtil.<NightModeEvent>rxSchedulerHelper())
.map(new Function<NightModeEvent, Boolean>() {
@Override
public Boolean apply(NightModeEvent nightModeEvent) {
return nightModeEvent.getNightMode();
}
})
.subscribeWith(new CommonSubscriber<Boolean>(mView, "切换模式失败ヽ(≧Д≦)ノ") {
@Override
public void onNext(Boolean aBoolean) {
mView.useNightMode(aBoolean);
}
})
);
}
示例7: onScheduleCrashes
import io.reactivex.functions.Function; //导入依赖的package包/类
@Test
public void onScheduleCrashes() {
RxSwingPlugins.setOnSchedule(new Function<Runnable, Runnable>() {
@Override
public Runnable apply(Runnable r) throws Exception {
throw new IllegalStateException("Failure");
}
});
try {
RxSwingPlugins.onSchedule(Functions.EMPTY_RUNNABLE);
Assert.fail("Should have thrown!");
} catch (IllegalStateException ex) {
Assert.assertEquals("Failure", ex.getMessage());
}
RxSwingPlugins.reset();
Assert.assertSame(Functions.EMPTY_RUNNABLE, RxSwingPlugins.onSchedule(Functions.EMPTY_RUNNABLE));
}
示例8: news
import io.reactivex.functions.Function; //导入依赖的package包/类
/**
* Informations sur une personne
*/
private Single<News> news(String idNews, String profile) {
final String params = ServiceSecurity.construireParams(false,
AllocineService.CODE, idNews,
AllocineService.PROFILE, profile
);
final String sed = ServiceSecurity.getSED();
final String sig = ServiceSecurity.getSIG(params, sed);
return allocineService.news(idNews, profile, sed, sig)
.map(new Function<AllocineResponse, News>() {
@Override
public News apply(AllocineResponse allocineResponse) throws Exception {
return null;
}
});
}
示例9: getById
import io.reactivex.functions.Function; //导入依赖的package包/类
@Override
public Flowable<Optional<T>> getById(final int id) {
List<Flowable<Optional<T>>> flowables = new ArrayList<>();
Flowable<Optional<T>> flowStorage = dao.getById(id);
if(hasSyncedStore()) {
flowStorage = flowStorage
.flatMap(new Function<Optional<T>, Flowable<Optional<T>>>() {
@Override
public Flowable<Optional<T>> apply(final Optional<T> item) throws Exception {
return syncedStore.insertOrUpdate(item.get());
}
});
flowables.add(syncedStore.getById(id));
}
flowables.add(flowStorage);
return Flowable.concat(flowables);
}
示例10: execute
import io.reactivex.functions.Function; //导入依赖的package包/类
@Override
public <T> Observable<CacheResult<T>> execute(ApiCache apiCache, String cacheKey, Observable<T> source, Type type) {
Observable<CacheResult<T>> cache = loadCache(apiCache, cacheKey, type);
cache.onErrorReturn(new Function<Throwable, CacheResult<T>>() {
@Override
public CacheResult<T> apply(Throwable throwable) throws Exception {
return null;
}
});
Observable<CacheResult<T>> remote = loadRemote(apiCache, cacheKey, source);
return Observable.concat(remote, cache).filter(new Predicate<CacheResult<T>>() {
@Override
public boolean test(CacheResult<T> tCacheResult) throws Exception {
return tCacheResult != null && tCacheResult.getCacheData() != null;
}
}).firstElement().toObservable();
}
示例11: startRefresh
import io.reactivex.functions.Function; //导入依赖的package包/类
@Override
protected void startRefresh(HandleBase<MultiHeaderEntity> refreshData) {
Flowable.just(refreshData)
.onBackpressureDrop()
.observeOn(Schedulers.computation())
.map(new Function<HandleBase<MultiHeaderEntity>, DiffUtil.DiffResult>() {
@Override
public DiffUtil.DiffResult apply(@NonNull HandleBase<MultiHeaderEntity> handleBase) throws Exception {
return handleRefresh(handleBase.getNewData(), handleBase.getNewHeader(), handleBase.getNewFooter(), handleBase.getType(), handleBase.getRefreshType());
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<DiffUtil.DiffResult>() {
@Override
public void accept(@NonNull DiffUtil.DiffResult diffResult) throws Exception {
handleResult(diffResult);
}
});
}
示例12: seedDatabaseQuestions
import io.reactivex.functions.Function; //导入依赖的package包/类
@Override
public Observable<Boolean> seedDatabaseQuestions() {
GsonBuilder builder = new GsonBuilder().excludeFieldsWithoutExposeAnnotation();
final Gson gson = builder.create();
return mDbHelper.isQuestionEmpty()
.concatMap(new Function<Boolean, ObservableSource<? extends Boolean>>() {
@Override
public ObservableSource<? extends Boolean> apply(Boolean isEmpty)
throws Exception {
if (isEmpty) {
Type type = $Gson$Types
.newParameterizedTypeWithOwner(null, List.class,
Question.class);
List<Question> questionList = gson.fromJson(
CommonUtils.loadJSONFromAsset(mContext,
AppConstants.SEED_DATABASE_QUESTIONS),
type);
return saveQuestionList(questionList);
}
return Observable.just(false);
}
});
}
示例13: initView
import io.reactivex.functions.Function; //导入依赖的package包/类
@Override protected void initView(View parent) {
requestBaseInit(getPageTitle());
Observable.just("123")
.subscribeOn(Schedulers.io())
.map(new Function<String, Bitmap>() {
@Override public Bitmap apply(@NonNull String s) throws Exception {
return BitmapFactory.decodeResource(getResources(), R.drawable.test1);
}
})
.observeOn(PausedHandlerScheduler.from(getHandler()))
.compose(mLifecycleProvider.<Bitmap>bindUntilEvent(FragmentEvent.DESTROY))
.subscribe(new Consumer<Bitmap>() {
@Override public void accept(@NonNull Bitmap bitmap) throws Exception {
mBitmap = bitmap;
mImage.setImageBitmap(mBitmap);
}
});
}
示例14: mainThreadCallsThroughToHook
import io.reactivex.functions.Function; //导入依赖的package包/类
@Test
public void mainThreadCallsThroughToHook() {
final AtomicInteger called = new AtomicInteger();
final Scheduler newScheduler = new EmptyScheduler();
RxAndroidPlugins.setMainThreadSchedulerHandler(new Function<Scheduler, Scheduler>() {
@Override public Scheduler apply(Scheduler scheduler) {
called.getAndIncrement();
return newScheduler;
}
});
assertSame(newScheduler, AndroidSchedulers.mainThread());
assertEquals(1, called.get());
assertSame(newScheduler, AndroidSchedulers.mainThread());
assertEquals(2, called.get());
}
示例15: directScheduleOnceUsesHook
import io.reactivex.functions.Function; //导入依赖的package包/类
@Test
public void directScheduleOnceUsesHook() {
final CountingRunnable newCounter = new CountingRunnable();
final AtomicReference<Runnable> runnableRef = new AtomicReference<>();
RxJavaPlugins.setScheduleHandler(new Function<Runnable, Runnable>() {
@Override public Runnable apply(Runnable runnable) {
runnableRef.set(runnable);
return newCounter;
}
});
CountingRunnable counter = new CountingRunnable();
scheduler.scheduleDirect(counter);
// Verify our runnable was passed to the schedulers hook.
assertSame(counter, runnableRef.get());
runUiThreadTasks();
// Verify the scheduled runnable was the one returned from the hook.
assertEquals(1, newCounter.get());
assertEquals(0, counter.get());
}