Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.

this.tiesStrategy = tiesStrategy != null ? tiesStrategy : DEFAULT_TIES_STRATEGY;
this.random = random;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,15 @@ public void testNoNaNsFailed() {
double[] ranks = ranking.rank(data);
TestUtils.assertEquals(data, ranks, 0d);
}

/**
* Tests NaturalRanking constructor with null strategies as inputs.
* These should be switched to the default strategies.
*/
@Test
public void testNullStrategies() {
NaturalRanking ranking = new NaturalRanking((NaNStrategy) null, (TiesStrategy) null);
Assert.assertEquals(ranking.getNanStrategy(), NaturalRanking.DEFAULT_NAN_STRATEGY);
Assert.assertEquals(ranking.getTiesStrategy(), NaturalRanking.DEFAULT_TIES_STRATEGY);
}
}