-
Notifications
You must be signed in to change notification settings - Fork 360
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
base: master
Are you sure you want to change the base?
Conversation
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; |
There was a problem hiding this comment.
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>
There was a problem hiding this comment.
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.
@aherbert Just pushed the suggested changes; PTAL. |
In the current implementation, it is possible to set the
nanStrategy
andtiesStrategy
ofNaturalRanking
tonull
, even though they will inevitably throw NPEs upon any calls torank
orresolveTie
. This commit modifies the constructor such that ifnull
is given to either of the strategies, then they are set to the default strategies.