my problem is duplicit code. I have 4 methods like below but one is for Name, other for Login, etc. So the only thing that changes are getters (p.getName(), p.getLogin()...).
private Set<Person> filterByName(Set<String> values, Set<Person> peeps){
Set<Person> filtered = new HashSet<>();
for (Person p : peeps){
if(isIn(p.getName(), values)){
filtered.add(p);
}
}
return filtered;
}
I call these methods in switch:
switch(var){
case NAME:
filtered = filterByName(arg1, arg2);
case ...
How can I do this without implementing a method for each case
block?
My idea is to somehow tell the method to use the right getter in case
blocks and therefore have only one method for all filtering, e.g.
private Set<Person> universalFilter(Set<String> values, Set<Person> peeps) {
Set<Person> filtered = new HashSet<>();
for (Person p : peeps){
if(isIn(***SOMETHING CLEVER***, values)){
filtered.add(p);
}
}
return filtered;
}