X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2FTKeypress.java;h=8143f151006af3fc656a8188d66b6d660c576802;hb=091d8a06c7f9f91c2a15c9350b6502cf4c10f5a3;hp=aeaadb0023e33cf2dbfca79b8cb6d1d440df8e18;hpb=7b5261bc5b641e0769902f014e3b21f61050b02b;p=fanfix.git diff --git a/src/jexer/TKeypress.java b/src/jexer/TKeypress.java index aeaadb0..8143f15 100644 --- a/src/jexer/TKeypress.java +++ b/src/jexer/TKeypress.java @@ -33,7 +33,7 @@ package jexer; /** * This class represents keystrokes. */ -public class TKeypress { +public final class TKeypress { // Various special keystrokes @@ -175,35 +175,89 @@ public class TKeypress { /** * If true, ch is meaningless, use fnKey instead. */ - public boolean isKey; + private boolean isKey; + + /** + * Getter for isKey. + * + * @return if true, ch is meaningless, use fnKey instead + */ + public boolean getIsKey() { + return isKey; + } /** * Will be set to F1, F2, HOME, END, etc. if isKey is true. */ - public int fnKey; + private int fnKey; + + /** + * Getter for fnKey. + * + * @return fnKey value (only valid is isKey is true) + */ + public int getFnKey() { + return fnKey; + } /** * Keystroke modifier ALT. */ - public boolean alt; + private boolean alt; + + /** + * Getter for ALT. + * + * @return alt value + */ + public boolean getAlt() { + return alt; + } /** * Keystroke modifier CTRL. */ - public boolean ctrl; + private boolean ctrl; + + /** + * Getter for CTRL. + * + * @return ctrl value + */ + public boolean getCtrl() { + return ctrl; + } /** * Keystroke modifier SHIFT. */ - public boolean shift; + private boolean shift; + + /** + * Getter for SHIFT. + * + * @return shift value + */ + public boolean getShift() { + return shift; + } /** * The character received. */ - public char ch; + private char ch; + + /** + * Getter for character. + * + * @return the character (only valid if isKey is false) + */ + public char getCh() { + return ch; + } /** - * Convenience constructor for the pre-defined instances. + * Public constructor makes an immutable instance. * * @param isKey is true, this is a function key * @param fnKey the function key code (only valid if isKey is true) @@ -230,7 +284,7 @@ public class TKeypress { * @return true if all fields are equal */ @Override - public final boolean equals(Object rhs) { + public boolean equals(final Object rhs) { if (!(rhs instanceof TKeypress)) { return false; } @@ -244,13 +298,32 @@ public class TKeypress { && (shift == that.shift)); } + /** + * Hashcode uses all fields in equals(). + * + * @return the hash + */ + @Override + public int hashCode() { + int A = 13; + int B = 23; + int hash = A; + hash = (B * hash) + (isKey ? 1 : 0); + hash = (B * hash) + fnKey; + hash = (B * hash) + (int)ch; + hash = (B * hash) + (alt ? 1 : 0); + hash = (B * hash) + (ctrl ? 1 : 0); + hash = (B * hash) + (shift ? 1 : 0); + return hash; + } + /** * Make human-readable description of this TKeypress. * * @return displayable String */ @Override - public final String toString() { + public String toString() { if (isKey) { switch (fnKey) { case F1: @@ -417,16 +490,14 @@ public class TKeypress { } /** - * Convert a keypress to lowercase. Function keys and ctrl keys are not - * converted. + * Convert a keypress to lowercase. Function keys and alt/ctrl keys are + * not converted. * - * @param key keypress to convert * @return a new instance with the key converted */ - public static TKeypress toLower(final TKeypress key) { - TKeypress newKey = new TKeypress(key.isKey, key.fnKey, key.ch, - key.alt, key.ctrl, key.shift); - if (!(key.isKey) && (key.ch >= 'A') && (key.ch <= 'Z') && (!key.ctrl)) { + public TKeypress toLowerCase() { + TKeypress newKey = new TKeypress(isKey, fnKey, ch, alt, ctrl, shift); + if (!isKey && (ch >= 'A') && (ch <= 'Z') && !ctrl && !alt) { newKey.shift = false; newKey.ch += 32; } @@ -434,16 +505,14 @@ public class TKeypress { } /** - * Convert a keypress to uppercase. Function keys and ctrl keys are not - * converted. + * Convert a keypress to uppercase. Function keys and alt/ctrl keys are + * not converted. * - * @param key keypress to convert * @return a new instance with the key converted */ - public static TKeypress toUpper(final TKeypress key) { - TKeypress newKey = new TKeypress(key.isKey, key.fnKey, key.ch, - key.alt, key.ctrl, key.shift); - if (!(key.isKey) && (key.ch >= 'a') && (key.ch <= 'z') && (!key.ctrl)) { + public TKeypress toUpperCase() { + TKeypress newKey = new TKeypress(isKey, fnKey, ch, alt, ctrl, shift); + if (!isKey && (ch >= 'a') && (ch <= 'z') && !ctrl && !alt) { newKey.shift = true; newKey.ch -= 32; }