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();
Content copied to clipboard
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;
})
Content copied to clipboard
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");
}));
Content copied to clipboard