/**
* This class represents keystrokes.
*/
-public class TKeypress {
+public final class TKeypress {
// Various special keystrokes
/**
* 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)
* @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;
}
&& (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:
}
/**
- * 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;
}
}
/**
- * 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;
}