Skip to content

Instantly share code, notes, and snippets.

@AHaliq
Created October 4, 2019 07:54
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/616b0b1a7439afc24347f4c31b520f72 to your computer and use it in GitHub Desktop.
Save AHaliq/616b0b1a7439afc24347f4c31b520f72 to your computer and use it in GitHub Desktop.
package com.core;
public enum Response {
BYE("(?i)^b(ye)?\\s*", (i, s) -> {
Printer.printString("Bye. Hope to see you again soon!");
s.toExit = true;
s.lastError = false;
return true;
}),
RESPONDER_NAME("regex", (commandInput, programState) -> {
// carry out command
return true; // capture is valid, end checking other commands
return false; // even though match, keep checking other commands match
});
private String regex;
private ResponseFunc func;
Response(String r, ResponseFunc f) {
regex = r;
func = f;
}
/**
* Given a string and program state, if string matches regex this enum will call its response
* function.
*
* @param i input string
* @param s state object
* @return boolean if the string has matched
*/
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