I have Java enum:
public enum ConflictResolutionStrategy {
softResolve,
hardResolve,
}
I want to call it like ConflictResolutionStrategy.hardResolve.apply(case1, case2).
Both case1 and case2 objects of the same type. apply in my case should return nothing.
The basic idea behind this design. Create Strategy design pattern and resolve conflicts based on the set enum value.
I cannot find any similar questions on StackOveflow even simple search gives me tons of similar cases which don't resolve my case directly.
I tried The following:
public enum ConflictResolutionStrategy {
softResolve ((CaseType case1, CaseType case2) -> case1.update(case2)),
hardResolve,
}
This version above doesn't compile.
I tried another solution:
public enum ConflictResolutionStrategy {
softResolve,
hardResolve {
public void apply(CaseType case1, CaseType case2) {
case1.update(case2);
}
},
}
The second solution, works okay but requires too much code.
softResolveandhardResolveare actually solved in different ways, so you have to overrideapplyfor them to handle that in different ways... so "too much code" will be required, yessoftResolveandhardResolveare different values and should be treated differently. I do understand that different code should be in both cases. The main point of my question if I can write inlambda-like style way the same what works in the last my code sample? I want to reduce noise of the code buy using modernJavafeatures.