I have following code which is supposed to return orgId from modelService but not working with Optional
public Long getOrgId(String someId) {
return this.getSpecialOrgId(someId).orElse(this.getById(someId).getOrgId());
}
private Optional<Long> getSpecialOrgId(String someId) {
return modelService.getModel(someId).map(Model::getOrgId).filter(this::isConditionTrue);
}
This code is not working getSpecialOrgId
always returns empty
But following code works well, not sure what's wrong with above code
private Long getOrgId(String someId) {
Optional<Long> orgIdFromModel = this.getSpecialOrgId(someId);
if (orgIdFromModel.isPresent()) {
return orgIdFromModel.get();
}
return this.getById(someId).getOrgId();
}
private Optional<Long> getSpecialOrgId(String someId) {
Optional<Model> modelOptional = modelService.getModel(someId);
if (modelOptional.isPresent()) {
Model model = modelOptional.get();
if (isConditionTrue(model.getOrgId())) {
return of(model.getOrgId());
}
}
return empty();
}
where isConditionTrue
is always true
, This is exact code, I have changed some variable names only