You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by rw...@apache.org on 2017/06/24 00:58:09 UTC
svn commit: r1799725 - in /pivot/trunk/wtk:
src/org/apache/pivot/wtk/Bounds.java
test/org/apache/pivot/wtk/test/BoundsTest.java
Author: rwhitcomb
Date: Sat Jun 24 00:58:09 2017
New Revision: 1799725
URL: http://svn.apache.org/viewvc?rev=1799725&view=rev
Log:
Code cleanup: Rename "xxxArgument" to "xxxValue" in parameter names.
Add an "expand()" method to Bounds.
Add BoundsTest that tests a lot of things about Bounds, including the new JSON
parsing and "translate" / "expand".
Added:
pivot/trunk/wtk/test/org/apache/pivot/wtk/test/BoundsTest.java
Modified:
pivot/trunk/wtk/src/org/apache/pivot/wtk/Bounds.java
Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/Bounds.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/Bounds.java?rev=1799725&r1=1799724&r2=1799725&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/Bounds.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/Bounds.java Sat Jun 24 00:58:09 2017
@@ -141,18 +141,18 @@ public final class Bounds implements Ser
* bounds specified by the arguments (X- and Y-position will be the minimum of either
* and width and height will be the maximum).
*
- * @param xArgument The X-position of the bounded area to union with this one.
- * @param yArgument The Y-position of the other bounded area.
- * @param widthArgument The width of the other area to union with this one.
- * @param heightArgument The other area's height.
+ * @param xValue The X-position of the bounded area to union with this one.
+ * @param yValue The Y-position of the other bounded area.
+ * @param widthValue The width of the other area to union with this one.
+ * @param heightValue The other area's height.
* @return A new bounds that is the union of this one with the bounds specified by
* the given arguments.
*/
- public Bounds union(int xArgument, int yArgument, int widthArgument, int heightArgument) {
- int x1 = Math.min(this.x, xArgument);
- int y1 = Math.min(this.y, yArgument);
- int x2 = Math.max(this.x + this.width, xArgument + widthArgument);
- int y2 = Math.max(this.y + this.height, yArgument + heightArgument);
+ public Bounds union(int xValue, int yValue, int widthValue, int heightValue) {
+ int x1 = Math.min(this.x, xValue);
+ int y1 = Math.min(this.y, yValue);
+ int x2 = Math.max(this.x + this.width, xValue + widthValue);
+ int y2 = Math.max(this.y + this.height, yValue + heightValue);
return new Bounds(x1, y1, x2 - x1, y2 - y1);
@@ -176,17 +176,17 @@ public final class Bounds implements Ser
* <p> "Intersection" means the new bounds will include only the area that is common to
* both areas (X- and Y-position will be the maximum of either, while width and height will
* be the minimum of the two).
- * @param xArgument The X-position of the other area to intersect with.
- * @param yArgument The Y-position of the other area.
- * @param widthArgument The width of the other bounded area.
- * @param heightArgument The height of the other area.
+ * @param xValue The X-position of the other area to intersect with.
+ * @param yValue The Y-position of the other area.
+ * @param widthValue The width of the other bounded area.
+ * @param heightValue The height of the other area.
* @return The new bounds that is the intersection of this one and the given area.
*/
- public Bounds intersect(int xArgument, int yArgument, int widthArgument, int heightArgument) {
- int x1 = Math.max(this.x, xArgument);
- int y1 = Math.max(this.y, yArgument);
- int x2 = Math.min(this.x + this.width, xArgument + widthArgument);
- int y2 = Math.min(this.y + this.height, yArgument + heightArgument);
+ public Bounds intersect(int xValue, int yValue, int widthValue, int heightValue) {
+ int x1 = Math.max(this.x, xValue);
+ int y1 = Math.max(this.y, yValue);
+ int x2 = Math.min(this.x + this.width, xValue + widthValue);
+ int y2 = Math.min(this.y + this.height, yValue + heightValue);
return new Bounds(x1, y1, x2 - x1, y2 - y1);
}
@@ -230,6 +230,18 @@ public final class Bounds implements Ser
}
/**
+ * Create a new bounds object that represents this bounds expanded/contracted by
+ * the given width/height values (negative represent contraction).
+ * <p> The new bounds have the same x- and y-origin values as the original.
+ * @param dw The amount of expansion(contraction) in the width.
+ * @param dh The amount of expansion(contraction) in the height.
+ * @return A new bounds expanded by this amount.
+ */
+ public Bounds expand(int dw, int dh) {
+ return new Bounds(x, y, width + dw, height + dh);
+ }
+
+ /**
* @return A new bounds offset by the amounts given by the point.
* @param offset X- and Y-values which are used to offset this bounds
* to a new position (must not be {@code null}).
@@ -256,15 +268,15 @@ public final class Bounds implements Ser
/**
* Does this bounded area contain the point defined by the given arguments?
- * @param xArgument The X-position of the other point to test.
- * @param yArgument The Y-position of the other point to test.
+ * @param xValue The X-position of the other point to test.
+ * @param yValue The Y-position of the other point to test.
* @return Whether this bounds contains the given point.
*/
- public boolean contains(int xArgument, int yArgument) {
- return (xArgument >= this.x &&
- yArgument >= this.y &&
- xArgument < this.x + width &&
- yArgument < this.y + height);
+ public boolean contains(int xValue, int yValue) {
+ return (xValue >= this.x &&
+ yValue >= this.y &&
+ xValue < this.x + width &&
+ yValue < this.y + height);
}
/**
@@ -283,17 +295,17 @@ public final class Bounds implements Ser
/**
* @return Does this bounded area completely contain (could be coincident with) the bounded area
* specified by the given arguments?
- * @param xArgument The X-position of the area to test.
- * @param yArgument The Y-position of the other area.
- * @param widthArgument The width of the other area.
- * @param heightArgument The height of the area to test.
+ * @param xValue The X-position of the area to test.
+ * @param yValue The Y-position of the other area.
+ * @param widthValue The width of the other area.
+ * @param heightValue The height of the area to test.
*/
- public boolean contains(int xArgument, int yArgument, int widthArgument, int heightArgument) {
+ public boolean contains(int xValue, int yValue, int widthValue, int heightValue) {
return (!isEmpty() &&
- xArgument >= this.x &&
- yArgument >= this.y &&
- xArgument + widthArgument <= this.x + this.width &&
- yArgument + heightArgument <= this.y + this.height);
+ xValue >= this.x &&
+ yValue >= this.y &&
+ xValue + widthValue <= this.x + this.width &&
+ yValue + heightValue <= this.y + this.height);
}
/**
@@ -310,17 +322,17 @@ public final class Bounds implements Ser
/**
* @return Does this bounded area intersect with the bounded area given by the arguments?
- * @param xArgument The X-position of the other area to check.
- * @param yArgument The Y-position of the other area.
- * @param widthArgument The width of the other bounded area.
- * @param heightArgument The height of the other area.
+ * @param xValue The X-position of the other area to check.
+ * @param yValue The Y-position of the other area.
+ * @param widthValue The width of the other bounded area.
+ * @param heightValue The height of the other area.
*/
- public boolean intersects(int xArgument, int yArgument, int widthArgument, int heightArgument) {
+ public boolean intersects(int xValue, int yValue, int widthValue, int heightValue) {
return (!isEmpty() &&
- xArgument + widthArgument > this.x &&
- yArgument + heightArgument > this.y &&
- xArgument < this.x + this.width &&
- yArgument < this.y + this.height);
+ xValue + widthValue > this.x &&
+ yValue + heightValue > this.y &&
+ xValue < this.x + this.width &&
+ yValue < this.y + this.height);
}
/**
Added: pivot/trunk/wtk/test/org/apache/pivot/wtk/test/BoundsTest.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/test/org/apache/pivot/wtk/test/BoundsTest.java?rev=1799725&view=auto
==============================================================================
--- pivot/trunk/wtk/test/org/apache/pivot/wtk/test/BoundsTest.java (added)
+++ pivot/trunk/wtk/test/org/apache/pivot/wtk/test/BoundsTest.java Sat Jun 24 00:58:09 2017
@@ -0,0 +1,87 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the bndecific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pivot.wtk.test;
+
+import java.awt.Rectangle;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+import org.apache.pivot.wtk.Bounds;
+import org.apache.pivot.wtk.Dimensions;
+import org.apache.pivot.wtk.Point;
+
+
+/**
+ * Tests the {@link Bounds} class which is used extensively
+ * in the "wtk" source, and deserves good tests.
+ */
+public class BoundsTest {
+
+ @Test
+ public void test() {
+ Bounds bnd_1 = new Bounds(-1, -1, 0, 0);
+ Bounds bnd0 = new Bounds(0, 0, 0, 0);
+ Bounds bnd1 = new Bounds(1, 1, 1, 1);
+
+ Dimensions dim0 = new Dimensions(0, 0);
+ Dimensions dim1 = new Dimensions(1, 1);
+ Point p10 = new Point(10, 10);
+ Bounds bnd10 = new Bounds(p10, dim1);
+
+ Bounds bnd2 = Bounds.decode("[2, 3, 4, 5]");
+ Bounds bnd3 = Bounds.decode("{x:2, y:3, width:4, height:5}");
+ Bounds bnd3a = new Bounds(2, 3, 4, 5);
+
+ Bounds bnd4 = new Bounds(4, 4, 4, 5);
+ Bounds bnd5 = bnd3a.translate(1, 1); // -> {3, 4, 4, 5}
+ bnd5 = bnd5.expand(-2, -4);
+ Bounds bnd5a = new Bounds(3, 4, 2, 1);
+ Bounds bnd5b = new Bounds(4, 3, 1, 2);
+ Bounds bndN = new Bounds(0, 0, 8, 9);
+ Bounds bndAll = bnd1.union(bnd0).union(bnd2).union(bnd3).union(bnd4);
+
+ assertNotEquals(bnd_1, bnd0);
+ assertNotEquals(bnd0, bnd1);
+
+ assertEquals(bnd10.getLocation(), p10);
+ assertEquals(bnd10.getSize(), dim1);
+ assertEquals(bnd2, bnd3);
+ assertEquals(bnd3, bnd3a);
+
+ assertEquals(bnd_1.getSize(), dim0);
+ assertEquals(bnd0.getSize(), dim0);
+ assertEquals(bnd1.getSize(), dim1);
+
+ assertFalse(bnd1.contains(bnd0));
+
+ assertFalse(bnd_1.intersects(bnd0));
+ assertFalse(bnd0.intersects(bnd1));
+ assertEquals(bnd0.intersect(bnd1), new Bounds(1, 1, -1, -1));
+ assertTrue(bnd5a.intersects(bnd5b));
+ assertTrue(bnd0.union(bnd1).equals(new Bounds(0, 0, 2, 2)));
+
+ assertFalse(bnd0.equals(bnd1));
+
+ assertTrue(bnd5.equals(bnd5a));
+ assertEquals(bndN, bndAll);
+ }
+
+}