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

Add method in KiwiPreconditions to check collection contains only non-null elements #1151

Closed
sleberknight opened this issue Jun 14, 2024 · 1 comment · Fixed by #1157
Closed
Assignees
Labels
new feature A new feature such as a new class, method, package, group of classes, etc.
Milestone

Comments

@sleberknight
Copy link
Member

sleberknight commented Jun 14, 2024

Add a new validation method in KiwiPreconditions that checks a Collection argument is not empty, and that none of its elements are null.

Some possible names:

  • checkAllElementsNotNull
  • checkArgumentAllElementsNotNull
  • checkArgumentContainsOnlyNotNull
  • checkArgumentContainsOnlyNotNullElements

To be consistent, it should probably start with checkArgument since that is our general convention in KiwiPreconditions.

As I write this, I like checkArgumentAllElementsNotNull and checkArgumentContainsOnlyNotNull best, since they are not overly long and (seem to) convey the intent.

@sleberknight sleberknight added the new feature A new feature such as a new class, method, package, group of classes, etc. label Jun 14, 2024
@sleberknight
Copy link
Member Author

sleberknight commented Jun 14, 2024

I came across this requirement recently in some code that was validating a Set<String> named typesToMatch and did it in two steps:

checkArgumentNotEmpty(collection, "typesToMatch must not be empty");
checkArgument(typesToMatch.stream().noneMatch(StringUtils::isBlank),
        "typesToMatch must not contain blank elements");

This is probably not the most efficient since noneMatch must traverse the entire collection instead of short-circuiting at the first null element. After writing this, I took a few moments and went back to that code and changed it to:

checkArgumentNotEmpty(collection, "typesToMatch must not be empty");
var anyBlank = typesToMatch.stream().anyMatch(StringUtils::isBlank);
checkArgument(!anyBlank, "typesToMatch must not contain blank elements");

This new implementation using anyMatch stops once it finds the first blank String.

Since this was validating a collection of String it uses a check for a blank string as opposed to null. Should there be a dedicated method to validate a collection of String contains all non-blank elements? e.g., checkArgumentContainsOnlyNotBlank which only accepts Collection<String>.

@sleberknight sleberknight changed the title Add method in KiwiPreconditions to check collection argument contains only non-null elements Add method in KiwiPreconditions to check collection contains only non-null elements Jun 14, 2024
@sleberknight sleberknight added this to the 4.1.0 milestone Jul 1, 2024
@sleberknight sleberknight self-assigned this Jul 1, 2024
sleberknight added a commit that referenced this issue Jul 1, 2024
* Add overloaded checkArgumentContainsOnlyNotNull methods in KiwiPreconditions
  to check collection contains only non-null elements
* Add overloaded checkArgumentContainsOnlyNotBlank methods in KiwiPreconditions
  to check that a collection of String contains only non-blank elements

Closes #1151
Closes #1152
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature A new feature such as a new class, method, package, group of classes, etc.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant