0

I'm trying to implement a lambda expression in a switchMap callback but it complains about the parameter type and I've been unable to figure out why.

public class Notifier {

  public ReplaySubject emitter = ReplaySubject.create();

  private PublishSubject status = PublishSubject.create();
  private Observable<Long> interval$;
  private Disposable notificationSubscription;

  public Notifier() {

    interval$ = Observable
      .interval(5000, TimeUnit.MILLISECONDS)
      .concatMap((i) -> {

        Observable nextNotification$ = interval$
          .share()
          .take(1);

        //noinspection unchecked
        return status
          .startWith(new Boolean(true))
          .switchMap((Boolean value) -> {

            if (!value.booleanValue()) {
              return Observable.empty();
            }

            return Observable.timer(1000, TimeUnit.MILLISECONDS).zip(nextNotification$, (val) -> i);
          });
      });

    notificationSubscription = interval$.subscribe(this::newNotification);
  }
}

It's the .switchMap((Boolean value) -> { ... } line that complains.

I tried using a Consumer too but then I got other issues which was even worse.

How can I tweak this to work as expected?

Chrillewoodz
  • 27,055
  • 21
  • 92
  • 175
  • You have a very awkward code that makes little sense. Why do you reference interval$ from inside the concatMap? Combining `zip` with `timer` that way doesn't work. – akarnokd Mar 15 '19 at 13:52
  • @ArnaudDenoyelle Updated question with more code. `pipe` is not available as far as I'm aware? Isn't that a `RXJS` thing? – Chrillewoodz Mar 15 '19 at 13:58
  • @Chrillewoodz My bad, I misread the question. – Arnaud Denoyelle Mar 15 '19 at 13:59
  • @akarnokd I know it's a tad awkward. Only just started Java and trying to rewrite a `RxJS` snippet to `rxJava` but I haven't been able to test it yet due to this compile error. Basically it logs numbers to a server and when you type it should pause/start. That's what I'm trying to achieve. – Chrillewoodz Mar 15 '19 at 14:00
  • 1
    PublishSubject is a generic type. But you use it as a raw type. Don't. https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it – JB Nizet Mar 15 '19 at 14:19
  • Raw types everywhere, `ReplaySubject emitter`, `PublishSubject status`, and `Observable nextNotification$`. Besides that, don’t use expressions like `new Boolean(true)`. Use `Boolean.TRUE` or just `true`. – Holger Mar 15 '19 at 14:22

0 Answers0