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

Set to default strategies if null is given #236

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

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.
@@ -158,8 +158,8 @@ public NaturalRanking(NaNStrategy nanStrategy,
private NaturalRanking(NaNStrategy nanStrategy,
TiesStrategy tiesStrategy,
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