mercredi 17 avril 2019

How do i combine condition statements with this particular lambda expression?

I have a java method written using lambda expressions where i need to add more conditions, and i was forbidden from using classical if and elses. This is my current code that checks if getInstrument has the correct ENUM:

public static Predicate<Deal> isDeal() {
  return  i ->
    i.getInstrument() == ENUM1
      || i.getInstrument() == ENUM2
      || i.getInstrument() == ENUM3;
}

To this code, i need to add a condition that checks if i.getGroup() is null and and then keep checking if the enums are correct. I also need to add a condition that, if i.getGroup() != null and i.getGroup() != "NODEAL" and i.getInstrument() is not an ENUM2 or ENUM3, it returns i. This is how i would write it with classical if and elses:

public static Predicate<Deal> is Deal() {

    if ( i.getGroup() == null && i.getInstrument() == ENUM1
                || i.getInstrument() == ENUM2
                || i.getInstrument() == ENUM3) {
      return i;
    } else if ( i.getGroup() != null && i.getGroup() == "DEAL" && 
    i.getInstrument() != ENUM2 || i.getInstrument() != ENUM3) {
      return i;
    }
}

How could i write this using lambda?

Aucun commentaire:

Enregistrer un commentaire