Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/java/src/org/openqa/selenium/DeviceRotation.java
4012 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
package org.openqa.selenium;
19
20
import java.util.Map;
21
import java.util.Objects;
22
23
/**
24
* Defines an object which represents the three dimensional plane and how a device can be rotated
25
* about it. Each of the axes is in positive degrees on the real number scale (0 <= deg <=
26
* 360).
27
*
28
* <p>Example instantiation to rotate device to "Landscape Right": DeviceRotation(0, 0, 90);
29
*/
30
public class DeviceRotation {
31
// The default orientation is portrait.
32
private int x = 0;
33
private int y = 0;
34
private int z = 0;
35
36
/** Instantiate a DeviceRotation object based on three integers. */
37
public DeviceRotation(int x, int y, int z) {
38
this.x = x;
39
this.y = y;
40
this.z = z;
41
this.validateParameters(this.x, this.y, this.z);
42
}
43
44
/**
45
* Instantiate a DeviceRotation object based on a HashMap object where the keys are the axes x, y,
46
* and z respectively: x : xVal y : yVal z : zVal
47
*/
48
public DeviceRotation(Map<String, Number> map) {
49
if (map == null || !map.containsKey("x") || !map.containsKey("y") || !map.containsKey("z")) {
50
throw new IllegalArgumentException(
51
"Could not initialize DeviceRotation with map given: " + map);
52
}
53
this.x = map.get("x").intValue();
54
this.y = map.get("y").intValue();
55
this.z = map.get("z").intValue();
56
this.validateParameters(x, y, z);
57
}
58
59
private void validateParameters(int x, int y, int z) {
60
if (x < 0 || y < 0 || z < 0) {
61
throw new IllegalArgumentException(
62
String.format(
63
"DeviceRotation requires positive axis values: %nx = %s%ny = %s%nz = %s", x, y, z));
64
} else if (x >= 360 || y >= 360 || z >= 360) {
65
throw new IllegalArgumentException(
66
String.format(
67
"DeviceRotation requires positive axis values under 360: %nx = %s%ny = %s%nz = %s",
68
x, y, z));
69
}
70
}
71
72
/**
73
* @return The x.
74
*/
75
public int getX() {
76
return x;
77
}
78
79
/**
80
* @return The y.
81
*/
82
public int getY() {
83
return y;
84
}
85
86
/**
87
* @return The z.
88
*/
89
public int getZ() {
90
return z;
91
}
92
93
/**
94
* @return All axes mapped to a Map.
95
*/
96
public Map<String, Integer> parameters() {
97
return Map.of("x", this.x, "y", this.y, "z", this.z);
98
}
99
100
@Override
101
public boolean equals(Object o) {
102
if (!(o instanceof DeviceRotation)) {
103
return false;
104
}
105
if (o == this) {
106
return true;
107
}
108
109
DeviceRotation obj = (DeviceRotation) o;
110
return obj.getX() == this.getX() && obj.getY() == this.getY() && obj.getZ() == this.getZ();
111
}
112
113
@Override
114
public int hashCode() {
115
return Objects.hash(getX(), getY(), getZ());
116
}
117
}
118
119