This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit 3ef698bff4ce5d1e90ee3912c2773fa809eca5c8 Author: Gary Gregory <[email protected]> AuthorDate: Thu Mar 5 22:50:49 2026 -0500 CPUTF8 is missing optional hashCode() and equals() to match its Comparable.compareTo(). --- src/changes/changes.xml | 1 + .../commons/compress/harmony/pack200/CPUTF8.java | 17 ++++++ .../compress/harmony/pack200/CPUTF8Test.java | 70 ++++++++++++++++++++++ 3 files changed, 88 insertions(+) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 342a10d64..8ba4aee65 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -134,6 +134,7 @@ The <action> type attribute can be add,update,fix,remove. <action type="fix" dev="ggregory" due-to="Gary Gregory">Deprecate org.apache.commons.compress.harmony.unpack200.SegmentUtils.SegmentUtils().</action> <action type="fix" dev="ggregory" due-to="Gary Gregory">Fix all Javadoc warnings.</action> <action type="fix" dev="ggregory" due-to="Gary Gregory, Shan Jiang" issue="COMPRESS-719">CPConstant subclasses are missing optional hashCode() and equals().</action> + <action type="fix" dev="ggregory" due-to="Gary Gregory">CPUTF8 is missing optional hashCode() and equals() to match its Comparable.compareTo().</action> <!-- ADD --> <action type="add" dev="ggregory" due-to="Gary Gregory">Add MemoryLimitException.MemoryLimitException(long, long).</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add CompressException.CompressException(String, Object...).</action> diff --git a/src/main/java/org/apache/commons/compress/harmony/pack200/CPUTF8.java b/src/main/java/org/apache/commons/compress/harmony/pack200/CPUTF8.java index 98cdc2f13..ae9ead082 100644 --- a/src/main/java/org/apache/commons/compress/harmony/pack200/CPUTF8.java +++ b/src/main/java/org/apache/commons/compress/harmony/pack200/CPUTF8.java @@ -41,6 +41,17 @@ public int compareTo(final Object arg0) { return string.compareTo(((CPUTF8) arg0).string); } + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (!(obj instanceof CPUTF8)) { + return false; + } + return compareTo(obj) == 0; + } + /** * Gets the underlying string. * @@ -50,6 +61,12 @@ public String getUnderlyingString() { return string; } + @Override + public int hashCode() { + return string.hashCode(); + } + + @Override public String toString() { return string; diff --git a/src/test/java/org/apache/commons/compress/harmony/pack200/CPUTF8Test.java b/src/test/java/org/apache/commons/compress/harmony/pack200/CPUTF8Test.java new file mode 100644 index 000000000..31b0cac33 --- /dev/null +++ b/src/test/java/org/apache/commons/compress/harmony/pack200/CPUTF8Test.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.commons.compress.harmony.pack200; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +import org.junit.jupiter.api.Test; + +class CPUTF8Test { + + @Test + void testEquals() { + final CPUTF8 a = new CPUTF8("42"); + final CPUTF8 b = new CPUTF8("42"); + final CPUTF8 c = new CPUTF8("98"); + // Reflexivity + assertEquals(a, a); + // Symmetry + assertEquals(a, b); + assertEquals(b, a); + // Inequality + assertNotEquals(a, c); + assertNotEquals(c, a); + // Null and different type + assertNotEquals(null, a); + assertNotEquals(a, "42"); + } + + @Test + void testEqualsEdgeCases() { + final CPUTF8 zero = new CPUTF8("0"); + final CPUTF8 minValue = new CPUTF8(""); + final CPUTF8 maxValue = new CPUTF8("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"); + assertEquals(zero, new CPUTF8("0")); + assertEquals(minValue, new CPUTF8("")); + assertEquals(maxValue, new CPUTF8("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); + assertNotEquals(zero, minValue); + assertNotEquals(zero, maxValue); + assertNotEquals(minValue, maxValue); + } + + @Test + void testHashCode() { + final CPUTF8 a = new CPUTF8("42"); + final CPUTF8 b = new CPUTF8("42"); + final CPUTF8 c = new CPUTF8("99"); + // Equal objects must have equal hash codes + assertEquals(a.hashCode(), b.hashCode()); + // Unequal objects should (typically) have different hash codes + assertNotEquals(a.hashCode(), c.hashCode()); + } +}
