Skip to content

Commit a878942

Browse files
committed
Don't override getSize() and getLocation() in PrecisionRectangle
The PreciseDimension/PrecisePoint might have a different rounding behavior. Therefore a call to e.g. r.getSize().width might be different from r.width. Instead, two new preciseSize() and preciseLocation() methods have been added. See #845 (comment)
1 parent ee1514c commit a878942

File tree

3 files changed

+43
-20
lines changed

3 files changed

+43
-20
lines changed

org.eclipse.draw2d.tests/src/org/eclipse/draw2d/test/PrecisionRectangleTest.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
package org.eclipse.draw2d.test;
1515

16+
import org.eclipse.draw2d.geometry.Dimension;
1617
import org.eclipse.draw2d.geometry.Insets;
18+
import org.eclipse.draw2d.geometry.Point;
1719
import org.eclipse.draw2d.geometry.PrecisionRectangle;
1820
import org.eclipse.draw2d.geometry.Rectangle;
1921

@@ -125,14 +127,32 @@ public void testScale() {
125127
public void testScaleLocation() {
126128
// check work scale(double) with rounding errors
127129
PrecisionRectangle r = new PrecisionRectangle(-9.47, -34.43, 41.95, 25.92);
128-
assertEquals(r.getPreciseCopy().getLocation().scale(1.153), r.getPreciseCopy().scale(1.153).getLocation());
130+
assertEquals(r.getCopy().preciseLocation().scale(1.153), r.getCopy().scale(1.153).preciseLocation());
129131
}
130132

131133
@SuppressWarnings("static-method")
132134
@Test
133135
public void testScaleDimension() {
134136
// check work scale(double) with rounding errors
135137
PrecisionRectangle r = new PrecisionRectangle(-9.47, -34.43, 41.95, 25.92);
136-
assertEquals(r.getPreciseCopy().getSize().scale(1.153), r.getPreciseCopy().scale(1.153).getSize());
138+
assertEquals(r.getCopy().preciseSize().scale(1.153), r.getCopy().scale(1.153).preciseSize());
139+
}
140+
141+
@SuppressWarnings("static-method")
142+
@Test
143+
public void testGetSize() {
144+
PrecisionRectangle r = new PrecisionRectangle(0, 0, 41.95, 25.92);
145+
assertEquals(r.getSize().getClass(), Dimension.class);
146+
assertEquals(r.width, r.getSize().width);
147+
assertEquals(r.height, r.getSize().height);
148+
}
149+
150+
@SuppressWarnings("static-method")
151+
@Test
152+
public void testGetLocation() {
153+
PrecisionRectangle r = new PrecisionRectangle(8.94, -34.37, 0, 0);
154+
assertEquals(r.getLocation().getClass(), Point.class);
155+
assertEquals(r.x, r.getLocation().x);
156+
assertEquals(r.y, r.getLocation().y);
137157
}
138158
}

org.eclipse.draw2d/src/org/eclipse/draw2d/geometry/PrecisionRectangle.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -260,15 +260,6 @@ public Rectangle getCopy() {
260260
return getPreciseCopy();
261261
}
262262

263-
/**
264-
* @see org.eclipse.draw2d.geometry.Rectangle#getLocation()
265-
* @since 3.21
266-
*/
267-
@Override
268-
public PrecisionPoint getLocation() {
269-
return new PrecisionPoint(preciseX(), preciseY());
270-
}
271-
272263
/**
273264
* Returns a precise copy of this.
274265
*
@@ -278,15 +269,6 @@ public PrecisionRectangle getPreciseCopy() {
278269
return new PrecisionRectangle(preciseX(), preciseY(), preciseWidth(), preciseHeight());
279270
}
280271

281-
/**
282-
* @see org.eclipse.draw2d.geometry.Rectangle#getSize()
283-
* @since 3.21
284-
*/
285-
@Override
286-
public PrecisionDimension getSize() {
287-
return new PrecisionDimension(preciseWidth(), preciseHeight());
288-
}
289-
290272
/**
291273
* @see org.eclipse.draw2d.geometry.Rectangle#getTop()
292274
*/

org.eclipse.draw2d/src/org/eclipse/draw2d/geometry/Rectangle.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,27 @@ public double preciseY() {
823823
return y;
824824
}
825825

826+
/**
827+
* Returns the precise dimensions of this Rectangle.
828+
*
829+
* @return Size of this Rectangle as a PrecisionDimension
830+
* @since 3.21
831+
*/
832+
public final PrecisionDimension preciseSize() {
833+
return new PrecisionDimension(preciseWidth(), preciseHeight());
834+
}
835+
836+
/**
837+
* Returns the precise upper left hand corner of the rectangle.
838+
*
839+
* @return Precise location of the rectangle
840+
* @see #setLocation(Point)
841+
* @since 3.21
842+
*/
843+
public final PrecisionPoint preciseLocation() {
844+
return new PrecisionPoint(preciseX(), preciseY());
845+
}
846+
826847
/**
827848
* Resizes this Rectangle by the Dimension provided as input and returns this
828849
* for convenience. This Rectange's width will become this.width +

0 commit comments

Comments
 (0)