Here there is such a method that I use in order to make a pars immediately in the enam when I receive a response from the server

@JsonCreator public static eTaxCode getTaxCodeByIdOrDefault(int iTaxCode) { eTaxCode result = DEFAULT; for (eTaxCode tmp : eTaxCode.values()) { if (tmp.getTaxCodeId() == iTaxCode) { result = tmp; break; } } return result; } 

Everything is simple, I take the enam values ​​for each and if there is a match, then return this value if not, then return the default value.

Now I want to do the same with RxJava2 (I know that maybe this is not the case and that the standard approach is very cool, but it's interesting to understand how to do it)

I tried to do something like this (on the same example)

 @JsonCreator public static eSingDetails getSingDetails(int iSignDtls) { eSingDetails result = DEFAULT; Observable.fromArray(eSingDetails.values())// .filter(iValue -> iValue.getSignDtls() == iSignDtls)// .isEmpty()// .subscribe(iIsEmpty -> { if (iIsEmpty) { } else { } }); return result; } 

But still not what it turns out ...

How to do it right?

  • 2
    Rx doesn’t fit here at all, so you don’t get it. This is more of a challenge for stream api. - eugeneek
  • one
    @eugeneek I have heard several times that there are developers who are writing a completely reactive fitting (although I haven’t met such attachments yet) and I wonder how this method would have been written if I used the fully reactive approach - Aleksey Timoshchenko
  • Well, if just for the sake of interest, the method may not return immediately to eSingDetails , but an observable that is wrapped around it: Observable<eSingDetails> (or Flowable, Single, etc. depending on the task) so that you can use it further in the Rx chain , or just subscribe to the right place. But I would not do that. Any technology is only a tool, and they need to be used in the places intended for it. And the nails, as you know, can be hammered with a microscope. - eugeneek

1 answer 1

The function itself may look like this:

 public static Single<eTaxCode> getTaxCodeByIdOrDefault(int iTaxCode) { return Flowable.fromIterable(eTaxCode.values()) .takeUntil(value -> { return value.getTaxCodeId() == iTaxCode; }) .filter(value -> value.getTaxCodeId() == iTaxCode) .single(DEFAULT); } 

And the call accordingly:

 getTaxCodeByIdOrDefault(taxCodeId) .subscribe(taxCode -> { // что-то делаем }); 

Operator documentation takeUntil

Single Documentation

  • I understand that the takeUntil is needed here only to stop execution if the necessary element is found, right? - Aleksey Timoshchenko
  • @AlekseyTimoshchenko, precisely - A. Shakhov
  • Yes, but how to return not Single and object? As in my code, Rx is not everywhere and more convenient if the object returns, and not Single - Aleksey Timoshchenko
  • @AlekseyTimoshchenko, using Rx implies that entities in wrappers are streams to which you need to subscribe. I think that if you need to get the result in sync, then you don't need to apply the reactive approach - A. Shakhov