diff --git a/src/java.desktop/share/classes/java/awt/font/NumericShaper.java b/src/java.desktop/share/classes/java/awt/font/NumericShaper.java index ae507036112..99b59cc2e0e 100644 --- a/src/java.desktop/share/classes/java/awt/font/NumericShaper.java +++ b/src/java.desktop/share/classes/java/awt/font/NumericShaper.java @@ -346,6 +346,19 @@ public final class NumericShaper implements java.io.Serializable { return index < NUM_KEYS ? Range.values()[index] : null; } + private static int toRangeHash(Set ranges) { + int m = 0; + for (Range range : ranges) { + int index = range.ordinal(); + if (index < NUM_KEYS) { + m |= 1 << index; + } else { + m |= (1 << NUM_KEYS) + index; + } + } + return m; + } + private static int toRangeMask(Set ranges) { int m = 0; for (Range range : ranges) { @@ -576,7 +589,7 @@ public final class NumericShaper implements java.io.Serializable { // and a linear probe is ok. private static int ctCache = 0; - private static int ctCacheLimit = contexts.length - 2; + private static final int ctCacheLimit = contexts.length - 2; // warning, synchronize access to this as it modifies state private static int getContextKey(char c) { @@ -1510,6 +1523,9 @@ public final class NumericShaper implements java.io.Serializable { private NumericShaper(int key, int mask) { this.key = key; this.mask = mask; + if (((this.mask & ARABIC) != 0) && ((this.mask & EASTERN_ARABIC) != 0)) { + this.mask &= ~ARABIC; + } } private NumericShaper(Range defaultContext, Set ranges) { @@ -1795,15 +1811,7 @@ public final class NumericShaper implements java.io.Serializable { * @see java.lang.Object#hashCode */ public int hashCode() { - int hash = mask; - if (rangeSet != null) { - // Use the CONTEXTUAL_MASK bit only for the enum-based - // NumericShaper. A deserialized NumericShaper might have - // bit masks. - hash &= CONTEXTUAL_MASK; - hash ^= rangeSet.hashCode(); - } - return hash; + return (rangeSet != null) ? Range.toRangeHash(rangeSet) : (mask & ~CONTEXTUAL_MASK); } /** diff --git a/test/jdk/java/awt/font/NumericShaper/NSEqualsTest.java b/test/jdk/java/awt/font/NumericShaper/NSEqualsTest.java new file mode 100644 index 00000000000..6af73b7efa2 --- /dev/null +++ b/test/jdk/java/awt/font/NumericShaper/NSEqualsTest.java @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8365077 + * @summary confirm that an instance which is created with Enum ranges is + * equal to another instance which is created with equivalent traditional + * ranges, and that in such a case the hashCodes are also equal. + */ + +import java.awt.font.NumericShaper; +import java.awt.font.NumericShaper.Range; +import static java.awt.font.NumericShaper.Range.*; +import java.util.EnumSet; + +public class NSEqualsTest { + + public static void main(String[] args) { + + for (Range r1 : Range.values()) { + test(r1); + for (Range r2 : Range.values()) { + test(r1, r2); + } + } + } + + static void test(Range r) { + if (r.ordinal() > MONGOLIAN.ordinal()) { + return; + } + int o = 1 << r.ordinal(); + NumericShaper nsr = NumericShaper.getContextualShaper(EnumSet.of(r)); + NumericShaper nso = NumericShaper.getContextualShaper(o); + printAndCompare(nsr, nso); + } + + static void test(Range r1, Range r2) { + if (r1.ordinal() > MONGOLIAN.ordinal() || r2.ordinal() > MONGOLIAN.ordinal()) { + return; + } + int o1 = 1 << r1.ordinal(); + int o2 = 1 << r2.ordinal(); + + NumericShaper nsr = NumericShaper.getContextualShaper(EnumSet.of(r1, r2)); + NumericShaper nso = NumericShaper.getContextualShaper(o1 | o2); + printAndCompare(nsr, nso); + } + + static void printAndCompare(NumericShaper nsr, NumericShaper nso) { + System.err.println(nsr); + System.err.println(nso); + System.err.println(nsr.hashCode() + " vs " + nso.hashCode() + + " equal: " + nsr.equals(nso)); + if (!nsr.equals(nso)) { + throw new RuntimeException("Expected equal"); + } + if (nsr.hashCode() != nso.hashCode()) { + throw new RuntimeException("Different hash codes:"); + } + } +} +