Skip to content

Conversation

@pingpingy1
Copy link

In the current implementation, it is possible to set the nanStrategy and tiesStrategy of NaturalRanking to null, even though they will inevitably throw NPEs upon any calls to rank or resolveTie. This commit modifies the constructor such that if null is given to either of the strategies, then they are set to the default strategies.

In the current implementation, it is possible to set the `nanStrategy` and
`tiesStrategy` of `NaturalRanking` to `null`, even though they will
inevitably throw NPEs upon any calls to `rank` or `resolveTie`.
This commit modifies the constructor such that if `null` is given to either
of the strategies, then they are set to the default strategies.
UniformRandomProvider random) {
this.nanStrategy = nanStrategy;
this.tiesStrategy = tiesStrategy;
this.nanStrategy = nanStrategy != null ? nanStrategy : DEFAULT_NAN_STRATEGY;
Copy link
Contributor

Choose a reason for hiding this comment

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

If the user-supplied parameter is null then this is an error. I think it may be preferable to throw a NPE here:

this.nanStrategy = Objects.requireNonNull(nanStrategy, "nanStrategy");
this.tiesStrategy = Objects.requireNonNull(tiesStrategy, "tiesStrategy");

Note: This class has been ported to the Commons Statistics project and will be released in version 1.1.
See: https://github.com/apache/commons-statistics/blob/master/commons-statistics-ranking/src/main/java/org/apache/commons/statistics/ranking/NaturalRanking.java

I updated that implementation to throw NPE for user-supplied arguments. Thanks for the prompting.

You can test the current implementation using the apache snapshots repo using e.g.:

    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-statistics-ranking</artifactId>
      <version>1.1-SNAPSHOT</version>
    </dependency>

Copy link
Author

Choose a reason for hiding this comment

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

Sounds good. Let me modify the test case accordingly.

As suggested by @aherbert, replaces the default strategies with
fail-fast `null` guards.
@pingpingy1
Copy link
Author

@aherbert Just pushed the suggested changes; PTAL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants