public static <U extends UserProfile> Optional<U> flatIntoOneProfile(final Collection<U> profiles) {
        val profile = profiles.stream().filter(p -> p != null && !(p instanceof AnonymousProfile)).findFirst();
        if (profile.isPresent()) {
            return profile;
        } else {
            return profiles.stream().filter(Objects::nonNull).findFirst();
        }
    }
Flat the list of profiles into a single optional profile (skip any anonymous profile unless it's the only one).

@param profiles the list of profiles
@param <U> the kind of profile
@return the (optional) profile
pac4j-core/src/main/java/org/pac4j/core/profile/ProfileHelper.java