How to implement BiFunctional function that corresponds to Enum in Java?
A function accepting two parameters and returning nothing is a BiConsumer
.
Your enum may implement BiConsumer
:
public enum ConflictResolutionStrategy implements BiConsumer<CaseType, CaseType> {
softResolve ((case1, case2) -> case1.update(case2)),
hardResolve((case1, case2) -> {/* do something else */});
private final BiConsumer<CaseType, CaseType> consumer;
ConflictResolutionStrategy(BiConsumer<CaseType, CaseType> consumer){
this.consumer = consumer;
}
@Override
public void accept(CaseType case1, CaseType case2) {
consumer.accept(case1, case2);
}
}
Suppose you have a method for processing your cases:
public void processCases(Collection<CaseType> cases, BiConsumer<CaseType, CaseType> conflictResolutionStrategy){
// ...
}
Now you can pass in either one of your existing strategies:
processCases(cases, ConflictResolutionStrategy.softResolve);
or an inline strategy:
processCases(cases, (c1, c2) -> { /* do smth here */ });