code: @SuppressWarnings("unchecked")
      public static <T extends Serializable> Collection<PrincipalProvider<T>> getPrincipalsByType(
              Collection<PrincipalProvider<?>> principalProviders, Class<T> principalClass) {
          Collection<PrincipalProvider<T>> principals = new ArrayList<>();
          for (PrincipalProvider<?> principal : principalProviders) {
              for (Type principalInterface : principal.getClass().getGenericInterfaces()) {
                  if (principalInterface instanceof ParameterizedType) {
                      ParameterizedType currentPrincipalClass = (ParameterizedType) principalInterface;
                      Type currentType = currentPrincipalClass.getActualTypeArguments()[0];
                      if (!principalClass.isArray()) {
                          if (principalClass.equals(currentType)) {
                              principals.add((PrincipalProvider<T>) principal);
                          }
                      } else if (currentType instanceof Class && principalClass.getComponentType().equals(
                              ((Class<?>) currentType).getComponentType())) {
                          principals.add((PrincipalProvider<T>) principal);
                      }
                  }
              }
          }
          return principals;
      }
docs: Gets all the PrincipalProviders corresponding to a type of PrincipalProvider in a collection.<br>
  <br>
  For example, you can use this method to get the LDAPUser by calling :<br>
  <code>getPrincipalsByType(principals, LDAPUserPrincipalProvider.class)</code> .<br>
  <br>
  Then on the first element of the collection : <br>
  <code>LDAPUser user =
  ldapUserPrincipalProvider.getPrincipal()</code>.
  
  @param <T>                type of the PrincipalProvider
  @param principalProviders the principals to find the type.
  @param principalClass     the PrincipalProvider type, not null
  @return A collection of the user's PrincipalProviders of type principalProviderClass. Not null.