Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HV-1971 fix CNPJ validation when all digits are the same #1339

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

edurbs
Copy link

@edurbs edurbs commented Jan 14, 2024

@hibernate-github-bot
Copy link

hibernate-github-bot bot commented Jan 14, 2024

Thanks for your pull request!

This pull request appears to follow the contribution rules.

› This message was automatically generated.

Copy link
Member

@marko-bekhta marko-bekhta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, thanks for reporting the problem and submitting a PR. Please see an inline comment.

@@ -55,6 +56,13 @@ public boolean isValid(CharSequence value, ConstraintValidatorContext context) {
return true;
}

// Check for repeated digits
if ( value.toString().chars()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe let's replace this with a simple for loop? Something along the lines:

// the number is too short, hence it's invalid anyway:
if ( value.length() < 2 ) {
	return false;
}

char firstDigit = value.charAt( 0 );
char otherDigit = value.charAt( 1 );
for ( int i = 2; i < value.length(); i++ ) {
	char c = value.charAt( i );
	if ( Character.isDigit( c ) && firstDigit != c ) {
		otherDigit = c;
	}
}
if ( firstDigit == otherDigit ) {
	return false;
}

From what I see distinct may not be optimized:

public final IntStream distinct() {
	// While functional and quick to implement, this approach is not very efficient.
	// An efficient version requires an int-specific map/set implementation.
	return boxed().distinct().mapToInt(i -> i);
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perfect, I'll change it. Thanks!

Copy link
Member

@marko-bekhta marko-bekhta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants