From fecc4dd70ae8e5f2fc40e9679c99b306768cf603 Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Sun, 29 Jan 2023 19:38:54 +0100 Subject: [PATCH] Remove the explicit hashmap in KeyValue Refactoring. Predefined keys are represented by a big switch statement rather than added into a hashmap. --- srcs/juloo.keyboard2/KeyValue.java | 232 ++++++++++++++--------------- 1 file changed, 113 insertions(+), 119 deletions(-) diff --git a/srcs/juloo.keyboard2/KeyValue.java b/srcs/juloo.keyboard2/KeyValue.java index 8d12518..21a3baf 100644 --- a/srcs/juloo.keyboard2/KeyValue.java +++ b/srcs/juloo.keyboard2/KeyValue.java @@ -193,8 +193,6 @@ final class KeyValue return _symbol.hashCode() + _code; } - private static HashMap keys = new HashMap(); - public KeyValue(String s, int kind, int value, int flags) { check((kind & ~KIND_BITS) == 0); @@ -209,69 +207,53 @@ final class KeyValue this(s, (k.ordinal() << 29), v, f); } - public static KeyValue getKeyByName(String name) + private static KeyValue charKey(String symbol, char c, int flags) { - KeyValue kv = keys.get(name); - if (kv != null) - return kv; - if (name.length() == 1) - return new KeyValue(name, Kind.Char, name.charAt(0), 0); - else - return new KeyValue(name, Kind.String, 0, 0); + return new KeyValue(symbol, Kind.Char, c, flags); } - private static void addKey(String name, String symbol, Kind kind, int code, int flags) - { - keys.put(name, new KeyValue(symbol, kind, code, flags)); - } - - private static void addCharKey(String name, String symbol, char c, int flags) - { - addKey(name, symbol, Kind.Char, c, flags); - } - - private static void addModifierKey(String name, String symbol, Modifier m, int flags) + private static KeyValue modifierKey(String symbol, Modifier m, int flags) { if (symbol.length() > 1) flags |= FLAG_SMALLER_FONT; - addKey(name, symbol, Kind.Modifier, m.ordinal(), + return new KeyValue(symbol, Kind.Modifier, m.ordinal(), FLAG_LATCH | FLAG_SPECIAL | FLAG_SECONDARY | flags); } - private static void addModifierKey(String name, int symbol, Modifier m, int flags) + private static KeyValue modifierKey(int symbol, Modifier m, int flags) { - addModifierKey(name, String.valueOf((char)symbol), m, flags | FLAG_KEY_FONT); + return modifierKey(String.valueOf((char)symbol), m, flags | FLAG_KEY_FONT); } - private static void addDiacritic(String name, int symbol, Modifier m) + private static KeyValue diacritic(int symbol, Modifier m) { - addKey(name, String.valueOf((char)symbol), Kind.Modifier, m.ordinal(), + return new KeyValue(String.valueOf((char)symbol), Kind.Modifier, m.ordinal(), FLAG_LATCH | FLAG_SPECIAL | FLAG_KEY_FONT); } - private static void addEventKey(String name, String symbol, Event e, int flags) + private static KeyValue eventKey(String symbol, Event e, int flags) { - addKey(name, symbol, Kind.Event, e.ordinal(), flags | FLAG_SPECIAL | FLAG_SECONDARY); + return new KeyValue(symbol, Kind.Event, e.ordinal(), flags | FLAG_SPECIAL | FLAG_SECONDARY); } - private static void addEventKey(String name, int symbol, Event e, int flags) + private static KeyValue eventKey(int symbol, Event e, int flags) { - addEventKey(name, String.valueOf((char)symbol), e, flags | FLAG_KEY_FONT); + return eventKey(String.valueOf((char)symbol), e, flags | FLAG_KEY_FONT); } - private static void addKeyeventKey(String name, String symbol, int code, int flags) + private static KeyValue keyeventKey(String symbol, int code, int flags) { - addKey(name, symbol, Kind.Keyevent, code, flags | FLAG_SECONDARY); + return new KeyValue(symbol, Kind.Keyevent, code, flags | FLAG_SECONDARY); } - private static void addKeyeventKey(String name, int symbol, int code, int flags) + private static KeyValue keyeventKey(int symbol, int code, int flags) { - addKeyeventKey(name, String.valueOf((char)symbol), code, flags | FLAG_KEY_FONT); + return keyeventKey(String.valueOf((char)symbol), code, flags | FLAG_KEY_FONT); } - private static void addEditingKey(String name, String symbol, Editing action) + private static KeyValue editingKey(String symbol, Editing action) { - addKey(name, symbol, Kind.Editing, action.ordinal(), + return new KeyValue(symbol, Kind.Editing, action.ordinal(), FLAG_SPECIAL | FLAG_SECONDARY | FLAG_SMALLER_FONT); } @@ -280,98 +262,110 @@ final class KeyValue /** Use a unique id as the value because the symbol is shared between every placeholders (it is the empty string). */ - private static void addPlaceholderKey(String name) + private static KeyValue placeholderKey() { - addKey(name, "", Kind.String, placeholder_unique_id++, 0); + return new KeyValue("", Kind.String, placeholder_unique_id++, 0); } - static + private static KeyValue fallbackMakeKey(String name) { - addModifierKey("shift", 0x0A, Modifier.SHIFT, 0); - addModifierKey("ctrl", "Ctrl", Modifier.CTRL, 0); - addModifierKey("alt", "Alt", Modifier.ALT, 0); - addDiacritic("accent_aigu", 0x50, Modifier.AIGU); - addDiacritic("accent_caron", 0x51, Modifier.CARON); - addDiacritic("accent_cedille", 0x52, Modifier.CEDILLE); - addDiacritic("accent_circonflexe", 0x53, Modifier.CIRCONFLEXE); - addDiacritic("accent_grave", 0x54, Modifier.GRAVE); - addDiacritic("accent_macron", 0x55, Modifier.MACRON); - addDiacritic("accent_ring", 0x56, Modifier.RING); - addDiacritic("accent_tilde", 0x57, Modifier.TILDE); - addDiacritic("accent_trema", 0x58, Modifier.TREMA); - addDiacritic("accent_ogonek", 0x59, Modifier.OGONEK); - addDiacritic("accent_dot_above", 0x5A, Modifier.DOT_ABOVE); - addDiacritic("accent_double_aigu", 0x5B, Modifier.DOUBLE_AIGU); - addDiacritic("accent_slash", 0x5C, Modifier.SLASH); - addDiacritic("accent_arrow_right", 0x5D, Modifier.ARROW_RIGHT); - addDiacritic("accent_breve", 0x5E, Modifier.BREVE); - addDiacritic("accent_bar", 0x5F, Modifier.BAR); - addModifierKey("superscript", "Sup", Modifier.SUPERSCRIPT, 0); - addModifierKey("subscript", "Sub", Modifier.SUBSCRIPT, 0); - addModifierKey("ordinal", "Ord", Modifier.ORDINAL, 0); - addModifierKey("arrows", "Arr", Modifier.ARROWS, 0); - addModifierKey("box", "Box", Modifier.BOX, 0); - addModifierKey("fn", "Fn", Modifier.FN, 0); - addModifierKey("meta", "Meta", Modifier.META, 0); + if (name.length() == 1) + return new KeyValue(name, Kind.Char, name.charAt(0), 0); + else + return new KeyValue(name, Kind.String, 0, 0); + } - addEventKey("config", 0x04, Event.CONFIG, FLAG_SMALLER_FONT); - addEventKey("switch_text", "ABC", Event.SWITCH_TEXT, FLAG_SMALLER_FONT); - addEventKey("switch_numeric", "123+", Event.SWITCH_NUMERIC, FLAG_SMALLER_FONT); - addEventKey("switch_emoji", 0x01, Event.SWITCH_EMOJI, FLAG_SMALLER_FONT); - addEventKey("switch_back_emoji", "ABC", Event.SWITCH_BACK_EMOJI, 0); - addEventKey("switch_second", 0x13, Event.SWITCH_SECOND, FLAG_SMALLER_FONT); - addEventKey("switch_second_back", 0x14, Event.SWITCH_SECOND_BACK, FLAG_SMALLER_FONT); - addEventKey("switch_greekmath", "πλ∇¬", Event.SWITCH_GREEKMATH, FLAG_SMALLER_FONT); - addEventKey("change_method", 0x09, Event.CHANGE_METHOD, FLAG_SMALLER_FONT); - addEventKey("action", "Action", Event.ACTION, FLAG_SMALLER_FONT); // Will always be replaced - addEventKey("capslock", 0x12, Event.CAPS_LOCK, 0); + public static KeyValue getKeyByName(String name) + { + switch (name) + { + case "shift": return modifierKey(0x0A, Modifier.SHIFT, 0); + case "ctrl": return modifierKey("Ctrl", Modifier.CTRL, 0); + case "alt": return modifierKey("Alt", Modifier.ALT, 0); + case "accent_aigu": return diacritic(0x50, Modifier.AIGU); + case "accent_caron": return diacritic(0x51, Modifier.CARON); + case "accent_cedille": return diacritic(0x52, Modifier.CEDILLE); + case "accent_circonflexe": return diacritic(0x53, Modifier.CIRCONFLEXE); + case "accent_grave": return diacritic(0x54, Modifier.GRAVE); + case "accent_macron": return diacritic(0x55, Modifier.MACRON); + case "accent_ring": return diacritic(0x56, Modifier.RING); + case "accent_tilde": return diacritic(0x57, Modifier.TILDE); + case "accent_trema": return diacritic(0x58, Modifier.TREMA); + case "accent_ogonek": return diacritic(0x59, Modifier.OGONEK); + case "accent_dot_above": return diacritic(0x5A, Modifier.DOT_ABOVE); + case "accent_double_aigu": return diacritic(0x5B, Modifier.DOUBLE_AIGU); + case "accent_slash": return diacritic(0x5C, Modifier.SLASH); + case "accent_arrow_right": return diacritic(0x5D, Modifier.ARROW_RIGHT); + case "accent_breve": return diacritic(0x5E, Modifier.BREVE); + case "accent_bar": return diacritic(0x5F, Modifier.BAR); + case "superscript": return modifierKey("Sup", Modifier.SUPERSCRIPT, 0); + case "subscript": return modifierKey("Sub", Modifier.SUBSCRIPT, 0); + case "ordinal": return modifierKey("Ord", Modifier.ORDINAL, 0); + case "arrows": return modifierKey("Arr", Modifier.ARROWS, 0); + case "box": return modifierKey("Box", Modifier.BOX, 0); + case "fn": return modifierKey("Fn", Modifier.FN, 0); + case "meta": return modifierKey("Meta", Modifier.META, 0); - addKeyeventKey("esc", "Esc", KeyEvent.KEYCODE_ESCAPE, FLAG_SMALLER_FONT); - addKeyeventKey("enter", 0x0E, KeyEvent.KEYCODE_ENTER, 0); - addKeyeventKey("up", 0x05, KeyEvent.KEYCODE_DPAD_UP, 0); - addKeyeventKey("right", 0x06, KeyEvent.KEYCODE_DPAD_RIGHT, 0); - addKeyeventKey("down", 0x07, KeyEvent.KEYCODE_DPAD_DOWN, 0); - addKeyeventKey("left", 0x08, KeyEvent.KEYCODE_DPAD_LEFT, 0); - addKeyeventKey("page_up", 0x02, KeyEvent.KEYCODE_PAGE_UP, 0); - addKeyeventKey("page_down", 0x03, KeyEvent.KEYCODE_PAGE_DOWN, 0); - addKeyeventKey("home", 0x0B, KeyEvent.KEYCODE_MOVE_HOME, 0); - addKeyeventKey("end", 0x0C, KeyEvent.KEYCODE_MOVE_END, 0); - addKeyeventKey("backspace", 0x11, KeyEvent.KEYCODE_DEL, 0); - addKeyeventKey("delete", 0x10, KeyEvent.KEYCODE_FORWARD_DEL, 0); - addKeyeventKey("insert", "Ins", KeyEvent.KEYCODE_INSERT, FLAG_SMALLER_FONT); - addKeyeventKey("f1", "F1", KeyEvent.KEYCODE_F1, 0); - addKeyeventKey("f2", "F2", KeyEvent.KEYCODE_F2, 0); - addKeyeventKey("f3", "F3", KeyEvent.KEYCODE_F3, 0); - addKeyeventKey("f4", "F4", KeyEvent.KEYCODE_F4, 0); - addKeyeventKey("f5", "F5", KeyEvent.KEYCODE_F5, 0); - addKeyeventKey("f6", "F6", KeyEvent.KEYCODE_F6, 0); - addKeyeventKey("f7", "F7", KeyEvent.KEYCODE_F7, 0); - addKeyeventKey("f8", "F8", KeyEvent.KEYCODE_F8, 0); - addKeyeventKey("f9", "F9", KeyEvent.KEYCODE_F9, 0); - addKeyeventKey("f10", "F10", KeyEvent.KEYCODE_F10, 0); - addKeyeventKey("f11", "F11", KeyEvent.KEYCODE_F11, FLAG_SMALLER_FONT); - addKeyeventKey("f12", "F12", KeyEvent.KEYCODE_F12, FLAG_SMALLER_FONT); - addKeyeventKey("tab", 0x0F, KeyEvent.KEYCODE_TAB, FLAG_SMALLER_FONT); + case "config": return eventKey(0x04, Event.CONFIG, FLAG_SMALLER_FONT); + case "switch_text": return eventKey("ABC", Event.SWITCH_TEXT, FLAG_SMALLER_FONT); + case "switch_numeric": return eventKey("123+", Event.SWITCH_NUMERIC, FLAG_SMALLER_FONT); + case "switch_emoji": return eventKey(0x01, Event.SWITCH_EMOJI, FLAG_SMALLER_FONT); + case "switch_back_emoji": return eventKey("ABC", Event.SWITCH_BACK_EMOJI, 0); + case "switch_second": return eventKey(0x13, Event.SWITCH_SECOND, FLAG_SMALLER_FONT); + case "switch_second_back": return eventKey(0x14, Event.SWITCH_SECOND_BACK, FLAG_SMALLER_FONT); + case "switch_greekmath": return eventKey("πλ∇¬", Event.SWITCH_GREEKMATH, FLAG_SMALLER_FONT); + case "change_method": return eventKey(0x09, Event.CHANGE_METHOD, FLAG_SMALLER_FONT); + case "action": return eventKey("Action", Event.ACTION, FLAG_SMALLER_FONT); // Will always be replaced + case "capslock": return eventKey(0x12, Event.CAPS_LOCK, 0); - addCharKey("\\t", "\\t", '\t', 0); // Send the tab character - addCharKey("space", "\r", ' ', FLAG_KEY_FONT | FLAG_SECONDARY); - addCharKey("nbsp", "\u237d", '\u00a0', FLAG_SMALLER_FONT); + case "esc": return keyeventKey("Esc", KeyEvent.KEYCODE_ESCAPE, FLAG_SMALLER_FONT); + case "enter": return keyeventKey(0x0E, KeyEvent.KEYCODE_ENTER, 0); + case "up": return keyeventKey(0x05, KeyEvent.KEYCODE_DPAD_UP, 0); + case "right": return keyeventKey(0x06, KeyEvent.KEYCODE_DPAD_RIGHT, 0); + case "down": return keyeventKey(0x07, KeyEvent.KEYCODE_DPAD_DOWN, 0); + case "left": return keyeventKey(0x08, KeyEvent.KEYCODE_DPAD_LEFT, 0); + case "page_up": return keyeventKey(0x02, KeyEvent.KEYCODE_PAGE_UP, 0); + case "page_down": return keyeventKey(0x03, KeyEvent.KEYCODE_PAGE_DOWN, 0); + case "home": return keyeventKey(0x0B, KeyEvent.KEYCODE_MOVE_HOME, 0); + case "end": return keyeventKey(0x0C, KeyEvent.KEYCODE_MOVE_END, 0); + case "backspace": return keyeventKey(0x11, KeyEvent.KEYCODE_DEL, 0); + case "delete": return keyeventKey(0x10, KeyEvent.KEYCODE_FORWARD_DEL, 0); + case "insert": return keyeventKey("Ins", KeyEvent.KEYCODE_INSERT, FLAG_SMALLER_FONT); + case "f1": return keyeventKey("F1", KeyEvent.KEYCODE_F1, 0); + case "f2": return keyeventKey("F2", KeyEvent.KEYCODE_F2, 0); + case "f3": return keyeventKey("F3", KeyEvent.KEYCODE_F3, 0); + case "f4": return keyeventKey("F4", KeyEvent.KEYCODE_F4, 0); + case "f5": return keyeventKey("F5", KeyEvent.KEYCODE_F5, 0); + case "f6": return keyeventKey("F6", KeyEvent.KEYCODE_F6, 0); + case "f7": return keyeventKey("F7", KeyEvent.KEYCODE_F7, 0); + case "f8": return keyeventKey("F8", KeyEvent.KEYCODE_F8, 0); + case "f9": return keyeventKey("F9", KeyEvent.KEYCODE_F9, 0); + case "f10": return keyeventKey("F10", KeyEvent.KEYCODE_F10, 0); + case "f11": return keyeventKey("F11", KeyEvent.KEYCODE_F11, FLAG_SMALLER_FONT); + case "f12": return keyeventKey("F12", KeyEvent.KEYCODE_F12, FLAG_SMALLER_FONT); + case "tab": return keyeventKey(0x0F, KeyEvent.KEYCODE_TAB, FLAG_SMALLER_FONT); - addPlaceholderKey("removed"); - addPlaceholderKey("f11_placeholder"); - addPlaceholderKey("f12_placeholder"); + case "\\t": return charKey("\\t", '\t', 0); // Send the tab character + case "space": return charKey("\r", ' ', FLAG_KEY_FONT | FLAG_SECONDARY); + case "nbsp": return charKey("\u237d", '\u00a0', FLAG_SMALLER_FONT); - addEditingKey("copy", "copy", Editing.COPY); - addEditingKey("paste", "paste", Editing.PASTE); - addEditingKey("cut", "cut", Editing.CUT); - addEditingKey("selectAll", "s. all", Editing.SELECT_ALL); - addEditingKey("shareText", "share", Editing.SHARE); - addEditingKey("pasteAsPlainText", "", Editing.PASTE_PLAIN); - addEditingKey("undo", "undo", Editing.UNDO); - addEditingKey("redo", "redo", Editing.REDO); - addEditingKey("replaceText", "repl.", Editing.REPLACE); - addEditingKey("textAssist", "assist", Editing.ASSIST); - addEditingKey("autofill", "auto.", Editing.AUTOFILL); + case "removed": return placeholderKey(); + case "f11_placeholder": return placeholderKey(); + case "f12_placeholder": return placeholderKey(); + + case "copy": return editingKey("copy", Editing.COPY); + case "paste": return editingKey("paste", Editing.PASTE); + case "cut": return editingKey("cut", Editing.CUT); + case "selectAll": return editingKey("s. all", Editing.SELECT_ALL); + case "shareText": return editingKey("share", Editing.SHARE); + case "pasteAsPlainText": return editingKey("", Editing.PASTE_PLAIN); + case "undo": return editingKey("undo", Editing.UNDO); + case "redo": return editingKey("redo", Editing.REDO); + case "replaceText": return editingKey("repl.", Editing.REPLACE); + case "textAssist": return editingKey("assist", Editing.ASSIST); + case "autofill": return editingKey("auto.", Editing.AUTOFILL); + default: return fallbackMakeKey(name); + } } static final HashMap keys_descr = new HashMap();