I am using lamdbas so I can consistently set the properties of a ModelObject according to the values I can retrieve from three different objects. The code works like this:
public class Processor {
private void bar(Setter setter, MyClass myObject) {
String variable = myObject.getStringByABunchOfMethods();
setter.setVariable(variable);
}
protected void foo(...) {
...
bar(value -> model.setA(CONSTANT, value), aObject);
bar(value -> model.setB(value), bObject);
bar(value -> model.setC(value), cObject);
...
}
private interface Setter {
public void setVariable(String string);
}
}
public interface IModel {
public void setA(String arg0, String arg1);
public void setB(String arg0);
public void setC(String arg0);
}
I have read here that it is possible to rewrite bar(value -> model.setB(value), bObject);
to bar(model::setB, bObject)
. I think this looks better and more concise, but I haven't found a way to rewrite the setA
method to a double :: notation. Can anyone tell me if this is possible, and if so: how is this possible?
from https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html and https://www.codementor.io/eh3rrera/tutorials/using-java-8-method-reference-du10866vx
There would be 4 different kinds of method references. The corresponding lambda and method reference:
The lambda value -> model.setA(CONSTANT, value) does not correspond with any of the lambdas above, so it is not possible to rewrite it as a method reference.
To use the double colon notation, the method that you're referencing must have the same signature as the required method. So you can't use ::
unless you change your IModel
:
You can add an overload of setA
in IModel
:
default void setA(String arg0) {
setA(CONSTANT, arg0);
}
Then, you can reference that overload:
bar(model::setA, aObject);
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With