How can i solve duplication of code in this?

RafaelKoll :

I want to check if all the ingredients(toppings and fillings) inside a wrap are both vegan and nut free. This is the solution that I came up with, howver I think its a bit inefficient as there is duplication of code. Is there a more efficient way to do it?

(I have a map for all the toppings and fillings which every one contains boolean to know if the topping/filling is vegan and if it is nut free.

public boolean isVegan() {
    for (Topping t : toppings) {
        if (!t.isVegan()) {
            return false;
        }
    }
    for (Filling f : fillings) {
        if (!f.isVegan()) {
            return false;
        }
    }
    return bread.isVegan();
}

public boolean isNutFree() {
    for (Topping t : toppings) {
        if (!t.isNutFree()) {
            return false;
        }
    }
    for (Filling f : fillings) {
        if (!f.isNutFree()) {
            return false;
        }
    }
    return bread.isNutFree();
}
davidxxx :

Supposing that Ingredient is the base class of these different classes and that this class defines the isVegan() method, you could create a Stream from all these objects and computing whether all are vegan :

public boolean isVegan() {
    return 
    Stream.concat(toppings.stream(), fillings.stream(), Stream.of(bread))
          .allMatch(Ingredient::isVegan);

}

For isNutFree() the idea is the same :

public boolean isNutFree() {
    return 
    Stream.concat(toppings.stream(), fillings.stream(), Stream.of(bread))
          .allMatch(Ingredient::isNutFree);

} 

Note that you could also generalize a matching method to reduce further the duplication :

public boolean allMatch(Predicate<Ingredient> predicate) {
    return 
    Stream.concat(toppings.stream(), fillings.stream(), Stream.of(bread))
          .allMatch( i -> predicate.test(i));

} 

And use it such as :

boolean isNutFree = allMatch(Ingredient::isNutFree);
boolean isVegan =  allMatch(Ingredient::isVegan);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=111365&siteId=1