Skip to content

Commit

Permalink
[java] type casting and numeric improvements (#13909)
Browse files Browse the repository at this point in the history
* replaced condition controll flow with switch

* remove casting from double to int

division operation with integer ergument is equal to cast to int Math.floor()

* remove division by 1 in tests

* replaced manual calculation of hashcode with Long.hashCode()

* removed redundand toString method call

* removed cast to string in string concat

* applying code formatting
  • Loading branch information
iampopovich authored May 6, 2024
1 parent 72562d8 commit 4b41538
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 20 deletions.
29 changes: 17 additions & 12 deletions java/src/org/openqa/selenium/devtools/CdpClientGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -913,18 +913,23 @@ public TypeDeclaration<?> toTypeDeclaration() {
fromJson.getBody().get().addStatement(String.format("return new %s(%s);", name, getMapper()));

MethodDeclaration toJson = classDecl.addMethod("toJson").setPublic(true);
if (type.equals("object")) {
toJson.setType("java.util.Map<String, Object>");
toJson.getBody().get().addStatement(String.format("return %s;", propertyName));
} else if (type.equals("number")) {
toJson.setType(Number.class);
toJson.getBody().get().addStatement(String.format("return %s;", propertyName));
} else if (type.equals("integer")) {
toJson.setType(Integer.class);
toJson.getBody().get().addStatement(String.format("return %s;", propertyName));
} else {
toJson.setType(String.class);
toJson.getBody().get().addStatement(String.format("return %s.toString();", propertyName));
switch (type) {
case "object":
toJson.setType("java.util.Map<String, Object>");
toJson.getBody().get().addStatement(String.format("return %s;", propertyName));
break;
case "number":
toJson.setType(Number.class);
toJson.getBody().get().addStatement(String.format("return %s;", propertyName));
break;
case "integer":
toJson.setType(Integer.class);
toJson.getBody().get().addStatement(String.format("return %s;", propertyName));
break;
default:
toJson.setType(String.class);
toJson.getBody().get().addStatement(String.format("return %s.toString();", propertyName));
break;
}

MethodDeclaration toString = classDecl.addMethod("toString").setPublic(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ protected void assertValidFindBy(FindBy findBy) {
throw new IllegalArgumentException(
String.format(
"You must specify at most one location strategy. Number found: %d (%s)",
finders.size(), finders.toString()));
finders.size(), finders));
}
}

Expand Down
2 changes: 1 addition & 1 deletion java/src/org/openqa/selenium/support/Color.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public int hashCode() {
result = 31 * result + green;
result = 31 * result + blue;
temp = alpha != +0.0d ? Double.doubleToLongBits(alpha) : 0L;
result = 31 * result + (int) (temp ^ (temp >>> 32));
result = 31 * result + Long.hashCode(temp);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ public void testCanMoveOverAndOutOfAnElement() {
inputModule.perform(
windowHandle,
getBuilder(driver)
.moveToElement(redbox, redSize.getWidth() / 1 + 1, redSize.getHeight() / 1 + 1)
.moveToElement(redbox, redSize.getWidth() + 1, redSize.getHeight() + 1)
.getSequences());

wait.until(attributeToBe(redbox, "background-color", Colors.GREEN.getColorValue().asRgba()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private static void sleep(int ms) {
try {
Thread.sleep(ms);
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted: " + e.toString());
throw new RuntimeException("Interrupted: " + e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ public void testCanMoveOverAndOutOfAnElement() {
.isEqualTo(RED.getColorValue());

getBuilder(driver)
.moveToElement(redbox, redSize.getWidth() / 1 + 1, redSize.getHeight() / 1 + 1)
.moveToElement(redbox, redSize.getWidth() + 1, redSize.getHeight() + 1)
.perform();

wait.until(attributeToBe(redbox, "background-color", Colors.GREEN.getColorValue().asRgba()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public void testCanMoveOverAndOutOfAnElement() {
.isEqualTo(RED.getColorValue());

setDefaultPen(driver)
.moveToElement(redbox, redSize.getWidth() / 1 + 1, redSize.getHeight() / 1 + 1)
.moveToElement(redbox, redSize.getWidth() + 1, redSize.getHeight() + 1)
.perform();

wait.until(attributeToBe(redbox, "background-color", Colors.GREEN.getColorValue().asRgba()));
Expand Down Expand Up @@ -407,8 +407,8 @@ public void setPointerEventProperties() {

Rectangle rect = pointerArea.getRect();

int centerX = (int) Math.floor(rect.width / 2 + rect.getX());
int centerY = (int) Math.floor(rect.height / 2 + rect.getY());
int centerX = rect.width / 2 + rect.getX();
int centerY = rect.height / 2 + rect.getY();
Assertions.assertThat(moveTo.get("button")).isEqualTo("-1");
Assertions.assertThat(moveTo.get("pageX")).isEqualTo("" + centerX);
Assertions.assertThat(moveTo.get("pageY")).isEqualTo("" + centerY);
Expand Down

0 comments on commit 4b41538

Please sign in to comment.