import java.util.HashMap;
import java.util.Map;
public final class Main {
private static final Runnable DO_NOTHING
= () -> {};
public Main() {
map = new HashMap<>();
map.put('h', () -> moveCursorLeft());
map.put('j', () -> moveCursorUp());
map.put('k', () -> moveCursorDown());
map.put('l', () -> moveCursorRight());
}
private void handleKeyInput() {
var c = getPressedKey();
// Of course, you can pass the method reference to an empty method
// instead of 'DO_NOTHING'.
var action = map.getOrDefault(c, DO_NOTHING);
action.run();
// In fact, you don't need 'action' and can write:
//
// map.getOrDefault(c, DO_NOTHING)
// .run();
}
/**
Returns the character of the pressed key.
If no key is pressed, blocks until any key is pressed.
@return
The character of the pressed key.
*/
private char getPressedKey() {
return 'a';
}
private void moveCursorRight() {}
private void moveCursorUp() {}
private void moveCursorDown() {}
private void moveCursorLeft() {}
public static void main
(String[] args
) { var m = new Main();
m.handleKeyInput();
}
}
aW1wb3J0IGphdmEudXRpbC5IYXNoTWFwOwppbXBvcnQgamF2YS51dGlsLk1hcDsKCnB1YmxpYyBmaW5hbCBjbGFzcyBNYWluIHsKCiAgICBwcml2YXRlIHN0YXRpYyBmaW5hbCBSdW5uYWJsZSBET19OT1RISU5HID0gKCkgLT4ge307CgogICAgcHJpdmF0ZSBmaW5hbCBNYXA8Q2hhcmFjdGVyLCBSdW5uYWJsZT4gbWFwOwoKICAgIHB1YmxpYyBNYWluKCkgewogICAgICAgIG1hcCA9IG5ldyBIYXNoTWFwPD4oKTsKICAgICAgICBtYXAucHV0KCdoJywgKCkgLT4gbW92ZUN1cnNvckxlZnQoKSk7CiAgICAgICAgbWFwLnB1dCgnaicsICgpIC0+IG1vdmVDdXJzb3JVcCgpKTsKICAgICAgICBtYXAucHV0KCdrJywgKCkgLT4gbW92ZUN1cnNvckRvd24oKSk7CiAgICAgICAgbWFwLnB1dCgnbCcsICgpIC0+IG1vdmVDdXJzb3JSaWdodCgpKTsKICAgIH0KCiAgICBwcml2YXRlIHZvaWQgaGFuZGxlS2V5SW5wdXQoKSB7CiAgICAgICAgdmFyIGM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