Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send expression in java

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?

like image 505
William Åkerman Avatar asked Jan 27 '23 01:01

William Åkerman


2 Answers

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));
}
like image 143
shmosel Avatar answered Feb 07 '23 23:02

shmosel


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)); }
like image 21
Ousmane D. Avatar answered Feb 07 '23 22:02

Ousmane D.