I am trying to get value from multiple Optional based on priority and condition Lets say in following two set of optional the activitys that can be return are Walking and Swimming. If there is any activity in either of the optional and if swimming is there, then swimming should get preference else walking. If there are no activities then this will return empty. I managed to write it but there are too many conditions and wanted to see if there is a smart way of avoiding so many conditions
public Optional<Activity> getActivity(){
Optional<Activity> activityWhenSunShines= getActivityWhenSunShiningForUser(u);
Optional<Activity> activityWhenDayIsGood= getActivityWhenDayIsGoodForUser(u);
if(activityWhenSunShines.isPresent() && Activity.SWIMMING == activityWhenSunShines.get()){
return activityWhenSunShines;
}else if(activityWhenDayIsGood.isPresent() && Activity.SWIMMING == activityWhenDayIsGood.get()){
return activityWhenDayIsGood;
}else if(activityWhenSunShines.isPresent()){
return activityWhenSunShines;
}else if(activityWhenDayIsGood.isPresent()){
return activityWhenSunShines;
}else{
return Optional.empty();
}
}