io.reactivex.Observable.just()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(7.6k)|赞(0)|评价(0)|浏览(267)

本文整理了Java中io.reactivex.Observable.just()方法的一些代码示例,展示了Observable.just()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Observable.just()方法的具体详情如下:
包路径:io.reactivex.Observable
类名称:Observable
方法名:just

Observable.just介绍

[英]Returns an Observable that signals the given (constant reference) item and then completes.

Note that the item is taken and re-emitted as is and not computed by any means by just. Use #fromCallable(Callable)to generate a single item on demand (when Observers subscribe to it).

See the multi-parameter overloads of just to emit more than one (constant reference) items one after the other. Use #fromArray(Object...) to emit an arbitrary number of items that are known upfront.

To emit the items of an Iterable sequence (such as a java.util.List), use #fromIterable(Iterable). Scheduler: just does not operate by default on a particular Scheduler.
[中]返回一个可观测值,该值表示给定(常量引用)项,然后完成。
请注意,该项目是按原样获取和重新发射的,而不是通过任何方式计算的。使用#fromCallable(Callable)按需生成单个项目(当观察员订阅时)。
请参阅的多参数重载,以便一个接一个地发出多个(常量引用)项。使用#fromArray(对象…)发出预先已知的任意数量的项。
要发出Iterable序列的项(例如java.util.List),请使用#fromIterable(Iterable)。调度器:只是默认情况下不会在特定的调度器上运行。

代码示例

代码示例来源:origin: ReactiveX/RxJava

@Override
  public ObservableSource<Integer> apply(Integer v) {
    return Observable.just(1);
  }
})

代码示例来源:origin: ReactiveX/RxJava

@Override
  public ObservableSource<Integer> apply(Integer v) throws Exception {
    return Observable.just(1);
  }
})

代码示例来源:origin: ReactiveX/RxJava

@Override
  public Observable<Integer> apply(Integer v) {
    return Observable.just(v);
  }
});

代码示例来源:origin: ReactiveX/RxJava

@Override
  public Observable<Integer> apply(Boolean t1) {
    return Observable.just(2).delay(500, TimeUnit.MILLISECONDS);
  }
});

代码示例来源:origin: ReactiveX/RxJava

@Override
  public Observable<Integer> apply(Integer t) {
    Observable<Integer> o = Observable.just(t)
        .subscribeOn(sch)
    ;
    Subject<Integer> subject = UnicastSubject.create();
    o.subscribe(subject);
    return subject;
  }
};

代码示例来源:origin: ReactiveX/RxJava

