Skip to content

Commit 4b41538

Browse files
authored
[java] type casting and numeric improvements (#13909)
* 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
1 parent 72562d8 commit 4b41538

File tree

7 files changed

+25
-20
lines changed

7 files changed

+25
-20
lines changed

java/src/org/openqa/selenium/devtools/CdpClientGenerator.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -913,18 +913,23 @@ public TypeDeclaration<?> toTypeDeclaration() {
913913
fromJson.getBody().get().addStatement(String.format("return new %s(%s);", name, getMapper()));
914914

915915
MethodDeclaration toJson = classDecl.addMethod("toJson").setPublic(true);
916-
if (type.equals("object")) {
917-
toJson.setType("java.util.Map<String, Object>");
918-
toJson.getBody().get().addStatement(String.format("return %s;", propertyName));
919-
} else if (type.equals("number")) {
920-
toJson.setType(Number.class);
921-
toJson.getBody().get().addStatement(String.format("return %s;", propertyName));
922-
} else if (type.equals("integer")) {
923-
toJson.setType(Integer.class);
924-
toJson.getBody().get().addStatement(String.format("return %s;", propertyName));
925-
} else {
926-
toJson.setType(String.class);
927-
toJson.getBody().get().addStatement(String.format("return %s.toString();", propertyName));
916+
switch (type) {
917+
case "object":
918+
toJson.setType("java.util.Map<String, Object>");
919+
toJson.getBody().get().addStatement(String.format("return %s;", propertyName));
920+
break;
921+
case "number":
922+
toJson.setType(Number.class);
923+
toJson.getBody().get().addStatement(String.format("return %s;", propertyName));
924+
break;
925+
case "integer":
926+
toJson.setType(Integer.class);
927+
toJson.getBody().get().addStatement(String.format("return %s;", propertyName));
928+
break;
929+
default:
930+
toJson.setType(String.class);
931+
toJson.getBody().get().addStatement(String.format("return %s.toString();", propertyName));
932+
break;
928933
}
929934

930935
MethodDeclaration toString = classDecl.addMethod("toString").setPublic(true);

java/src/org/openqa/selenium/support/AbstractFindByBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ protected void assertValidFindBy(FindBy findBy) {
109109
throw new IllegalArgumentException(
110110
String.format(
111111
"You must specify at most one location strategy. Number found: %d (%s)",
112-
finders.size(), finders.toString()));
112+
finders.size(), finders));
113113
}
114114
}
115115

java/src/org/openqa/selenium/support/Color.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public int hashCode() {
116116
result = 31 * result + green;
117117
result = 31 * result + blue;
118118
temp = alpha != +0.0d ? Double.doubleToLongBits(alpha) : 0L;
119-
result = 31 * result + (int) (temp ^ (temp >>> 32));
119+
result = 31 * result + Long.hashCode(temp);
120120
return result;
121121
}
122122

java/test/org/openqa/selenium/bidi/input/DefaultMouseTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ public void testCanMoveOverAndOutOfAnElement() {
481481
inputModule.perform(
482482
windowHandle,
483483
getBuilder(driver)
484-
.moveToElement(redbox, redSize.getWidth() / 1 + 1, redSize.getHeight() / 1 + 1)
484+
.moveToElement(redbox, redSize.getWidth() + 1, redSize.getHeight() + 1)
485485
.getSequences());
486486

487487
wait.until(attributeToBe(redbox, "background-color", Colors.GREEN.getColorValue().asRgba()));

java/test/org/openqa/selenium/bidi/input/DragAndDropTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private static void sleep(int ms) {
5757
try {
5858
Thread.sleep(ms);
5959
} catch (InterruptedException e) {
60-
throw new RuntimeException("Interrupted: " + e.toString());
60+
throw new RuntimeException("Interrupted: " + e);
6161
}
6262
}
6363

java/test/org/openqa/selenium/interactions/DefaultMouseTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ public void testCanMoveOverAndOutOfAnElement() {
404404
.isEqualTo(RED.getColorValue());
405405

406406
getBuilder(driver)
407-
.moveToElement(redbox, redSize.getWidth() / 1 + 1, redSize.getHeight() / 1 + 1)
407+
.moveToElement(redbox, redSize.getWidth() + 1, redSize.getHeight() + 1)
408408
.perform();
409409

410410
wait.until(attributeToBe(redbox, "background-color", Colors.GREEN.getColorValue().asRgba()));

java/test/org/openqa/selenium/interactions/PenPointerTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ public void testCanMoveOverAndOutOfAnElement() {
367367
.isEqualTo(RED.getColorValue());
368368

369369
setDefaultPen(driver)
370-
.moveToElement(redbox, redSize.getWidth() / 1 + 1, redSize.getHeight() / 1 + 1)
370+
.moveToElement(redbox, redSize.getWidth() + 1, redSize.getHeight() + 1)
371371
.perform();
372372

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

408408
Rectangle rect = pointerArea.getRect();
409409

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

0 commit comments

Comments
 (0)