I have several predifined static "processors" implementing the same method, for example:
default double process(double num){
Sample :
public class Test {
public static void main(String[] args) {
test(Test::processor1, 1d);
test(Test::processor2, 1d);
}
static double processor1(double num){
return num * 10;
}
static double processor2(double num){
return num * 20;
}
//...
static void test(Function<Double, Double> f, double d){
// Do something util here
System.out.println(f.apply(d));
}
...
Now imagine that I have some objects which can provide a list of additional "processors".
I'm trying to use an interface
to define those additional "processors".
static interface IProcessor{
double process(double num);
}
Implementation of an object with additional "processors":
static class SomeObject{
// Just return one but should be a list...
static IProcessor getExtraProccessors(){
return new IProcessor(){
public double process(double num){
return num * 30;
}
};
}
}
Up here everything compiles and works fine. But now I'm stuck.
Using SomeObject::getExtraProccessors
I have a reference on a static method returning an interface, how can I invoke the interface's method?
My first try was with
test(SomeObject::getExtraProccessors::process, 1d);
But this doesn't compile giving a The target type of this expression must be a functional interface
So please could you tell me if it possible to do this and if yes how? And if it's not possible how should I do it?