abouttreesummaryrefslogcommitdiff
path: root/srcs/juloo.keyboard2
diff options
context:
space:
mode:
authorJules Aguillon2025-01-11 15:47:32 +0100
committerJules Aguillon2025-01-11 15:54:24 +0100
commit4f8b5fa6cee72feda63162c8aa71905ba893a71d (patch)
treef670e8d81b849c5ec4a73f47f945cfa1ebd13758 /srcs/juloo.keyboard2
parentc4e2b446e5d91fa122f9414818fd844aa15971fe (diff)
downloadunexpected-keyboard-4f8b5fa6cee72feda63162c8aa71905ba893a71d.tar.gz
unexpected-keyboard-4f8b5fa6cee72feda63162c8aa71905ba893a71d.zip
Refactor: Make KeyValue payload Comparable
and use 'toString()' instead of 'getSymbol()'. This removes unecessary casts when computing symbols and comparing key values. This will make adding other kind of keys easier.
Diffstat (limited to 'srcs/juloo.keyboard2')
-rw-r--r--srcs/juloo.keyboard2/KeyValue.java38
1 files changed, 21 insertions, 17 deletions
diff --git a/srcs/juloo.keyboard2/KeyValue.java b/srcs/juloo.keyboard2/KeyValue.java
index 7c75316..7e4558d 100644
--- a/srcs/juloo.keyboard2/KeyValue.java
+++ b/srcs/juloo.keyboard2/KeyValue.java
@@ -132,12 +132,10 @@ public final class KeyValue implements Comparable<KeyValue>
}
/**
- * The symbol that is rendered on the keyboard as a [String].
- * Except for keys of kind:
- * - [String], this is also the string to output.
- * - [Complex], this is an instance of [KeyValue.Complex].
+ * [_payload.toString()] is the symbol that is rendered on the keyboard.
+ * For keys of kind [String], this is also the string to output.
*/
- private final Object _payload;
+ private final Comparable _payload;
/** This field encodes three things: Kind, flags and value. */
private final int _code;
@@ -161,9 +159,7 @@ public final class KeyValue implements Comparable<KeyValue>
When [getKind() == Kind.String], also the string to send. */
public String getString()
{
- if (getKind() == Kind.Complex)
- return ((Complex)_payload).getSymbol();
- return (String)_payload;
+ return _payload.toString();
}
/** Defined only when [getKind() == Kind.Char]. */
@@ -255,6 +251,7 @@ public final class KeyValue implements Comparable<KeyValue>
return sameKey((KeyValue)obj);
}
+ @Override
public int compareTo(KeyValue snd)
{
// Compare the kind and value first, then the flags.
@@ -264,9 +261,7 @@ public final class KeyValue implements Comparable<KeyValue>
d = _code - snd._code;
if (d != 0)
return d;
- if (getKind() == Kind.Complex)
- return ((Complex)_payload).compareTo((Complex)snd._payload);
- return ((String)_payload).compareTo((String)snd._payload);
+ return _payload.compareTo(snd._payload);
}
/** Type-safe alternative to [equals]. */
@@ -289,7 +284,7 @@ public final class KeyValue implements Comparable<KeyValue>
return "[KeyValue " + getKind().toString() + "+" + getFlags() + "+" + value + " \"" + getString() + "\"]";
}
- private KeyValue(Object p, int kind, int value, int flags)
+ private KeyValue(Comparable p, int kind, int value, int flags)
{
if (p == null)
throw new NullPointerException("KeyValue payload cannot be null");
@@ -297,13 +292,12 @@ public final class KeyValue implements Comparable<KeyValue>
_code = (kind & KIND_BITS) | (flags & FLAGS_BITS) | (value & VALUE_BITS);
}
- public KeyValue(Complex p, Complex.Kind value, int flags)
+ private KeyValue(Complex p, Complex.Kind value, int flags)
{
- this((Object)p, (Kind.Complex.ordinal() << KIND_OFFSET), value.ordinal(),
- flags);
+ this(p, Kind.Complex, value.ordinal(), flags);
}
- public KeyValue(String p, Kind k, int v, int f)
+ public KeyValue(Comparable p, Kind k, int v, int f)
{
this(p, (k.ordinal() << KIND_OFFSET), v, f);
}
@@ -747,13 +741,15 @@ public final class KeyValue implements Comparable<KeyValue>
throw new RuntimeException("Assertion failure");
}
- public static abstract class Complex
+ public static abstract class Complex implements Comparable<Complex>
{
public abstract String getSymbol();
/** [compareTo] can assume that [snd] is an instance of the same class. */
+ @Override
public abstract int compareTo(Complex snd);
+ @Override
public boolean equals(Object snd)
{
if (snd instanceof Complex)
@@ -761,6 +757,12 @@ public final class KeyValue implements Comparable<KeyValue>
return false;
}
+ @Override
+ public String toString()
+ {
+ return getSymbol();
+ }
+
/** [hashCode] will be called on this class. */
/** The kind is stored in the [value] field of the key. */
@@ -780,8 +782,10 @@ public final class KeyValue implements Comparable<KeyValue>
_symbol = _sym;
}
+ @Override
public String getSymbol() { return _symbol; }
+ @Override
public int compareTo(Complex _snd)
{
StringWithSymbol snd = (StringWithSymbol)_snd;