0

I want the most optimized and simplest way to make this code in Java 8 :


(Authentication auth, HttpServletRequest request, ....)

...

Cookie cookies[] = request.getCookies();
if (cookies != null) {

    for (Cookie cookie : cookies) {
        if (cookie.getName().equals("_id")) {
            boolean bool_1 = isIdCookieValid(cookie, auth);
        }
        else if (cookie.getName().equals("XSRF-TOKEN")) {
            boolean bool_2 = isXSRFTokenValid(cookie, request);
        }
    }

    if (bool_1 && bool_2) {
        System.out.println("Ok");
    }
    // else if they are not both true do nothing
}

Maybe with the Stream class ?

mtnp
  • 314
  • 7
  • 24
  • 1
    if you want the fastest code, [using streams](https://stackoverflow.com/questions/22658322/java-8-performance-of-streams-vs-collections) doesn't seem like the way to do it – Kaan Jan 21 '20 at 23:47
  • Thanks for the tips, I didn't know this benchmark – mtnp Jan 21 '20 at 23:56

2 Answers2

1

You can complete the task early if you know that either:

  1. A result boolean is false
  2. You have calculated both booleans

Ergo:

// Use wrapped Booleans to distinguish between true, false and "no value yet"
Boolean bool_1 = null;
Boolean bool_2 = null;
Cookie cookies[] = request.getCookies();
if (cookies != null) {
    for (Cookie cookie : cookies) {
        if (cookie.getName().equals("_id")) {
            bool_1 = isIdCookieValid(cookie, auth);
            if (!bool_1) {
                break;
            }
        }
        else if (cookie.getName().equals("XSRF-TOKEN")) {
            bool_2 = isXSRFTokenValid(cookie, request);
            if (!bool_2) {
                break;
            }
        }
        if (bool_1 != null && bool_2 != null) {
            break;
        }
    }

    if (bool_1 != null && bool_1 != null && bool_1 && bool_2) {
        System.out.println("Ok");
    }
    // else if they are not both true do nothing
}
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Indeed, this could be faster than waiting the end loop thanks ! I wait more solutions (maybe more concise) before to mark this thread as solved. – mtnp Jan 21 '20 at 23:59
1

Perhaps there's a more simple way:

  • Simply replace your
Cookie cookies[] = request.getCookies();
if (cookies != null) {

    for (Cookie cookie : cookies) {
        if (cookie.getName().equals("_id")) {
            boolean bool_1 = isIdCookieValid(cookie, auth);
        }
        else if (cookie.getName().equals("XSRF-TOKEN")) {
            boolean bool_2 = isXSRFTokenValid(cookie, request);
        }
    }

    if (bool_1 && bool_2) {
        System.out.println("Ok");
    }
    // else if they are not both true do nothing
}
  • with
Cookie[] cookies = request.getCookies();
if (cookies != null) {
    boolean bool = Arrays.stream(cookies)
                         .filter(cookie -> 
                            (cookie.getName().equals("_id") ? 
                              isIdCookieValid(cookie, auth) : 
                              cookie.getName().equals("XSRF-TOKEN") && 
                                isXSRFTokenValid(cookie, request)))
                         .count() == 2;

    System.out.println(bool ? "Ok" : "")
}
dryleaf
  • 415
  • 3
  • 18
  • Thanks ! This is cleaner than my code. I'll check performance between this and the solution of Bohemian. – mtnp Jan 22 '20 at 10:56