Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/java/src/org/openqa/selenium/DeviceRotation.java
1865 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.Collections;
21
import java.util.HashMap;
22
import java.util.Map;
23
import java.util.Objects;
24
25
/**
26
* Defines an object which represents the three dimensional plane and how a device can be rotated
27
* about it. Each of the axes is in positive degrees on the real number scale (0 <= deg <=
28
* 360).
29
*
30
* <p>Example instantiation to rotate device to "Landscape Right": DeviceRotation(0, 0, 90);
31
*/
32
public class DeviceRotation {
33
// The default orientation is portrait.
34
private int x = 0;
35
private int y = 0;
36
private int z = 0;
37
38
/** Instantiate a DeviceRotation object based on three integers. */
39
public DeviceRotation(int x, int y, int z) {
40
this.x = x;
41
this.y = y;
42
this.z = z;
43
this.validateParameters(this.x, this.y, this.z);
44
}
45
46
/**
47
* Instantiate a DeviceRotation object based on a HashMap object where the keys are the axes x, y,
48
* and z respectively: x : xVal y : yVal z : zVal
49
*/
50
public DeviceRotation(Map<String, Number> map) {
51
if (map == null || !map.containsKey("x") || !map.containsKey("y") || !map.containsKey("z")) {
52
throw new IllegalArgumentException(
53
"Could not initialize DeviceRotation with map given: " + map);
54
}
55
this.x = map.get("x").intValue();
56
this.y = map.get("y").intValue();
57
this.z = map.get("z").intValue();
58
this.validateParameters(x, y, z);
59
}
60
61
private void validateParameters(int x, int y, int z) {
62
if (x < 0 || y < 0 || z < 0) {
63
throw new IllegalArgumentException(
64
String.format(
65
"DeviceRotation requires positive axis values: %nx = %s%ny = %s%nz = %s", x, y, z));
66
} else if (x >= 360 || y >= 360 || z >= 360) {
67
throw new IllegalArgumentException(
68
String.format(
69
"DeviceRotation requires positive axis values under 360: %nx = %s%ny = %s%nz = %s",
70
x, y, z));
71
}
72
}
73
74
/**
75
* @return The x.
76
*/
77
public int getX() {
78
return x;
79
}
80
81
/**
82
* @return The y.
83
*/
84
public int getY() {
85
return y;
86
}
87
88
/**
89
* @return The z.
90
*/
91
public int getZ() {
92
return z;
93
}
94
95
/**
96
* @return All axes mapped to a Map.
97
*/
98
public Map<String, Integer> parameters() {
99
HashMap<String, Integer> values = new HashMap<>();
100
values.put("x", this.x);
101
values.put("y", this.y);
102
values.put("z", this.z);
103
return Collections.unmodifiableMap(values);
104
}
105
106
@Override
107
public boolean equals(Object o) {
108
if (!(o instanceof DeviceRotation)) {
109
return false;
110
}
111
if (o == this) {
112
return true;
113
}
114
115
DeviceRotation obj = (DeviceRotation) o;
116
return obj.getX() == this.getX() && obj.getY() == this.getY() && obj.getZ() == this.getZ();
117
}
118
119
@Override
120
public int hashCode() {
121
return Objects.hash(getX(), getY(), getZ());
122
}
123
}
124
125