public final class Utils extends Object
Modifier and Type | Method and Description |
---|---|
static <C extends Closeable,T> |
tryWith(Single<C> closeable,
Function<? super C,Flowable<T>> mapper)
|
static <C extends Closeable,T> |
tryWithSingle(Single<C> closeable,
Function<? super C,Single<T>> mapper)
|
public static <C extends Closeable,T> Flowable<T> tryWith(Single<C> closeable, Function<? super C,Flowable<T>> mapper)
Flowable
based upon a given Closeable
and a Function
mapper.
The given Closeable
is automatically closed wheneveronComplete
or onError
or Subscription.cancel()
is invoked.
This method is similar to the try-with-resources of Java 7 that closes resources at the end of the statement.
Below an example of usage: when the subscription is complete, canceled or terminates with an error, the
socket
is automatically closed.
Single<LdapClientSocket> ldapSocket = ...
tryWith(ldapSocket, socket -> socket.search(request)).subscribe(this::handleResponse, this::handleException)
C
- Any element type that extends Closeable
T
- The element type of the generated Flowablecloseable
- The Single
that encapsulates the Closeable
mapper
- A factory function to create a Flowable based upon the given Closeable
Closeable
Flowable
,
Closeable
public static <C extends Closeable,T> Single<T> tryWithSingle(Single<C> closeable, Function<? super C,Single<T>> mapper)
Single
based upon a given Closeable
and a Function
mapper.
The given Closeable
is automatically closed whenever onComplete
or onError
or Subscription.cancel()
is invoked.
This method is similar to the try-with-resources of Java 7 that closes resources at the end of the statement.
Below an example of usage: when the subscription is complete, canceled or terminates with an error, the
socket
is automatically closed.
Single<LdapClientSocket> ldapSocket = ...
tryWithSingle(ldapSocket, socket -> socket.searchSingleEntry(request))
.subscribe(this::handleResponse, this::handleException)
C
- Any element type that extends Closeable
T
- The element type of the generated Singlecloseable
- The Single
that encapsulates the Closeable
mapper
- A factory function to create a Single based upon the given Closeable
Closeable
Single
,
Closeable
Copyright 2010-2022 ForgeRock AS.