4497648: TextLayout equals method is not implemented
Reviewed-by: serb, psadhukhan
--- a/jdk/src/java.desktop/share/classes/java/awt/font/TextLayout.java Thu Jun 23 16:46:13 2016 +0530
+++ b/jdk/src/java.desktop/share/classes/java/awt/font/TextLayout.java Thu Jun 23 16:54:19 2016 -0700
@@ -248,7 +248,6 @@
// all are recomputed from scratch in buildCache()
private TextLine.TextLineMetrics lineMetrics = null;
private float visibleAdvance;
- private int hashCodeCache;
/*
* TextLayouts are supposedly immutable. If you mutate a TextLayout under
@@ -722,9 +721,6 @@
naturalBounds = null;
boundsRect = null;
- // hashCode will be regenerated on demand
- hashCodeCache = 0;
-
cacheIsValid = true;
}
@@ -2569,33 +2565,8 @@
}
/**
- * Returns the hash code of this {@code TextLayout}.
- * @return the hash code of this {@code TextLayout}.
- */
- public int hashCode() {
- if (hashCodeCache == 0) {
- ensureCache();
- hashCodeCache = textLine.hashCode();
- }
- return hashCodeCache;
- }
-
- /**
- * Returns {@code true} if the specified {@code Object} is a
- * {@code TextLayout} object and if the specified {@code Object}
- * equals this {@code TextLayout}.
- * @param obj an {@code Object} to test for equality
- * @return {@code true} if the specified {@code Object}
- * equals this {@code TextLayout}; {@code false}
- * otherwise.
- */
- public boolean equals(Object obj) {
- return (obj instanceof TextLayout) && equals((TextLayout)obj);
- }
-
- /**
* Returns {@code true} if the two layouts are equal.
- * Two layouts are equal if they contain equal glyphvectors in the same order.
+ * Obeys the general contract of {@link java.lang.Object equals(Object)}.
* @param rhs the {@code TextLayout} to compare to this
* {@code TextLayout}
* @return {@code true} if the specified {@code TextLayout}
@@ -2603,16 +2574,7 @@
*
*/
public boolean equals(TextLayout rhs) {
-
- if (rhs == null) {
- return false;
- }
- if (rhs == this) {
- return true;
- }
-
- ensureCache();
- return textLine.equals(rhs.textLine);
+ return equals((Object)rhs);
}
/**
--- a/jdk/src/java.desktop/share/classes/java/awt/font/TextLine.java Thu Jun 23 16:46:13 2016 +0530
+++ b/jdk/src/java.desktop/share/classes/java/awt/font/TextLine.java Thu Jun 23 16:54:19 2016 -0700
@@ -880,11 +880,6 @@
return dstShape;
}
- public int hashCode() {
- return (fComponents.length << 16) ^
- (fComponents[0].hashCode() << 3) ^ (fCharsLimit-fCharsStart);
- }
-
public String toString() {
StringBuilder buf = new StringBuilder();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/awt/font/TextLayout/TextLayoutEqualsTest.java Thu Jun 23 16:54:19 2016 -0700
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2016, 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 4497648
+ * @summary Test equals methods on TextLayout
+ */
+import java.awt.Font;
+import java.awt.font.FontRenderContext;
+import java.awt.font.TextLayout;
+
+public class TextLayoutEqualsTest {
+
+ public static void main(String args[]) {
+
+ Font font = new Font(Font.DIALOG, Font.PLAIN, 12);
+ String text = "hello world";
+ FontRenderContext frc = new FontRenderContext(null, false, false);
+ TextLayout tl1 = new TextLayout(text, font, frc);
+ TextLayout tl2 = new TextLayout(text, font, frc);
+ if (tl1.equals(tl2) ||
+ tl2.equals(tl1) ||
+ tl1.equals((Object)tl2) ||
+ tl2.equals((Object)tl1))
+ {
+ throw new RuntimeException("Equal TextLayouts");
+ }
+ if (!tl1.equals(tl1) ||
+ !tl1.equals((Object)tl1))
+ {
+ throw new RuntimeException("Non-Equal TextLayouts");
+ }
+ }
+}