EmailDomain extends AbstractStringDomain { public static boolean isEmail( String value) { String[] parts = value.split( "@", -1); String[] localParts = parts.length == 2? parts[0].split( "\\.") : null; String[] domainParts = parts.length == 2? parts[1].split( "\\.") : null; return Optional.ofNullable( localParts) .filter( locals -> parts[0].length() > 0 && parts[0].length() <= MAX_LOCAL) .map( locals -> Arrays.stream( locals).allMatch( part -> isStringOf( part, localPartChars_))) .orElse( false) && Optional.ofNullable( domainParts) .filter( domains -> parts[1].length() > 0 && parts[1].length() <= MAX_DOMAIN) .map( labels -> Arrays.stream( labels).allMatch( part -> part.length() <= MAX_LABEL && isStringOf( part, domainPartChars_))) .orElse( false); } EmailDomain();  EmailDomain( int maxLength);  EmailDomain( Characters chars);  EmailDomain( int maxLength, Characters chars); boolean contains( String value); static boolean isEmail( String value);  }
@Test public void whenLengthInvalidRange() { EmailDomain domain = new EmailDomain(); domain.setLengthRange( 0, 6); DataValue<String> value = domain.select( getResolverContext()); assertThat( "Invalid email", EmailDomain.isEmail( value.getValue()), is( false)); }
@Test public void whenLengthInvalidConstant() { EmailDomain domain = new EmailDomain(); domain.setLengthRange( 6); DataValue<String> value = domain.select( getResolverContext()); assertThat( "Invalid email", EmailDomain.isEmail( value.getValue()), is( false)); domain.setLengthRange( 400); value = domain.select( getResolverContext()); assertThat( "Invalid email", EmailDomain.isEmail( value.getValue()), is( false)); }