Created
October 4, 2019 07:54
-
-
Save AHaliq/616b0b1a7439afc24347f4c31b520f72 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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