filter

Helper for Java users of the find/filter methods.

Example

import static opensavvy.ktmongo.sync.Query.*;

collection.find(options(), filter(filter -> {
filter.eq(JavaField.of(User::name), "Bob");
})).toList();

Why?

On the JVM, lambdas are not first-class citizens: they are emulated using SAM conversions. This plays poorly with genericity, because Java's void type cannot be represented as part of a type parameter (it isn't a subtype of java.lang.Object—in fact, it isn't a type at all).

Kotlin doesn't have a concept of void, instead using a singleton called Unit. In Kotlin code, this is invisible because Unit is implicit, but Java consumers need to explicitly specify it:

collection.find(it -> Unit.INSTANCE, filter -> {
filter.eq(JavaField.of(User::name), "foo");
return Unit.INSTANCE;
})

To facilitate using Kotlin lambdas from Java, the Query class contains helpers to declare Java-style lambdas instead of using Kotlin-style lambdas directly, allowing us to write the following code:

import static opensavvy.ktmongo.sync.Query.*;

collection.find(options(), filter(filter -> {
filter.eq(JavaField.of(User::name), "foo");
}));