Skip to content

Commit 72695f8

Browse files
committed
Restore equality for all kinds of Point/Rectangle
The MonitorAwarePoint and MonitorAwareRectangle classes have been introduced as specializations of the existing Point and Rectangle classes solely to support monitor-specific scaling and only used in the context of that feature. They are not supposed to be used outside of SWT but just an internal construct. However, instances of those classes reach consumers of SWT API, e.g., via the Shell class. This broke equality assumptions for consumers, as, e.g., a comparison between a Point and a MonitorAwarePoint (returned by methods of Shell) are never equal, even though the consumers is not supposed to even know that a MonitorAwarePoint can be returned here. To restore equality, this change aligns equality of the monitor-aware implementations of point and rectangle with their original implementations. In consequence, a Point and a MonitorAwarePoint will be considered equal if their x/y coordinates match. The monitor stored in a MonitorAwarePoint will be ignored. This analogously applies to MonitorAwareRectangle. Since those classes are only used internally, no one may rely on any other kind of equality specification for the monitor-aware implementations of point and rectangle, but existing equality for Points and Rectangles in general is restored.
1 parent 42ed1da commit 72695f8

File tree

4 files changed

+6
-26
lines changed

4 files changed

+6
-26
lines changed

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/MonitorAwarePoint.java

+2-11
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
*******************************************************************************/
1414
package org.eclipse.swt.graphics;
1515

16-
import java.util.*;
17-
1816
import org.eclipse.swt.widgets.*;
1917

2018
/**
@@ -53,19 +51,12 @@ public Monitor getMonitor() {
5351

5452
@Override
5553
public boolean equals(Object object) {
56-
if (this == object) {
57-
return true;
58-
}
59-
if (!super.equals(object)) {
60-
return false;
61-
}
62-
MonitorAwarePoint other = (MonitorAwarePoint) object;
63-
return Objects.equals(this.monitor, other.monitor);
54+
return super.equals(object);
6455
}
6556

6657
@Override
6758
public int hashCode() {
68-
return Objects.hash(super.hashCode(), monitor);
59+
return super.hashCode();
6960
}
7061

7162
}

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/MonitorAwareRectangle.java

+2-11
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
*******************************************************************************/
1414
package org.eclipse.swt.graphics;
1515

16-
import java.util.*;
17-
1816
import org.eclipse.swt.widgets.*;
1917

2018
/**
@@ -55,19 +53,12 @@ public Monitor getMonitor() {
5553

5654
@Override
5755
public boolean equals(Object object) {
58-
if (this == object) {
59-
return true;
60-
}
61-
if (!super.equals(object)) {
62-
return false;
63-
}
64-
MonitorAwareRectangle other = (MonitorAwareRectangle) object;
65-
return Objects.equals(this.monitor, other.monitor);
56+
return super.equals(object);
6657
}
6758

6859
@Override
6960
public int hashCode() {
70-
return Objects.hash(super.hashCode(), monitor);
61+
return super.hashCode();
7162
}
7263

7364
}

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Point.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,9 @@ public boolean equals (Object object) {
8484
if (object == this) {
8585
return true;
8686
}
87-
if (object.getClass() != this.getClass()) {
87+
if (!(object instanceof Point other)) {
8888
return false;
8989
}
90-
Point other = (Point) object;
9190
return (other.x == this.x) && (other.y == this.y);
9291
}
9392

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/Rectangle.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,9 @@ public boolean equals(Object object) {
162162
if (object == this) {
163163
return true;
164164
}
165-
if (object.getClass() != this.getClass()) {
165+
if (!(object instanceof Rectangle other)) {
166166
return false;
167167
}
168-
Rectangle other = (Rectangle) object;
169168
return (other.x == this.x) && (other.y == this.y) && (other.width == this.width) && (other.height == this.height);
170169
}
171170

0 commit comments

Comments
 (0)