Reputation: 446
I want to map a String
to a method reference. The problem is that this method can return void
or boolean
.
I was using @FunctionalInterface
but seems it can have only one method (I would need one for void
and one for boolean
?).
Below is the code I have so far. The commented lines are what I would like to do.
@FunctionalInterface
interface MethodParser <T1, T2> {
public void apply(T1 arg1, T2 arg2);
}
public class OperatorParser {
public static Map<String, MethodParser> INTEGER = new HashMap<String, MethodParser>();
public static void buildInteger() {
MethodParser<Integer_, Integer> plus = (pObj, pValue) -> pObj.plus(pValue);
MethodParser<Integer_, Integer> minus = (pObj, pValue) -> pObj.minus(pValue);
// MethodParser<Integer_, Integer> grThan = (pObj, pValue) -> pObj.greaterThan(pValue);
// MethodParser<Integer_, Integer> lessTh = (pObj, pValue) -> pObj.lessThan(pValue);
OperatorParser.INTEGER.put("+", plus);
OperatorParser.INTEGER.put("-", minus);
// OperatorParser.INTEGER.put(">", grThan);
// OperatorParser.INTEGER.put("<", lessTh);
}
}
The object Integer_
represents a integer number and has void methods (plus(int)
and minus(int)
) that changes the value
of the object and boolean
methods (greaterThan(int)
and lessThan(int)
) that returns a value.
The MethodParser
interface is just one way I found to map a String to a method reference. I use it to map a given String to a method that already exists somewhere else.
Note: Some similar questions I have tried (these don't worked for me):
Upvotes: 0
Views: 192
Reputation: 1231
Make a method for void
and one for boolean
and a method that includes the condition that tells when to use either.
I don't know of any method that could have 2 return types.
You Can use a POJO class instance to return multiple values with grouping.
Upvotes: 1