I'm struggling with a little java trick which is: how to pass a non void method with one parameter as a parameter (or two) of another method ? I guess this will involve Function or Biconsumer, and I've been trying with it. But I didn't manage to make it work... The exact context is quiet simple: I'm working on Fix messages with Quickfixj librairy and I need to call in the class Message:
- getInt(int tagValue) returning an int,
- getString(int tagValue) returning a String,
- getChar(int tagValue) returning a char.
I've been trying this:
public static < T > void setFieldsMap(int fieldTag,
Function< Integer, T > messageGetter,
String fieldKey ) throws FieldNotFound {
this.fieldsMap.put( fieldKey, String.valueOf( messageGetter.apply( fieldTag ) ) );
}
Which I wanted to call like that:
setFieldsMap( myTag, message::getString, "MY_KEY" );
But this doesn't compile. I'm getting the following error: "int is not a functional interface".
Of course I already checked Google but it didn't give me a fitting solution. I only found solution to call a method without parameter.
I've been using Function to call a getter without parameter, or Biconsumer to call a setter with a parameter. But it seems that calling a non void method with one parameter isn't that easy...
Does anyone has a clue to make this work? Thanks a lot for your time and your help guys! :)
fieldsMapand why would it accept a randomTas a value?Function<Integer, T> messageGetter = message::getString;(substituting the concrete type for whateverTis). This moves it out of potentially tricky contexts and tells you where the problem is.