I have this function a:
public void a(BooleanSupplier param){}
that is called by function b:
public void b(Boolean param){
a(param)
}
The problem is that function "a" is expecting a BooleanSupplier but function b is sending a Boolean. I think I should convert a Boolean into a BooleanSupplier but I could not manage to convert one to another.
Let us take a closer look at the
BooleanSupplier-interface. This is a functional interface, i.e. it has only one abstract methodboolean getAsBoolean(). As we can see, the method has no parameters and returns aboolean.Now let us look at the code presented. Method
breceives one parameterBoolean param. methodareceives one parameter of typeBooleanSupplier. How can we convert theBooleanreceived bybto aBooleanSupplier? We just need to create a lambda that - when called - returnsparam. When written as lambda, this looks as follows:The minor type mismatch between
Boolean(type ofparam) andboolean(expected return-type ofBooleanSupplier) is resolved through autoboxing (oracle.com).So in total, we can now call
aas follows:For further information on lambdas and their syntax, I recommend reading a tutorial on the topic, e.g. this one from
oracle.com.