@Test
public void concat3() {
  Observable.concat(Observable.just(1), Observable.just(2), Observable.just(3))
  .test()
  .assertResult(1, 2, 3);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testCombineLatest3TypesB() {
  Function3<String, Integer, int[], String> combineLatestFunction = getConcatStringIntegerIntArrayCombineLatestFunction();
  /* define an Observer to receive aggregated events */
  Observer<String> observer = TestHelper.mockObserver();
  Observable<String> w = Observable.combineLatest(Observable.just("one"), Observable.just(2), Observable.just(new int[] { 4, 5, 6 }, new int[] { 7, 8 }), combineLatestFunction);
  w.subscribe(observer);
  verify(observer, never()).onError(any(Throwable.class));
  verify(observer, times(1)).onComplete();
  verify(observer, times(1)).onNext("one2[4, 5, 6]");
  verify(observer, times(1)).onNext("one2[7, 8]");
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testUnsubscribeSource() throws Exception {
  Action unsubscribe = mock(Action.class);
  Observable<Integer> o = Observable.just(1).doOnDispose(unsubscribe).cache();
  o.subscribe();
  o.subscribe();
  o.subscribe();
  verify(unsubscribe, never()).run();
}

代码示例来源:origin: ReactiveX/RxJava

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void mappingBadCapacityHint() throws Exception {
  Observable<Integer> source = Observable.just(1);
  try {
    Observable.just(source, source, source).concatMapEager((Function)Functions.identity(), 10, -99);
  } catch (IllegalArgumentException ex) {
    assertEquals("prefetch > 0 required but it was -99", ex.getMessage());
  }
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testCast() {
  Observable<?> source = Observable.just(1, 2);
  Observable<Integer> observable = source.cast(Integer.class);
  Observer<Integer> observer = TestHelper.mockObserver();
  observable.subscribe(observer);
  verify(observer, times(1)).onNext(1);
  verify(observer, times(1)).onNext(1);
  verify(observer, never()).onError(
      any(Throwable.class));
  verify(observer, times(1)).onComplete();
}

代码示例来源:origin: ReactiveX/RxJava

@Test(expected = TestException.class)
public void blockingForEachThrows() {
  Observable.just(1)
  .blockingForEach(new Consumer<Integer>() {
    @Override
    public void accept(Integer e) throws Exception {
      throw new TestException();
    }
  });
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testIsEmptyWithTwoItemsObservable() {
  Observable<Integer> w = Observable.just(1, 2);
  Observable<Boolean> observable = w.isEmpty().toObservable();
  Observer<Boolean> observer = TestHelper.mockObserver();
  observable.subscribe(observer);
  verify(observer, never()).onNext(true);
  verify(observer, times(1)).onNext(false);
  verify(observer, never()).onError(any(Throwable.class));
  verify(observer, times(1)).onComplete();
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testIsEmptyWithOneItemObservable() {
  Observable<Integer> w = Observable.just(1);
  Observable<Boolean> observable = w.isEmpty().toObservable();
  Observer<Boolean> observer = TestHelper.mockObserver();
  observable.subscribe(observer);
  verify(observer, never()).onNext(true);
  verify(observer, times(1)).onNext(false);
  verify(observer, never()).onError(any(Throwable.class));
  verify(observer, times(1)).onComplete();
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testSkipAndCountGaplessBuffers() {
  Observable<String> source = Observable.just("one", "two", "three", "four", "five");
  Observable<List<String>> buffered = source.buffer(3, 3);
  buffered.subscribe(observer);
  InOrder inOrder = Mockito.inOrder(observer);
  inOrder.verify(observer, Mockito.times(1)).onNext(list("one", "two", "three"));
  inOrder.verify(observer, Mockito.times(1)).onNext(list("four", "five"));
  inOrder.verify(observer, Mockito.never()).onNext(Mockito.<String>anyList());
  inOrder.verify(observer, Mockito.never()).onError(Mockito.any(Throwable.class));
  inOrder.verify(observer, Mockito.times(1)).onComplete();
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testLongTimeAction() throws InterruptedException {
  final CountDownLatch latch = new CountDownLatch(1);
  LongTimeAction action = new LongTimeAction(latch);
  Observable.just(1).buffer(10, TimeUnit.MILLISECONDS, 10)
      .subscribe(action);
  latch.await();
  assertFalse(action.fail);
}

代码示例来源:origin: ReactiveX/RxJava

@Test(timeout = 2000)
public void bufferWithSizeSkipTake1() {
  Observable<Integer> source = Observable.just(1).repeat();
  Observable<List<Integer>> result = source.buffer(2, 3).take(1);
  Observer<Object> o = TestHelper.mockObserver();
  result.subscribe(o);
  verify(o).onNext(Arrays.asList(1, 1));
  verify(o).onComplete();
  verify(o, never()).onError(any(Throwable.class));
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void testToIterator() {
  Observable<String> obs = Observable.just("one", "two", "three");
  Iterator<String> it = obs.blockingIterable().iterator();
  assertEquals(true, it.hasNext());
  assertEquals("one", it.next());
  assertEquals(true, it.hasNext());
  assertEquals("two", it.next());
  assertEquals(true, it.hasNext());
  assertEquals("three", it.next());
  assertEquals(false, it.hasNext());
}

代码示例来源:origin: ReactiveX/RxJava

@SuppressWarnings("unchecked")
@Test
public void ambIterableOrder() {
  Observable<Integer> error = Observable.error(new RuntimeException());
  Observable.amb(Arrays.asList(Observable.just(1), error)).test().assertValue(1).assertComplete();
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void mapperThrowsDelayError() {
  Observable.just(1).hide()
  .concatMapDelayError(new Function<Integer, ObservableSource<Integer>>() {
    @Override
    public ObservableSource<Integer> apply(Integer v) throws Exception {
      throw new TestException();
    }
  })
  .test()
  .assertFailure(TestException.class);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void innerError() {
  Observable.just(1)
  .switchMapMaybe(Functions.justFunction(Maybe.error(new TestException())))
  .test()
  .assertFailure(TestException.class);
}

相关文章

Observable类方法