Search code examples
javajavafxreactfx

When to use "suppressWhen(...)" or "filter(...)"?


I have a question about the two methods "suppressWhen(...)" and "filter(...)" if it makes any difference to use them in a context e.g:

I have a boolean property like:

BooleanProperty bp = new SimpleBooleanProperty();

and I have a stream of values like:

var.values()...

Is there any difference when I want to filter the stream if the BooleanProperty bp is false like:

var.values().suppressWhen(bp.not())...

or

var.values().filter(val -> bp.getValue())...

OK its clear that "suppressWhen" creates an instance of "SuspendedWhenStream" and uses a "SuspendableEventStream" so maybe its better to ask what would make the most sense here?

Is it more non efficient that an extra "SuspendableEventStream" is created or something like that?


Solution

  • What would make the most sense here?

    Definitively using suppressWhen, simply because it is designed for that use case. You can directly provide a binding without any need to evaluate it manually. Using filter would be kind of a hack, because it is designed to decide for each element of the stream, not for the whole stream.

    Is it more [...] efficient[?]

    Well, to be absolutely sure, you would need to test the execution speed on your own, but I'm pretty sure that using suppressWhen is still more efficient:

    Even if the creation of a new stream (or a new object in general) may require extra resources first, those will be saved later on. Using filter, the condition must be evaluated for each new event element in the stream, but using suppressWhen, it will only be evaluated (automatically) when required.