This is an automated email from the ASF dual-hosted git repository.

pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git


The following commit(s) were added to refs/heads/trunk by this push:
     new bd97feb849 Fix equals() and hashCode() contract violation in XSSFColor 
(#1149)
bd97feb849 is described below

commit bd97feb849ab86670fe5b5513d4066c46b599797
Author: KALI 834X <[email protected]>
AuthorDate: Mon Jun 29 17:45:26 2026 +0530

    Fix equals() and hashCode() contract violation in XSSFColor (#1149)
    
    * Fix equals and hashCode contract violation in XSSFColor
    
    * Use Objects.hash in XSSFColor.hashCode
---
 .../org/apache/poi/xssf/usermodel/XSSFColor.java   |  8 +++++++-
 .../apache/poi/xssf/usermodel/TestXSSFColor.java   | 22 ++++++++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletion(-)

diff --git 
a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFColor.java 
b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFColor.java
index b03ab9a660..b92fcd19c0 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFColor.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFColor.java
@@ -17,6 +17,7 @@
 package org.apache.poi.xssf.usermodel;
 
 import java.util.Arrays;
+import java.util.Objects;
 
 import org.apache.poi.ss.usermodel.Color;
 import org.apache.poi.ss.usermodel.ExtendedColor;
@@ -382,7 +383,12 @@ public class XSSFColor extends ExtendedColor {
 
     @Override
     public int hashCode() {
-        return ctColor.toString().hashCode();
+        return Objects.hash(
+                isAuto(),
+                isIndexed() ? getIndex() : null,
+                isRGB() ? Arrays.hashCode(getARGB()) : null,
+                isThemed() ? getTheme() : null,
+                hasTint() ? getTint() : null);
     }
 
     // Helper methods for {@link #equals(Object)}
diff --git 
a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFColor.java 
b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFColor.java
index 88c54d7386..5fb37e2c4c 100644
--- a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFColor.java
+++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFColor.java
@@ -198,4 +198,26 @@ public final class TestXSSFColor {
           assertEquals(hexRgb, color.getARGBHex());
        }
    }
+
+    @Test
+    void testEqualsAndHashCodeContract() throws Exception {
+        // Create programmatic CTColor
+        org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor ct1 = 
+                
org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor.Factory.newInstance();
+        ct1.setRgb(new byte[]{1, 2, 3});
+
+        // Parse equivalent CTColor from string with custom namespace 
attribute to simulate different document contexts
+        org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor ct2 = 
+                
org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor.Factory.parse(
+                        "<xml-fragment rgb=\"010203\" 
xmlns:custom=\"http://custom-namespace\"/>");
+
+        XSSFColor color1 = XSSFColor.from(ct1);
+        XSSFColor color2 = XSSFColor.from(ct2);
+
+        // They must be logically equal
+        assertEquals(color1, color2, "Colors must be logically equal");
+
+        // The hashCodes must be identical under the Java equals-hashCode 
contract
+        assertEquals(color1.hashCode(), color2.hashCode(), "Equal colors must 
have the same hashCode");
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to