use of io.reactivex.functions.Function in project requery by requery.
the class ReactiveTest method testInsertOneToMany.
@Test
public void testInsertOneToMany() throws Exception {
final Person person = randomPerson();
data.insert(person).map(new Function<Person, Phone>() {
@Override
public Phone apply(Person person) {
Phone phone1 = randomPhone();
phone1.setOwner(person);
return phone1;
}
}).flatMap(new Function<Phone, Single<?>>() {
@Override
public Single<?> apply(Phone phone) {
return data.insert(phone);
}
}).blockingGet();
assertTrue(person.getPhoneNumbers().toList().size() == 1);
}
use of io.reactivex.functions.Function in project requery by requery.
the class ReactiveTest method testQuerySelfObservableMap.
@Test
public void testQuerySelfObservableMap() throws Exception {
final AtomicInteger count = new AtomicInteger();
Disposable disposable = data.select(Person.class).limit(2).get().observableResult().flatMap(new Function<ReactiveResult<Person>, Observable<Person>>() {
@Override
public Observable<Person> apply(ReactiveResult<Person> persons) {
return persons.observable();
}
}).subscribe(new Consumer<Person>() {
@Override
public void accept(Person persons) {
count.incrementAndGet();
}
});
data.insert(randomPerson()).blockingGet();
data.insert(randomPerson()).blockingGet();
assertEquals(3, count.get());
disposable.dispose();
}
use of io.reactivex.functions.Function in project requery by requery.
the class PeopleActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle("People");
}
setContentView(R.layout.activity_people);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
data = ((PeopleApplication) getApplication()).getData();
executor = Executors.newSingleThreadExecutor();
adapter = new PersonAdapter();
adapter.setExecutor(executor);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
data.count(Person.class).get().single().subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) {
if (integer == 0) {
Observable.fromCallable(new CreatePeople(data)).flatMap(new Function<Observable<Iterable<Person>>, Observable<?>>() {
@Override
public Observable<?> apply(Observable<Iterable<Person>> o) {
return o;
}
}).observeOn(Schedulers.computation()).subscribe(new Consumer<Object>() {
@Override
public void accept(Object o) {
adapter.queryAsync();
}
});
}
}
});
}
use of io.reactivex.functions.Function in project RxRelay by JakeWharton.
the class PublishRelayTest method testNestedSubscribe.
@Test
public void testNestedSubscribe() {
final PublishRelay<Integer> s = PublishRelay.create();
final AtomicInteger countParent = new AtomicInteger();
final AtomicInteger countChildren = new AtomicInteger();
final AtomicInteger countTotal = new AtomicInteger();
final ArrayList<String> list = new ArrayList<String>();
s.flatMap(new Function<Integer, Observable<String>>() {
@Override
public Observable<String> apply(final Integer v) {
countParent.incrementAndGet();
// then subscribe to subject again (it will not receive the previous value)
return s.map(new Function<Integer, String>() {
@Override
public String apply(Integer v2) {
countChildren.incrementAndGet();
return "Parent: " + v + " Child: " + v2;
}
});
}
}).subscribe(new Consumer<String>() {
@Override
public void accept(String v) {
countTotal.incrementAndGet();
list.add(v);
}
});
for (int i = 0; i < 10; i++) {
s.accept(i);
}
// System.out.println("countParent: " + countParent.get());
// System.out.println("countChildren: " + countChildren.get());
// System.out.println("countTotal: " + countTotal.get());
// 9+8+7+6+5+4+3+2+1+0 == 45
assertEquals(45, list.size());
}
use of io.reactivex.functions.Function in project RxRelay by JakeWharton.
the class PublishRelayTest method testUnsubscriptionCase.
@Test(timeout = 1000)
public void testUnsubscriptionCase() {
PublishRelay<String> src = PublishRelay.create();
for (int i = 0; i < 10; i++) {
final Observer<Object> o = TestHelper.mockObserver();
InOrder inOrder = inOrder(o);
String v = "" + i;
System.out.printf("Turn: %d%n", i);
src.firstElement().toObservable().flatMap(new Function<String, Observable<String>>() {
@Override
public Observable<String> apply(String t1) {
return Observable.just(t1 + ", " + t1);
}
}).subscribe(new DefaultObserver<String>() {
@Override
public void onNext(String t) {
o.onNext(t);
}
@Override
public void onError(Throwable e) {
o.onError(e);
}
@Override
public void onComplete() {
o.onComplete();
}
});
src.accept(v);
inOrder.verify(o).onNext(v + ", " + v);
inOrder.verify(o).onComplete();
verify(o, never()).onError(any(Throwable.class));
}
}
Aggregations