Optional.ifPresent and Optional.filter to log and filter the Scheduled Day according to holiday

Transcribed from the original LinkedIn image post.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import java.time.LocalDate;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class Day21 {
    public static void main(String[] args) throws Exception {
        Logger logger = LoggerFactory.getLogger(Day21.class);
        Optional<LocalDate> scheduleDay = ScheduleService.getAvailableScheduleDay();
        scheduleDay.ifPresent(localDate -> logger.info("Available Schedule day is {}", localDate));

        LocalDate scheduleDateToString = scheduleDay
            .filter(HolidayDateService::checkHoliday)
            .orElseThrow(() -> new NoHolidayException("No available Sync Time. Reschedule"));
    }
}

The original LinkedIn graphic is preserved below.

Day 21 LinkedIn post