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

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

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

Observable.toFuture介绍

[英]Returns a Future representing the single value emitted by this Observable.

If the Observable emits more than one item, java.util.concurrent.Future will receive an java.lang.IllegalArgumentException. If the Observable is empty, java.util.concurrent.Futurewill receive an java.util.NoSuchElementException.

If the Observable may emit more than one item, use Observable.toList().toFuture().

Scheduler: toFuture does not operate by default on a particular Scheduler.
[中]

代码示例

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

@Override
  public Integer apply(Integer v) throws Exception {
    Observable.just(1).delay(10, TimeUnit.SECONDS).toFuture().get();
    return v;
  }
})

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

@Test
public void testToFuture() throws InterruptedException, ExecutionException {
  Observable<String> obs = Observable.just("one");
  Future<String> f = obs.toFuture();
  assertEquals("one", f.get());
}

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

@Test(expected = NoSuchElementException.class)
public void testGetWithEmptyFlowable() throws Throwable {
  Observable<String> obs = Observable.empty();
  Future<String> f = obs.toFuture();
  try {
    f.get();
  }
  catch (ExecutionException e) {
    throw e.getCause();
  }
}

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

@Test(/* timeout = 5000, */expected = IndexOutOfBoundsException.class)
public void testExceptionWithMoreThanOneElement() throws Throwable {
  Observable<String> obs = Observable.just("one", "two");
  Future<String> f = obs.toFuture();
  try {
    // we expect an exception since there are more than 1 element
    f.get();
    fail("Should have thrown!");
  }
  catch (ExecutionException e) {
    throw e.getCause();
  }
}

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

@Ignore("null value is not allowed")
  @Test
  public void testGetWithASingleNullItem() throws Exception {
    Observable<String> obs = Observable.just((String)null);
    Future<String> f = obs.toFuture();
    assertEquals(null, f.get());
  }
}

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

@Test(expected = CancellationException.class)
public void testGetAfterCancel() throws Exception {
  Observable<String> obs = Observable.never();
  Future<String> f = obs.toFuture();
  boolean cancelled = f.cancel(true);
  assertTrue(cancelled);  // because OperationNeverComplete never does
  f.get();                // Future.get() docs require this to throw
}

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

@Test(expected = CancellationException.class)
public void testGetWithTimeoutAfterCancel() throws Exception {
  Observable<String> obs = Observable.never();
  Future<String> f = obs.toFuture();
  boolean cancelled = f.cancel(true);
  assertTrue(cancelled);  // because OperationNeverComplete never does
  f.get(Long.MAX_VALUE, TimeUnit.NANOSECONDS);    // Future.get() docs require this to throw
}

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

@Test
public void testToFutureWithException() {
  Observable<String> obs = Observable.unsafeCreate(new ObservableSource<String>() {
    @Override
    public void subscribe(Observer<? super String> observer) {
      observer.onSubscribe(Disposables.empty());
      observer.onNext("one");
      observer.onError(new TestException());
    }
  });
  Future<String> f = obs.toFuture();
  try {
    f.get();
    fail("expected exception");
  } catch (Throwable e) {
    assertEquals(TestException.class, e.getCause().getClass());
  }
}

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

@Test
public void fromFutureTimeout() throws Exception {
  Observable.fromFuture(Observable.never()
  .toFuture(), 100, TimeUnit.MILLISECONDS, Schedulers.io())
  .test()
  .awaitDone(5, TimeUnit.SECONDS)
  .assertFailure(TimeoutException.class);
}

代码示例来源:origin: nemtech/nem2-docs

@Test
  void gettingBlockByHeight() throws ExecutionException, InterruptedException, MalformedURLException {
    final BlockchainHttp blockchainHttp = new BlockchainHttp("http://localhost:3000");

    // Replace with block height
    final BigInteger blockHeight = BigInteger.valueOf(1);

    final BlockInfo blockInfo = blockchainHttp.getBlockByHeight(blockHeight).toFuture().get();

    System.out.print(blockInfo);
  }
}

代码示例来源:origin: nemtech/nem2-docs

@Test
  void gettingLastBlockchainBlock() throws ExecutionException, InterruptedException, MalformedURLException {
    final BlockchainHttp blockchainHttp = new BlockchainHttp("http://localhost:3000");

    final BigInteger blockchainHeight = blockchainHttp.getBlockchainHeight().toFuture().get();

    System.out.print(blockchainHeight);
  }
}

代码示例来源:origin: nemtech/nem2-docs

@Test
  void gettingAccountInformation() throws ExecutionException, InterruptedException, MalformedURLException {
    final AccountHttp accountHttp = new AccountHttp("http://localhost:3000");

    // Replace with address
    final String address = "SD5DT3-CH4BLA-BL5HIM-EKP2TA-PUKF4N-Y3L5HR-IR54";

    final AccountInfo accountInfo = accountHttp.getAccountInfo(Address.createFromRawAddress(address)).toFuture().get();

    System.out.println(accountInfo);
  }
}

代码示例来源:origin: nemtech/nem2-docs

@Test
  void listeningNewBlocks() throws ExecutionException, InterruptedException, MalformedURLException {
    Listener listener = new Listener("http://localhost:3000");

    listener.open().get();

    BlockInfo blockInfo = listener.newBlock().take(1).toFuture().get();

    System.out.println(blockInfo);
  }
}

代码示例来源:origin: nemtech/nem2-docs

