Search code examples
javalambdajava-8anonymous-functionanonymous-methods

Trying to do a minor refactor using lambdas in Java


I am working on part of a program (concerning speech recognition and a remote control car), where the code transmit(XXXXX); disableAutoMode(); is repeated many times. For curiosity's sake I would like to convert this into a lambda function similar to this var f = p -> transmit(p); disableAutoMode(); (forgive the var; I don't know what the type of the expression would be) and then call it in a way similar to this: f("s");, f("a"); and f("f"); or something similar to f.call("s");, f.call("a"); and f.call("f");.

What is the correct syntax to use a simple lambda function in Java similar to how I described above? (What should I put down as it's type instead of saying var?)

Here is the block of code, if you're curious:

@Override
public void onResult(Hypothesis hypothesis) {
    if (hypothesis != null) {
        String text = hypothesis.getHypstr();
        Log.i("onresult",text);
        ToastMaster(text);

        switch (text) {
            case "forward":
            case "go forward":
                transmit("f");
                disableAutoMode();
                break;
            case "go back":
            case "go backward":
            case "back":
            case "backward":
            case "reverse":
                transmit("b");
                disableAutoMode();
                break;
            case "skid left":
            case "go left":
                transmit("l");
                disableAutoMode();
                break;
            case "skid right":
            case "go right":
                transmit("r");
                disableAutoMode();
                break;
            case "turn left":
                transmit("q");
                disableAutoMode();
                break;
            case "turn right":
                transmit("e");
                disableAutoMode();
                break;
            case "reverse left":
                transmit("z");
                disableAutoMode();
                break;
            case "reverse right":
                transmit("x");
                disableAutoMode();
                break;
            case "stop":
                disableAutoMode();
                break;
            case "automatic":
                toggleAutoMode(null);
                break;
            case "enable automatic mode":
                enableAutoMode();
                break;
            case "disable automatic mode":
                disableAutoMode();
                break;
        }
    }
}

Solution

  • In this case you want a Consumer.

    Consumer<String> function = (x) -> { transmit(x); disableAutoMode(); };
    function.accept("hello!");
    

    However I'm not sure why you want to use a lambda expression here, you could just create a plain old method and call that.

    If you are after a more meaningful refactor one option would be to switch to a map of String, Action/Runnable. While you would end up with more code, the goal of refactors is not to "make it smaller" but to make it more readable/maintainable. By splitting each action into its own small self-contained class each action can be tested in isolation with minimal set-up. Each action can be reused (as it is just a class). With a good naming strategy it would be obvious to readers what is going on without having to dig through a large switch statement.