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-math.git
commit 148f3d43fda95123b7a2a3f80cce64dd7bbdd174 Author: Alex Herbert <[email protected]> AuthorDate: Thu May 21 13:36:38 2026 +0100 MATH-1689: Document non-use of Comparator for equality --- .../apache/commons/math4/legacy/stat/Frequency.java | 9 +++++++++ .../commons/math4/legacy/stat/FrequencyTest.java | 21 +++++++++++++++++++++ src/changes/changes.xml | 5 +++++ 3 files changed, 35 insertions(+) diff --git a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/stat/Frequency.java b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/stat/Frequency.java index 038f1287c..3bc942e15 100644 --- a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/stat/Frequency.java +++ b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/stat/Frequency.java @@ -36,6 +36,15 @@ import org.apache.commons.math4.legacy.exception.util.LocalizedFormats; * <p>The values are ordered using the default (natural order), unless a * <code>Comparator</code> is supplied in the constructor.</p> * + * <p><strong>Implementation note</strong> + * + * <p>This class does not use the {@link Comparator} for the {@link #equals(Object)} or + * {@link #hashCode()} implementation. Equality uses the content of the underlying + * frequency table mapping the values to frequency counts. The result is two + * {@link Frequency} instances with the same observations of values are considered equal + * even if the cumulative frequency of a specified value is different between the two + * instances. + * * @param <T> a comparable type used in the frequency distribution */ public class Frequency<T extends Comparable<T>> { diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/FrequencyTest.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/FrequencyTest.java index 30f7f329e..b8fe6bfca 100644 --- a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/FrequencyTest.java +++ b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/FrequencyTest.java @@ -19,6 +19,7 @@ package org.apache.commons.math4.legacy.stat; import java.io.BufferedReader; import java.io.StringReader; import java.util.ArrayList; +import java.util.Comparator; import java.util.Iterator; import java.util.List; @@ -342,4 +343,24 @@ public final class FrequencyTest { Assert.assertEquals(Float.valueOf(Float.POSITIVE_INFINITY), mode.get(1)); Assert.assertEquals(Float.valueOf(Float.NaN), mode.get(2)); } + + /** + * The Frequency class ignores the comparator for equality. + * See MATH-1689. + */ + @Test + public void testEqualsIgnoresComparator() { + Frequency<Integer> ascending = new Frequency<>(); + Frequency<Integer> descending = new Frequency<Integer>(Comparator.reverseOrder()); + ascending.addValue(1); + ascending.addValue(2); + descending.addValue(1); + descending.addValue(2); + + Assert.assertEquals(1, ascending.getCumFreq(1)); + Assert.assertEquals(2, descending.getCumFreq(1)); + Assert.assertEquals("[1, 2]", ascending.getMode().toString()); + Assert.assertEquals("[2, 1]", descending.getMode().toString()); + Assert.assertEquals(ascending, descending); + } } diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 067fb33a6..7e3c89def 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -96,6 +96,11 @@ Caveat: to support the whole codebase (it was one of the main reasons for creating more focused components). "> + <action dev="aherbert" type="update" issue="MATH-1689" due-to="Ruiqi Dong"> + "Frequency": Document behaviour of equals and hashCode that ignores + the value Comparator. The comparator only affects the cumulative + frequency of values, not the underlying observed frequency table. + </action> <action dev="aherbert" type="update"> Removed o.a.c.m.legacy.stat.interval package. Functionality has been transferred to Commons Statistics interval.
