Part of my code looks like this:
EntityTypeEnum entityType = EntityTypeEnum.fromName(entityTypeName).orElseThrow(...);
EntityService<? extends AbstractEntityDto> entityService = entityServiceFactory.getEntityService(importType);
return getFooArgs(bar)
.map(entityService::getListWithFooArgs)
.orElseGet(()->entityService.getListNoArgs());
There is an enum by which I obtain service class using static method from entityServiceFactory. Then I invoke method getFooArgs(...), that returns Optional< FooArg >. Then I'd like to map it using EntityService method or use a method without args to obtain "default" value, when getFooArgs return empty Optional.
public interface EntityService<T extends AbstractEntityDto> {
List<T> getListNoArgs();
List<T> getListWithFooArgs(FooArg fooArgs);
}
Sadly I get Bad return type in lambda expression: List<capture of ? extends AbstractEntityDto> cannot be converted to List<capture of ? extends AbstractEntityDto> from my IDE. What I have to do is:
Optional<List<? extends AbstractEntityDto>> listOptional = getFooArgs(bar)
.map(entityService::getListWithFooArgs);
if(listOptional.isPresent())
return listOptional.get();
else
return entityService.getListNoArgs();
Can you explain why does it happen? I assume it's soemthing related to ? and generics, but I thought that by using ? a lot of type's restrictions would disappear.
If there is too little of code to figure it out, please let me know.