package com.atlassian.stash.internal.user;

import com.atlassian.bitbucket.user.ApplicationUser;
import com.atlassian.bitbucket.util.UserUtils;

import java.util.function.Predicate;
import java.util.regex.Pattern;

public class UserPageFilter implements Predicate<ApplicationUser> {

    private final Pattern pattern;

    public UserPageFilter(String usernamePattern) {
        if (usernamePattern == null) {
            pattern = null;
        } else {
            pattern = UserUtils.createNameMatchingPattern(usernamePattern);
        }
    }

    @Override
    public boolean test(ApplicationUser user) {
        return pattern == null || pattern.matcher(user.getName()).find() || pattern.matcher(user.getDisplayName()).find();
    }

}
