-2

I have below block of code

if(Objects.nonNull(isMine)) {
    if (isMine) {
             this.books= // gets it from the database;
    } else  {
            this. books= // gets it from the database
    }

 } else {
     this. books = // gets it from the database
 }

isMine - is a Boolean object I tried with switch case by converting isMine to a string as below

 String.valueOf(isMine) 

But didn't work .Suggest a better and faster way to implement above code in java.

Raj gopal
  • 114
  • 3
  • 12
  • 5
    What do you mean by faster? It looks fine. You could make a bit easier to read by inverting the null check (see Flown's answer), but other than that... – Malte Hartwig Sep 19 '17 at 08:48
  • you can remove first if statement as boolean cannot be null. – Sam Sep 19 '17 at 08:50
  • 2
    @Sam a `Boolean` (empasis uppercase) can be null – Malte Hartwig Sep 19 '17 at 08:51
  • 1
    @Sam since there is a boxed version, you can. – Flown Sep 19 '17 at 08:51
  • 1
    @Sam, it can if he's using the wrapper object. – Jan B. Sep 19 '17 at 08:51
  • correct Matt. I was assuming it as wrapper. – Sam Sep 19 '17 at 08:54
  • 2
    What do you mean with “But didn't work”? Be more specific. I don’t see any reason why `String.valueOf(isMine)` shouldn’t work. Besides that, why do you have a conditional at all? In all cases, you are doing the same `this. books= // gets it from the database`… – Holger Sep 19 '17 at 09:48

5 Answers5

6

You can flatten your if-else statement by using else if:

if(isMine == null) {
  books = allList;
} else if(isMine) {
  books = myList;
} else {
  books = notMyList;
}

Another approach would be to sperate the checks into methods:

  public List<Book> getBookList(Boolean isMine) {
    return isMine == null ? allList : getBookList(isMine.booleanValue());
  }

  public List<Book> getBookList(boolean isMine) {
    return isMine ? myList : notMyList;
  }
Flown
  • 11,480
  • 3
  • 45
  • 62
1

this.books = isMine != null ? (isMine ? this.myList : this.notMyList) : this.allList;

Using ternary operator inside of ternary operator e.g.

Strahinja
  • 440
  • 9
  • 26
1

You can use Optional in this case:

Boolean isMine = null;
String str = "";
Optional<Boolean> optional = Optional.ofNullable(isMine);
str = optional.map(i -> i ? "a" : "b").orElse("c");
System.out.println(str);

So it will be something like:

this.books = optional.map(i -> i ? valForTrue : valForFalse).orElse(valForNull);
Anton Balaniuc
  • 10,889
  • 1
  • 35
  • 53
  • `List books = Optional.ofNullable(isMine).map(v -> v ? dbCallX() : dbCallY()).orElseGet(() -> dbCallZ());` – Rinor Sep 19 '17 at 11:52
0

Do not even think about performance in your case, that's tiny.

Your current way looks good. And you have an answer with flatten way and nested ternary too.

If you still want to learn how to use switch in this case (please do not put this code in production. posting for showing you a way)

Correct way to achieve using String.valueOf(isMine) is

switch ( String.valueOf(isMine) ) {
        case "null":
            //TODO
            break;
        case "true":
            //TODO
            break;
        case "false":
            //TODO
        break;
        }
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

With pattern matching provided by vavr.io it can get very clean:

List<Books> output = Match(input).of(
        Case($(true), dbCallX()),
        Case($(false), dbCallY()),
        Case($(), dbCallZ()));
Rinor
  • 1,790
  • 13
  • 22