I want to stream events multiple events, all inherited from the same base type, from a mongoDB, using the spring ReactiveMongoRepository
. Next I want to have them all differently handled and thus defined several overloads of a handle method, all for one child. However, the compiler complains, he can't find a proper method.
The problem is exemplarily shown in this test method:
@Test
public void polymorphismTest() {
this.createFlux()
.map(this::polymorphicMethod)
.subscribe();
}
private Flux<A> createFlux() {
A1 a1 = new A1();
a1.string1 = "foo";
A2 a2 = new A2();
a2.string2 = "bar";
return Flux.just(a1, a2);
}
private void polymorphicMethod(A1 a1) {
System.out.println(a1.string1);
}
private void polymorphicMethod(A2 a2) {
System.out.println(a2.string2);
}
I somehow understand the issue, since the compiler can't know I have a proper method for all inherited classes. However, it would be nice to have a solution similar to my approach, since it is (in my eyes) clean and readable.
I know, a solution would be to define the handle as an abstract method in the base type and implement it in the inherited classes, but this would break the functional approach of the rest of the application, plus events in a database should be POJOs.
I also would love to avoid the typical command pattern approach with one huge mapping of types to functions, but if there is no other idea this might be the solution.