All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.phasetwo.service.util.Domains Maven / Gradle / Ivy

There is a newer version: 0.79
Show newest version
package io.phasetwo.service.util;

import java.util.Optional;
import java.util.Set;

public final class Domains {

  public static Optional extract(String usernameOrEmail) {
    if (usernameOrEmail != null) {
      int atIndex = usernameOrEmail.trim().lastIndexOf("@");
      if (atIndex >= 0) {
        String strDomain = usernameOrEmail.trim().substring(atIndex + 1);
        if (!strDomain.isEmpty()) {
          return Optional.of(strDomain);
        }
      }
    }
    return Optional.empty();
  }

  public static boolean supportsDomain(Set orgDomains, String userEmailDomain) {
    return orgDomains.stream()
        .anyMatch(it -> it.equals(userEmailDomain) || isSubDomainOf(userEmailDomain, it));
  }

  private static boolean isSubDomainOf(String subdomain, String orgDomain) {
    return subdomain.endsWith("." + orgDomain);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy