Skip to content

Instantly share code, notes, and snippets.

@AHaliq
Created October 13, 2019 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AHaliq/cf948272854c75267d4bf3a7af757eca to your computer and use it in GitHub Desktop.
Save AHaliq/cf948272854c75267d4bf3a7af757eca to your computer and use it in GitHub Desktop.
// my motivation to use enum is so you dont need to create new objects everytime a command is executed
// instead you simply call a static method. and to iterate through a list of static methods we use enums.
// So this is a typical enum u encounter all the time
enum MyEnum {
A, B, C;
}
// However enums are capable of having properties and methods!
// if u define a method in an enum all enums will have it
// e.g.
enum MyEnum {
A,B,C;
public void func() {
System.out.println("hi");
}
}
// MyEnum.A.func() prints 'hi'
// MyEnum.B.func() prints 'hi'
// MyEnum.C.func() prints 'hi'
// and if u want properties for each enum you can give it arguments in the constructor
// yes enums have constructors, normally java provides a default constructor but you can implement your own!
//e.g.
enum MyEnum {
A(1), B(2), C(3);
private int num;
MyEmum(int i) {
num = i;
}
public int func() {
System.out.println(num);
return num * 5;
}
}
// MyEnum.A.func() will print '1' and return 5
// MyEnum.B.func() will print '2' and return 10
// MyEnum.C.func() will print '3' and return 15
// so in our case our enums (Responses.java) has a property regex and a
// ResponseFunc which can be a lambda or instance that implements ResponseFunc interface
// it also has a call function which checks if an input string matches regex then uses input
// and state as arguments for the ResponseFunc lambda or instance
// so lets say i have a state and input i can process it like so:
State s = new State();
String input = "create deck german-deck";
Responses.BYE.call(input, s);
Responses.CREATECARD.call(input,s);
Responses.CREATEDECK.call(input,s);
// and so on...
// but instead of calling one by one we can use a for loop iterating all enums
for (Responses r : Responses.values()) {
r.call(input,s);
}
// now the code will check every enum even after a successful match.
// to exit the loop on success the call has to return a boolean indicating success
// so finally read the following and see if it makes sense:
// (be warned all of this is written on my phone and not checked by an ide to be valid java code)
public class Responder {
public static State takeInput(String input, State state) {
for (Responses r : Responses.values()) {
if (r.call(input, state)) {
break;
}
}
}
}
enum Responses {
COMMAND1("com1regex", (i,s) -> {
System.out.println("your input matches com1regex");
return true;
});
private String regex;
private ResponseFunc func;
Response(String r, ResponseFunc f) {
regex = r;
func = f;
}
public boolean call(String i, State s) {
if (i.matches(regex)) {
return func.funcCall(i, s);
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment