Support for reactive types conversion

One of the most comprehensive improvements toward supporting the Reactive Streams specification is the introduction of ReactiveAdapter and ReactiveAdapterRegistry. The ReactiveAdapter class gives two fundamental methods for reactive type conversion, as shown in the following code:

class ReactiveAdapter {
...

<T> Publisher<T> toPublisher(@Nullable Object source) { ... } // (1)

Object fromPublisher(Publisher<?> publisher) { ... } // (2)
}

In the preceding example, ReactiveAdapter introduces two fundamental methods for the conversion of any type to Publisher<T> (annotated with (1)) and back to Object. For example, in order to provide a conversion for the Maybe reactive type from RxJava 2, we can create our own ReactiveAdapter in the following way:

public class MaybeReactiveAdapter extends ReactiveAdapter {        // (1)

public MaybeReactiveAdapter() { // (2)
super(
ReactiveTypeDescriptor // (3)
.singleOptionalValue(Maybe.class, Maybe::empty), //
rawMaybe -> ((Maybe<?>)rawMaybe).toFlowable(), // (4)
publisher -> Flowable.fromPublisher(publisher) // (5)
.singleElement() //
);
}
}

In the preceding example, we extend the default ReactiveAdapter and provide a custom implementation (1). In turn, we provide a default constructor and hide the details of the implementation behind it (2). The first parameter to the parent constructor (3) is a definition of the ReactiveTypeDescriptor instance.

The ReactiveTypeDescriptor provides information about the reactive type that is used in ReactiveAdapter. Finally, the parent constructor requires a definition of conversion functions (lambdas in our case) that transforms a raw object (which is assumed to be Maybe) to the Publisher (4) and transforms any Publisher back to Maybe.

Note that ReactiveAdapter assumes that, before passing any object to the toPublisher method, the object's type compatibility is checked using the ReactiveAdapter#getReactiveType method.

To simplify the interaction, there is ReactiveAdapterRegistry, which allows us to keep instances of ReactiveAdapter in one place, as well as generalizing access to them, as shown in the following code:

ReactiveAdapterRegistry
.getSharedInstance() // (1)
.registerReactiveType( // (2)
ReactiveTypeDescriptor
.singleOptionalValue(Maybe.class, Maybe::empty),
rawMaybe -> ((Maybe<?>)rawMaybe).toFlowable(),
publisher -> Flowable.fromPublisher(publisher)
.singleElement()
);

...

ReactiveAdapter maybeAdapter = ReactiveAdapterRegistry
.getSharedInstance() // (1)
.getAdapter(Maybe.class); // (3)

As we can see, the ReactiveAdapterRegistry represents a common pool of ReactiveAdapter instances for different reactive types. In turn, ReactiveAdapterRegistry provides a singleton instance (1) that might be used in many places within the framework or that may be employed in the developed application. Along with that, the registry makes it possible to register an adapter by providing the same list of parameters as in the previous example (2). Finally, we may get an existing adapter by providing the Java class to which the conversion should be done (3).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.138.202