Search code examples
javalambdaoption-typeflatmap

Bad return type in method reference: Cannot convert Employee to Optional<U>


I am trying to write a lambda function that gets employee location preference and have the code sample below.

But for my lambda function I get a compilation error at flatMap(this::buildEmployeeGeolocation) saying Bad return type in method reference: cannot convert com.abc.EmployeeGeolocation to java.util.Optional<U>.

What am I missing here?

public Optional<EmployeeGeolocation> getEmployee(final SessionId sessionId) {
    return Optional.ofNullable(employeePreferencesStore.getEmployeeAccountPreferences(sessionId))
            .map(preferences -> preferences.getPreference(PreferenceKey.Location))
            .filter(StringUtils::isNotBlank)
            .map(this::readEmployeelocation)
            .flatMap(this::buildEmployeeGeolocation);
}

private Optional<EncryptedGeolocation> readEmployeeLocation(@NonNull final String encryptedGeolocation) {
    try {
        return Optional.ofNullable(objectMapper.readValue(encryptedGeolocation, EmployeeGeolocation.class));
    } catch (final IOException e) {
        log.error("Error while reading the encrypted geolocation");
        throw new RuntimeException(e);
    }
}

private EmployeeGeolocation buildEmployeeGeolocation(@NonNull final EncryptedGeolocation unditheredEncryptedGeolocation) {
    return EmployeeGeolocation.builder()
            .latitude(10.0)
            .longitude(10.0)
            .accuracy(1.0)
            .locationType(ADDRESS)
            .build();
}

Solution

  • It seems like what you really need to do is swap the map and flatMap. Change the code

    .map(this::readEmployeeLocation) 
    .flatMap(this::buildEmployeeGeolocation);
    

    to

    .flatMap(this::readEmployeeLocation) // since you already have an Optional<String>
    .map(this::buildEmployeeGeolocation); // above results in Optional<EncryptedGeolocation>
    

    Important: Inferred from the code Optional.ofNullable(...).map(...).filter(StringUtils::isNotBlank), that it would result in an Optional<String> until this operation.