In order to validate rules to answers from users to questions, consider the following set of Enums in a Java Spring Boot application; the validation is fired whenever a user answers a question in the frontend. The validation goes through all the rules (many more than documented here):
public enum AngebotValidationRule implements ValidationRule<Angebot> {
BERATUNG_KRANKENTAGEGELD_PFLICHTFELD(
FEHLERTEXT_BERATUNGSFRAGE_PFLICHTFELD,
MessageType.ERROR,
((angebot, services) ->
ToggleContextHolder.getToggleManager().isOn(Toggle.CROSS_SELLING)
&& angebot
.getOrCreateBeratungsfrageByType(BeratungsfrageType.KRANKENTAGEGELD)
.getAbsicherung()
== null),
false),
BERATUNG_KRANKENTAGEGELD_CROSSSELLING(
Constants.HINWEISTEXT_KRANKENTAGEGELD_CROSSSELLING,
MessageType.INFO,
((angebot, services) ->
(ToggleContextHolder.getToggleManager().isOn(Toggle.CROSS_SELLING)
&& angebot
.getOrCreateBeratungsfrageByType(BeratungsfrageType.KRANKENTAGEGELD)
.getAbsicherung()
== BeratungAbsicherung.GEWUENSCHT),
false),
BERATUNG_KRANKENTAGEGELD_CROSSSELLING_MONOLITH(
Constants.HINWEISTEXT_KRANKENTAGEGELD_CROSSSELLING_MONOLITH,
MessageType.INFO,
((angebot, services) ->
(ToggleContextHolder.getToggleManager().isOff(Toggle.CROSS_SELLING))
&& angebot
.getOrCreateBeratungsfrageByType(BeratungsfrageType.KRANKENTAGEGELD)
.getAbsicherung()
== BeratungAbsicherung.GEWUENSCHT),
false),
static class Constants {
public static final String FEHLERTEXT_BERATUNGSFRAGE_PFLICHTFELD =
"Some terrific text here!";
public static final String HINWEISTEXT_KRANKENTAGEGELD_CROSSSELLING =
"Some other terrific text here!";
public static final String HINWEISTEXT_KRANKENTAGEGELD_CROSSSELLING_MONOLITH =
"Another terrific text here";
}
}
I need to implement an if-else or switch-condition in the Enum BERATUNG_KRANKENTAGEGELD_CROSSSELLING, so that - whenever a ToggleManager is on or off - a different text is displayed:
BERATUNG_KRANKENTAGEGELD_CROSSSELLING(
Constants.HINWEISTEXT_KRANKENTAGEGELD_CROSSSELLING,
MessageType.INFO,
((angebot, services) ->
(ToggleContextHolder.getToggleManager().isOn(Toggle.CROSS_SELLING)
&& angebot
.getOrCreateBeratungsfrageByType(BeratungsfrageType.KRANKENTAGEGELD)
.getAbsicherung()
== BeratungAbsicherung.GEWUENSCHT),
false),
BERATUNG_KRANKENTAGEGELD_CROSSSELLING(
Constants.HINWEISTEXT_KRANKENTAGEGELD_CROSSSELLING_MONOLITH,
MessageType.INFO,
((angebot, services) ->
(ToggleContextHolder.getToggleManager().isOff(Toggle.CROSS_SELLING))
&& angebot
.getOrCreateBeratungsfrageByType(BeratungsfrageType.KRANKENTAGEGELD)
.getAbsicherung()
== BeratungAbsicherung.GEWUENSCHT),
false),
I need to combine the Logic in these two Validation Rules with the same name, as I cannot have two Enums with the same name in a Set of Enums.
How can I achieve that? Any hints or help would be very much appreciated, thanks!
CodePudding user response:
Use private constructor inside enum and create properties or you can use function too
enum Rule{
RULE_1(ToggleManager.context);
String text;
String getText(){
// any logic
return text;
}
private Rule(ToggleManager context){
text = context.isOn ? "on" : "off";
}
}
All the constants in enum are objects, like class you can do any thing using these objects
to access property Rule.RULE_1.text