Set to default strategies if null is given#236
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.
| UniformRandomProvider random) { | ||
| this.nanStrategy = nanStrategy; | ||
| this.tiesStrategy = tiesStrategy; | ||
| this.nanStrategy = nanStrategy != null ? nanStrategy : DEFAULT_NAN_STRATEGY; |
There was a problem hiding this comment.
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.
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
nanStrategyandtiesStrategyofNaturalRankingtonull, even though they will inevitably throw NPEs upon any calls torankorresolveTie. This commit modifies the constructor such that ifnullis given to either of the strategies, then they are set to the default strategies.