Search code examples
javajava-8functional-programmingrunnable

Return a function with an arbitrary number of input parameters


My code needs to pre-check a series of complex regex via various helper classes. Then, it needs to execute a series of function calls in a post-check fashion if all is ok. If the case construct doesn't catch something I need to log it for later.

I am trying to avoid having two honking great duplicated if statements.

I was thinking, if the big if statement (or switch) were to return a function, I could check whether that returned function was null or not to do the pre-check. If it were null, I could also log it. If it weren't null, I could call it directly. This way I can do away with the need to have the complex logic checking in two parts of the code.

I was thinking something along the lines of:

class Playground {
    public static Function getFunction(String condition) {
        switch (condition) {
            case "one":
                return Function one(1);
            case "two":
                return Function two("two",2);
            default:
                return null;
        }
    }
    public static void one(int i) {
        System.out.println("one: i: " + i);
    }

    public static void two(String s, int i) {
        System.out.println("two: s: " + s + " i: " + i);
    }
    public static void main(String[ ] args) {
       Function f1 = getFunction("one");
       Function f2 = getFunction("two");
       f1();
       f2();
    }
}

But I can't quite get the syntax right.

Can someone tell me if this is feasible in Java? If so, perhaps someone can advise on the syntax corrections.

  • All method called will have void return.
  • They'll be instance calls rather than static methods.
  • The function returned may have a differing number of input parameters. When the method gets invoked I need to somehow set that up as well.

If there is no such way, is there an alternative, perhaps a design pattern, that might help? (Other than mapping the complex if statement to something like an integer. If nothing is matched it's 0, otherwise you have values. Then you'd have another switch based on the int.)


Solution

  • It looks like you want to return a Runnable that calls a method:

    class Playground{
        public static Runnable getRunnable(String condition) {
            switch (condition) {
                case "one":
                    return () -> one(1);
                case "two":
                    return () -> two("two", 2);
                default:
                    return null;
            }
        }
        public static void one(int i) {
            System.out.println("one: i: " + i);
        }
    
        public static void two(String s, int i) {
            System.out.println("two: s: " + s + " i: " + i);
        }
        public static void main(String[ ] args) {
           Runnable f1 = getRunnable("one");
           Runnable f2 = getRunnable("two");
           Runnable f3 = getRunnable("three");
           f1.run();
           f2.run();
           if (f3 == null) {
               System.out.println("none");
           }
        }
    }