13

I have a spring project where i have a boolean method which uses optional to filter through a set of requests. Im getting an error when i try return request.isEmpty(), I looked it up and i might be using an older version of java but is there any alternative way to say request.isEmpty() without having to update my version of java as Im worried if i update it, it will affect the rest of my project

This is my code for the method

private boolean hasNoDaysOffOnShiftDate(List<Request> requests, ShiftParams params) {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // for string
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); // for localdate

    // Shift start date in LocalDate
    String shiftDate = (params.start).format(formatter);
    LocalDate formatDateTime = LocalDate.parse(shiftDate, formatter);
    System.out.println("Shift date in date format " + formatDateTime);

    Optional<Request> request = requests.stream().filter(req -> req.getStatus() == Status.Accepted)
            .filter(req -> isDayOffOnShiftDate(req, formatDateTime)).findAny();
    return request.isEmpty();

}

The error I am getting is

The method isEmpty() is undefined for the type Optional<Request>

I am using this version of java

java.version=1.8.0_73

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
Jane Ryan
  • 159
  • 1
  • 1
  • 8
  • 1
    funny that you use `SimpleDateFormat`, but at the same time `Optional` - which implies java-8... – Eugene May 24 '19 at 09:54
  • @JaneRyan Optional is not a List, it's also not a request. first execute .get() on the optional, then .isEmpty() – Stultuske May 24 '19 at 10:08
  • 1
    Not related to your problem, but your Java 8 version is pretty old. Compare Java 8 update 73, with the current latest Java 8 update 212. – Mark Rotteveel May 24 '19 at 12:21

1 Answers1

32

Optional#isEmpty() is a Java 11 method, which is a shortcut for Java 8's !Optional#isPresent().

return !request.isPresent();
Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142