While Mark was satisfied with the result, he requested another filter to obtain melons of a certain weight (for example, all the melons that are 1,200 grams). We've just implemented a filter like this for melon types, and so we can come up with a new static method for melons of a certain weight, as follows:
public static List<Melon> filterByWeight(
List<Melon> melons, int weight) {
List<Melon> result = new ArrayList<>();
for (Melon melon: melons) {
if (melon != null && melon.getWeight() == weight) {
result.add(melon);
}
}
return result;
}
This is similar to filterByType(), except it has a different condition/filter. As developers, we are starting to understand that, if we continue like this, then the Filters class will end up with a lot of methods that simply repeat the code and use a different condition. We are very close to a boilerplate code case here.