You cannot subclass enum
in Java.
You need to define your own KeyCode enum. But I suspect you will not win much clarity here. You may want to encapsulate your if
command into an assert
method like so:
assertModifierKeyCode(KeyCode c) {
if(!c.isModifierKey()) throw new IllegalArgumentException();
}
Then your method is a little bit more concise about the meaning:
void doStuff(KeyCode c){
assertModifierKeyCode(c);
...
}
Of course you are free to check the keycode constraint before calling doStuff
, then you could have a ModifierCode
enum, that you misuse as a filter:
ModifierCode m = ModifierCode.fromKeyCode(c); // could throw Exception
doStuff( m );
...
void doStuff( ModifierCode m ) {
switch ( m ) {
case ...
}
To have the KeyCode
still available, you construct the ModifierCode
with the KeyCode
embedded:
public enum ModifierCode {
LSHIFT(KeyCode.LeftShift),
RSHIFT(KeyCode.RightSHift) // bear with me, I dont have KeyCode enum in memory
;
final private KeyCode keyCode;
private ModifierCode(KeyCode c) {
this.keyCode = c;
}
public KeyCode getKeyCode() { // maybe asKeyCode() would also be a nifty name :-)
return keyCode;
}
public static ModifierCode fromKeyCode(KeyCode c) {
for(ModifierCode m : values() ) {
if( m.keyCode == c ) {
return m;
}
}
throw IllegalArgumentException("No MOdifierCode with that KeyCode!");
}
}