I have two methods that both get a user object from an Arraylist.
public User getUser(int userID) {
ListIterator<User> listIterator = listOfLoggedInUsers.listIterator();
User user;
while(listIterator.hasNext()) {
user = listIterator.next();
if (user.getID() == userID) {
return user;
}
}
return null;
}
public User getUser(String username) {
ListIterator<User> listIterator = listOfLoggedInUsers.listIterator();
User user;
while(listIterator.hasNext()) {
user = listIterator.next();
if (user.getUsername().equals(username)) {
return user;
}
}
return null;
}
Since the methods only differ in the if statment how could code duplication be reduced? Is it possible to send a expression/dynamic boolean in the arguments for example?
You can create a common method that accepts a predicate and return the first matching element:
private User getUser(Predicate<User> predicate) {
return listOfLoggedInUsers.stream()
.filter(predicate)
.findFirst()
.orElse(null);
}
public User getUser(int userID) {
return getUser(user -> user.getID() == userID);
}
public User getUser(String username) {
return getUser(user -> user.getUsername().equals(username));
}
You could combine them both as follows with the use of a Predicate<User>
public User getUser(Predicate<User> predicate){
for (User u : listOfLoggedInUsers)
if (predicate.test(u)) return u;
return null;
}
then call it as:
getUser(u -> u.getUserId() == userID)
getUser(u -> u.getUsername().equals(username))
...
...
To make this better and more readable you may consider making getUser
private User getUser(Predicate<User> predicate){
for (User u : listOfLoggedInUsers)
if (predicate.test(u)) return u;
return null;
}
and then expose two public methods
public User getUserById(int userID) { return getUser(u -> u.getUserId() == userID); }
public User getUserByName(String name) { return getUser(u -> u.getUsername().equals(name)); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With