This is an automated email from the ASF dual-hosted git repository. aherbert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-statistics.git
commit 07547398efbff1e966cccf330c2f2152e9235b77 Author: Alex Herbert <aherb...@apache.org> AuthorDate: Fri Jun 6 14:50:19 2025 +0100 Switch statements checked using PMD ExhaustiveSwitchHasDefault --- .../commons/statistics/descriptive/DoubleStatistics.java | 11 +++++------ .../apache/commons/statistics/descriptive/IntStatistics.java | 11 +++++------ .../apache/commons/statistics/descriptive/LongStatistics.java | 11 +++++------ .../commons/statistics/inference/UnconditionedExactTest.java | 8 ++++---- .../org/apache/commons/statistics/ranking/NaturalRanking.java | 9 ++++----- src/conf/checkstyle/checkstyle.xml | 3 ++- 6 files changed, 25 insertions(+), 28 deletions(-) diff --git a/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/DoubleStatistics.java b/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/DoubleStatistics.java index d6f790d..4f9db22 100644 --- a/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/DoubleStatistics.java +++ b/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/DoubleStatistics.java @@ -101,6 +101,7 @@ public final class DoubleStatistics implements DoubleConsumer { * @return {@code this} instance */ Builder add(Statistic statistic) { + // Exhaustive switch statement switch (statistic) { case GEOMETRIC_MEAN: case SUM_OF_LOGS: @@ -134,8 +135,6 @@ public final class DoubleStatistics implements DoubleConsumer { case SUM_OF_SQUARES: sumOfSquares = SumOfSquares::createFromRange; break; - default: - throw new IllegalArgumentException(UNSUPPORTED_STATISTIC + statistic); } return this; } @@ -501,6 +500,7 @@ public final class DoubleStatistics implements DoubleConsumer { */ public boolean isSupported(Statistic statistic) { // Check for the appropriate underlying implementation + // Exhaustive switch statement switch (statistic) { case GEOMETRIC_MEAN: case SUM_OF_LOGS: @@ -524,9 +524,9 @@ public final class DoubleStatistics implements DoubleConsumer { return sum != null; case SUM_OF_SQUARES: return sumOfSquares != null; - default: - return false; } + // Unreachable code + throw new IllegalArgumentException(UNSUPPORTED_STATISTIC + statistic); } /** @@ -566,6 +566,7 @@ public final class DoubleStatistics implements DoubleConsumer { // of DoubleStatistic. This ensures the statistic implementation cannot // be updated with new values by casting the result and calling accept(double). StatisticResult stat = null; + // Exhaustive switch statement switch (statistic) { case GEOMETRIC_MEAN: stat = getGeometricMean(); @@ -603,8 +604,6 @@ public final class DoubleStatistics implements DoubleConsumer { case VARIANCE: stat = getVariance(); break; - default: - break; } if (stat != null) { return stat instanceof DoubleStatistic ? diff --git a/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/IntStatistics.java b/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/IntStatistics.java index dd9f709..8ce05ab 100644 --- a/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/IntStatistics.java +++ b/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/IntStatistics.java @@ -103,6 +103,7 @@ public final class IntStatistics implements IntConsumer { * @return {@code this} instance */ Builder add(Statistic statistic) { + // Exhaustive switch statement switch (statistic) { case GEOMETRIC_MEAN: case SUM_OF_LOGS: @@ -135,8 +136,6 @@ public final class IntStatistics implements IntConsumer { case SUM_OF_SQUARES: sumOfSquares = IntSumOfSquares::createFromRange; break; - default: - throw new IllegalArgumentException(UNSUPPORTED_STATISTIC + statistic); } return this; } @@ -471,6 +470,7 @@ public final class IntStatistics implements IntConsumer { */ public boolean isSupported(Statistic statistic) { // Check for the appropriate underlying implementation + // Exhaustive switch statement switch (statistic) { case GEOMETRIC_MEAN: case SUM_OF_LOGS: @@ -493,9 +493,9 @@ public final class IntStatistics implements IntConsumer { return sum != null; case SUM_OF_SQUARES: return sumOfSquares != null; - default: - return false; } + // Unreachable code + throw new IllegalArgumentException(UNSUPPORTED_STATISTIC + statistic); } /** @@ -598,6 +598,7 @@ public final class IntStatistics implements IntConsumer { // of IntStatistic. This ensures the statistic implementation cannot // be updated with new values by casting the result and calling accept(int). StatisticResult stat = null; + // Exhaustive switch statement switch (statistic) { case GEOMETRIC_MEAN: stat = getGeometricMean(); @@ -635,8 +636,6 @@ public final class IntStatistics implements IntConsumer { case VARIANCE: stat = getVariance(); break; - default: - break; } if (stat != null) { return stat; diff --git a/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/LongStatistics.java b/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/LongStatistics.java index f4fb8e2..2a35994 100644 --- a/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/LongStatistics.java +++ b/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/LongStatistics.java @@ -103,6 +103,7 @@ public final class LongStatistics implements LongConsumer { * @return {@code this} instance */ Builder add(Statistic statistic) { + // Exhaustive switch statement switch (statistic) { case GEOMETRIC_MEAN: case SUM_OF_LOGS: @@ -135,8 +136,6 @@ public final class LongStatistics implements LongConsumer { case SUM_OF_SQUARES: sumOfSquares = LongSumOfSquares::createFromRange; break; - default: - throw new IllegalArgumentException(UNSUPPORTED_STATISTIC + statistic); } return this; } @@ -471,6 +470,7 @@ public final class LongStatistics implements LongConsumer { */ public boolean isSupported(Statistic statistic) { // Check for the appropriate underlying implementation + // Exhaustive switch statement switch (statistic) { case GEOMETRIC_MEAN: case SUM_OF_LOGS: @@ -493,9 +493,9 @@ public final class LongStatistics implements LongConsumer { return sum != null; case SUM_OF_SQUARES: return sumOfSquares != null; - default: - return false; } + // Unreachable code + throw new IllegalArgumentException(UNSUPPORTED_STATISTIC + statistic); } /** @@ -576,6 +576,7 @@ public final class LongStatistics implements LongConsumer { // of LongStatistic. This ensures the statistic implementation cannot // be updated with new values by casting the result and calling accept(long). StatisticResult stat = null; + // Exhaustive switch statement switch (statistic) { case GEOMETRIC_MEAN: stat = getGeometricMean(); @@ -613,8 +614,6 @@ public final class LongStatistics implements LongConsumer { case VARIANCE: stat = getVariance(); break; - default: - break; } if (stat != null) { return stat; diff --git a/commons-statistics-inference/src/main/java/org/apache/commons/statistics/inference/UnconditionedExactTest.java b/commons-statistics-inference/src/main/java/org/apache/commons/statistics/inference/UnconditionedExactTest.java index 06c703b..f9740a0 100644 --- a/commons-statistics-inference/src/main/java/org/apache/commons/statistics/inference/UnconditionedExactTest.java +++ b/commons-statistics-inference/src/main/java/org/apache/commons/statistics/inference/UnconditionedExactTest.java @@ -642,6 +642,7 @@ public final class UnconditionedExactTest { final int d = table[1][1]; final int m = a + c; final int n = b + d; + // Exhaustive switch statement switch (method) { case Z_POOLED: return statisticZ(a, b, m, n, true); @@ -649,9 +650,8 @@ public final class UnconditionedExactTest { return statisticZ(a, b, m, n, false); case BOSCHLOO: return statisticBoschloo(a, b, m, n); - default: - throw new IllegalStateException(String.valueOf(method)); } + throw new IllegalStateException(String.valueOf(method)); } /** @@ -710,6 +710,7 @@ public final class UnconditionedExactTest { private double findExtremeTables(int a, int b, XYList tableList) { final int m = tableList.getMaxX(); final int n = tableList.getMaxY(); + // Exhaustive switch statement switch (method) { case Z_POOLED: return findExtremeTablesZ(a, b, m, n, true, tableList); @@ -717,9 +718,8 @@ public final class UnconditionedExactTest { return findExtremeTablesZ(a, b, m, n, false, tableList); case BOSCHLOO: return findExtremeTablesBoschloo(a, b, m, n, tableList); - default: - throw new IllegalStateException(String.valueOf(method)); } + throw new IllegalStateException(String.valueOf(method)); } /** diff --git a/commons-statistics-ranking/src/main/java/org/apache/commons/statistics/ranking/NaturalRanking.java b/commons-statistics-ranking/src/main/java/org/apache/commons/statistics/ranking/NaturalRanking.java index 0e479e5..5074e74 100644 --- a/commons-statistics-ranking/src/main/java/org/apache/commons/statistics/ranking/NaturalRanking.java +++ b/commons-statistics-ranking/src/main/java/org/apache/commons/statistics/ranking/NaturalRanking.java @@ -315,6 +315,7 @@ public class NaturalRanking implements RankingAlgorithm { * @return the operator applied to NaN values */ private DoubleUnaryOperator createNaNAction(int[] nanCount) { + // Exhaustive switch statement switch (nanStrategy) { case MAXIMAL: // Replace NaNs with +INFs return ACTION_POS_INF; @@ -329,10 +330,9 @@ public class NaturalRanking implements RankingAlgorithm { }; case FAILED: return ACTION_ERROR; - default: - // this should not happen unless NaNStrategy enum is changed - throw new IllegalStateException(); } + // Unreachable code + throw new IllegalStateException(String.valueOf(nanStrategy)); } /** @@ -399,6 +399,7 @@ public class NaturalRanking implements RankingAlgorithm { // length of sequence of tied ranks final int length = tiesTrace.size(); + // Exhaustive switch switch (tiesStrategy) { case AVERAGE: // Replace ranks with average: (lower + upper) / 2 fill(ranks, tiesTrace, (2 * c + length - 1) * 0.5); @@ -422,8 +423,6 @@ public class NaturalRanking implements RankingAlgorithm { ranks[tiesTrace.get(i)] = r++; } break; - default: // this should not happen unless TiesStrategy enum is changed - throw new IllegalStateException(); } tiesTrace.clear(); diff --git a/src/conf/checkstyle/checkstyle.xml b/src/conf/checkstyle/checkstyle.xml index 8328195..867df01 100644 --- a/src/conf/checkstyle/checkstyle.xml +++ b/src/conf/checkstyle/checkstyle.xml @@ -185,7 +185,8 @@ <!-- Allowed for algorithm implementations. --> <!-- <module name="InnerAssignment" /> --> <!-- <module name="MagicNumber" /> --> - <module name="MissingSwitchDefault" /> + <!-- Switch statements checked using PMD ExhaustiveSwitchHasDefault. --> + <!-- <module name="MissingSwitchDefault" /> --> <module name="MultipleVariableDeclarations" /> <module name="SimplifyBooleanExpression" /> <module name="SimplifyBooleanReturn" />