@Override public MaybeSource<Integer> apply(Maybe<Integer> m) { return m.map(new Function<Integer, Integer>() { @Override public Integer apply(Integer w) throws Exception { return w + 1; } }); } })
@Override public MaybeSource<Object> apply(Maybe<Object> m) throws Exception { return m.map(Functions.identity()); } });
@PostMapping("/rxjava2-maybe") public Maybe<Person> transformRxJava2Maybe(@RequestBody Maybe<Person> personFuture) { return personFuture.map(person -> new Person(person.getName().toUpperCase())); }
@Test(expected = NullPointerException.class) public void mapNull() { Maybe.just(1).map(null); }
/** * Casts the success value of the current Maybe into the target type or signals a * ClassCastException if not compatible. * <dl> * <dt><b>Scheduler:</b></dt> * <dd>{@code cast} does not operate by default on a particular {@link Scheduler}.</dd> * </dl> * @param <U> the target type * @param clazz the type token to use for casting the success result from the current Maybe * @return the new Maybe instance */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <U> Maybe<U> cast(final Class<? extends U> clazz) { ObjectHelper.requireNonNull(clazz, "clazz is null"); return map(Functions.castFunction(clazz)); }
public Maybe<List<Long>> appendToPlaylist(final long playlistId, final List<StreamEntity> streams) { return playlistStreamTable.getMaximumIndexOf(playlistId) .firstElement() .map(maxJoinIndex -> database.runInTransaction(() -> upsertStreams(playlistId, streams, maxJoinIndex + 1)) ).subscribeOn(Schedulers.io()); }
/** * Casts the success value of the current Maybe into the target type or signals a * ClassCastException if not compatible. * <dl> * <dt><b>Scheduler:</b></dt> * <dd>{@code cast} does not operate by default on a particular {@link Scheduler}.</dd> * </dl> * @param <U> the target type * @param clazz the type token to use for casting the success result from the current Maybe * @return the new Maybe instance */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final <U> Maybe<U> cast(final Class<? extends U> clazz) { ObjectHelper.requireNonNull(clazz, "clazz is null"); return map(Functions.castFunction(clazz)); }
@Test public void timerInterruptible() throws Exception { ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(); try { for (Scheduler s : new Scheduler[] { Schedulers.single(), Schedulers.computation(), Schedulers.newThread(), Schedulers.io(), Schedulers.from(exec) }) { final AtomicBoolean interrupted = new AtomicBoolean(); TestObserver<Long> to = Maybe.timer(1, TimeUnit.MILLISECONDS, s) .map(new Function<Long, Long>() { @Override public Long apply(Long v) throws Exception { try { Thread.sleep(3000); } catch (InterruptedException ex) { interrupted.set(true); } return v; } }) .test(); Thread.sleep(500); to.cancel(); Thread.sleep(500); assertTrue(s.getClass().getSimpleName(), interrupted.get()); } } finally { exec.shutdown(); } }
@Test public void mapThrows() { Maybe.just(1).map(new Function<Integer, Object>() { @Override public Object apply(Integer v) throws Exception { throw new IOException(); } }).test().assertFailure(IOException.class); }
@Test public void mapReturnNull() { Maybe.just(1).map(new Function<Integer, Object>() { @Override public Object apply(Integer v) throws Exception { return null; } }).test().assertFailure(NullPointerException.class); }
@Test public void map() { Maybe.just(1).map(new Function<Integer, String>() { @Override public String apply(Integer v) throws Exception { return v.toString(); } }).test().assertResult("1"); }
/** * We expect an ArithmeticException exception here because last() emits a single value * but then we divide by 0. */ @Test(expected = ArithmeticException.class) public void testMapWithErrorInFunc() { Observable.range(1, 1).lastElement().map(new Function<Integer, Integer>() { @Override public Integer apply(Integer i) { return i / 0; } }).blockingGet(); }
/** * We expect an ArithmeticException exception here because last() emits a single value * but then we divide by 0. */ @Test(expected = ArithmeticException.class) public void testMapWithErrorInFunc() { Flowable.range(1, 1).lastElement().map(new Function<Integer, Integer>() { @Override public Integer apply(Integer i) { return i / 0; } }).blockingGet(); }
@Test public void using() { final AtomicInteger disposeCount = new AtomicInteger(); Maybe.using(Functions.justCallable(1), new Function<Integer, MaybeSource<Integer>>() { @Override public MaybeSource<Integer> apply(Integer v) throws Exception { return Maybe.just(v); } }, new Consumer<Integer>() { @Override public void accept(Integer d) throws Exception { disposeCount.set(d); } }) .map(new Function<Integer, Object>() { @Override public String apply(Integer v) throws Exception { return "" + disposeCount.get() + v * 10; } }) .test() .assertResult("110"); }
@Test public void usingNonEager() { final AtomicInteger disposeCount = new AtomicInteger(); Maybe.using(Functions.justCallable(1), new Function<Integer, MaybeSource<Integer>>() { @Override public MaybeSource<Integer> apply(Integer v) throws Exception { return Maybe.just(v); } }, new Consumer<Integer>() { @Override public void accept(Integer d) throws Exception { disposeCount.set(d); } }, false) .map(new Function<Integer, Object>() { @Override public String apply(Integer v) throws Exception { return "" + disposeCount.get() + v * 10; } }) .test() .assertResult("010"); assertEquals(1, disposeCount.get()); }
/** * While mapping over range(1,0).last() we expect NoSuchElementException since the sequence is empty. */ @Test public void testErrorPassesThruMap() { assertNull(Observable.range(1, 0).lastElement().map(new Function<Integer, Integer>() { @Override public Integer apply(Integer i) { return i; } }).blockingGet()); }
@SuppressWarnings("unchecked") @Test public void upstream() { Maybe<Integer> source = Maybe.just(1); assertSame(source, ((HasUpstreamMaybeSource<Integer>)source.map(Functions.<Integer>identity())).source()); } }
/** * While mapping over range(1,0).last() we expect NoSuchElementException since the sequence is empty. */ @Test public void testErrorPassesThruMap() { assertNull(Flowable.range(1, 0).lastElement().map(new Function<Integer, Integer>() { @Override public Integer apply(Integer i) { return i; } }).blockingGet()); }
@Test public void observeOnSuccess() { String main = Thread.currentThread().getName(); Maybe.just(1) .observeOn(Schedulers.single()) .map(new Function<Integer, String>() { @Override public String apply(Integer v) throws Exception { return v + ": " + Thread.currentThread().getName(); } }) .test() .awaitDone(5, TimeUnit.SECONDS) .assertOf(TestHelper.observerSingleNot("1: " + main)) ; }
@Test public void failSingleMaybeBlockingGet() { try { RxJavaPlugins.setFailOnNonBlockingScheduler(true); Maybe.just(1) .subscribeOn(Schedulers.single()) .map(new Function<Integer, Integer>() { @Override public Integer apply(Integer v) throws Exception { Maybe.just(1).delay(10, TimeUnit.SECONDS).blockingGet(); return v; } }) .test() .awaitDone(5, TimeUnit.SECONDS) .assertFailure(IllegalStateException.class); } finally { RxJavaPlugins.reset(); } }