@Test
  void gettingMultisigAccountInformation() throws ExecutionException, InterruptedException, MalformedURLException {
    final AccountHttp accountHttp = new AccountHttp("http://localhost:3000");

    // Replace with address
    final String addressRaw = "SB2RPH-EMTFMB-KELX2Y-Q3MZTD-RV7DQG-UZEADV-CYKC";

    final Address address = Address.createFromRawAddress(addressRaw);

    final MultisigAccountInfo multisigAccountInfo = accountHttp.getMultisigAccountInfo(address).toFuture().get();

    System.out.println(multisigAccountInfo);
  }
}

代码示例来源:origin: nemtech/nem2-docs

@Test
  void checkingNamespaceExistence() throws ExecutionException, InterruptedException, MalformedURLException {

    final NamespaceId namespaceId = new NamespaceId("foo");

    final NamespaceHttp namespaceHttp = new NamespaceHttp("http://localhost:3000");

    final NamespaceInfo namespaceInfo = namespaceHttp.getNamespace(namespaceId).toFuture().get();

    System.out.println(namespaceInfo);
  }
}

代码示例来源:origin: nemtech/nem2-docs

@Test
  void debuggingTransactionsConfirmed() throws ExecutionException, InterruptedException, MalformedURLException {

    Listener listener = new Listener("http://localhost:3000");

    Address address = Address.createFromRawAddress("SD5DT3-CH4BLA-BL5HIM-EKP2TA-PUKF4N-Y3L5HR-IR54");

    listener.open().get();

    Transaction transaction = listener.confirmed(address).take(1).toFuture().get();

    System.out.println(transaction);
  }
}

代码示例来源:origin: nemtech/nem2-docs

@Test
  void gettingConfirmedTransactions() throws ExecutionException, InterruptedException, MalformedURLException {
    final AccountHttp accountHttp = new AccountHttp("http://localhost:3000");

    // Replace with public key
    final String publicKey = "";

    final PublicAccount publicAccount = PublicAccount.createFromPublicKey(publicKey, NetworkType.MIJIN_TEST);

    // Page size between 10 and 100, otherwise 10
    int pageSize = 20;

    final List<Transaction> transactions = accountHttp.transactions(publicAccount, new QueryParams(pageSize, null)).toFuture().get();

    System.out.print(transactions);
  }
}

代码示例来源:origin: nemtech/nem2-docs

@Test
  void registeringANamespace() throws ExecutionException, InterruptedException, MalformedURLException {

    // Replace with private key
    final String privateKey = "";

    final Account account = Account.createFromPrivateKey(privateKey, NetworkType.MIJIN_TEST);

    // Replace with namespace name
    final String namespaceName = "foo";

    final RegisterNamespaceTransaction registerNamespaceTransaction = RegisterNamespaceTransaction.createRootNamespace(
        Deadline.create(2, ChronoUnit.HOURS),
        namespaceName,
        BigInteger.valueOf(1000),
        NetworkType.MIJIN_TEST
    );

    final SignedTransaction signedTransaction = account.sign(registerNamespaceTransaction);

    final TransactionHttp transactionHttp = new TransactionHttp("http://localhost:3000");

    transactionHttp.announce(signedTransaction).toFuture().get();
  }
}

代码示例来源:origin: nemtech/nem2-docs

@Test
  void signingAnnouncedAggregateBondedTransactionsAutomatically() throws ExecutionException, InterruptedException, MalformedURLException {
    // Replace with a private key
    final String privateKey = "";

    final Account account = Account.createFromPrivateKey(privateKey, NetworkType.MIJIN_TEST);

    final AccountHttp accountHttp = new AccountHttp("http://localhost:3000");

    final TransactionHttp transactionHttp = new TransactionHttp("http://localhost:3000");

    final Listener listener = new Listener("http://localhost:3000");

    listener.open().get();

    final AggregateTransaction transaction = listener.aggregateBondedAdded(account.getAddress()).take(1).toFuture().get();

    if (!transaction.signedByAccount(account.getPublicAccount())) {
      // Filter aggregates that need my signature
      final CosignatureTransaction cosignatureTransaction = CosignatureTransaction.create(transaction);

      final CosignatureSignedTransaction cosignatureSignedTransaction = account.signCosignatureTransaction(cosignatureTransaction);

      transactionHttp.announceAggregateBondedCosignature(cosignatureSignedTransaction).toFuture().get();
    }
  }
}

代码示例来源:origin: nemtech/nem2-docs

@Test
  void modifyingMosaicSupply() throws ExecutionException, InterruptedException, MalformedURLException {

    // Replace with private key
    final String privateKey = "";

    final Account account = Account.createFromPrivateKey(privateKey, NetworkType.MIJIN_TEST);

    // Replace with mosaic id
    final MosaicId mosaicId = new MosaicId("foo:token"); // replace with mosaic full name

    MosaicSupplyChangeTransaction mosaicSupplyChangeTransaction = MosaicSupplyChangeTransaction.create(
        new Deadline(2, ChronoUnit.HOURS),
        mosaicId,
        MosaicSupplyType.INCREASE,
        BigInteger.valueOf(2000000),
        NetworkType.MIJIN_TEST
    );

    final SignedTransaction signedTransaction = account.sign(mosaicSupplyChangeTransaction);

    final TransactionHttp transactionHttp = new TransactionHttp("http://localhost:3000");

    transactionHttp.announce(signedTransaction).toFuture().get();
  }
}

相关文章

Observable类方法