code: public static <U extends UserProfile> Optional<U> flatIntoOneProfile(final Collection<U> profiles) {
          final Optional<U> profile = profiles.stream().filter(p -> p != null && !(p instanceof AnonymousProfile)).findFirst();
          if (profile.isPresent()) {
              return profile;
          } else {
              return profiles.stream().filter(p -> p != null).findFirst();
          }
      }
docs: 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