Path: blob/trunk/java/src/org/openqa/selenium/DeviceRotation.java
3992 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617package org.openqa.selenium;1819import java.util.Map;20import java.util.Objects;2122/**23* Defines an object which represents the three dimensional plane and how a device can be rotated24* about it. Each of the axes is in positive degrees on the real number scale (0 <= deg <=25* 360).26*27* <p>Example instantiation to rotate device to "Landscape Right": DeviceRotation(0, 0, 90);28*/29public class DeviceRotation {30// The default orientation is portrait.31private int x = 0;32private int y = 0;33private int z = 0;3435/** Instantiate a DeviceRotation object based on three integers. */36public DeviceRotation(int x, int y, int z) {37this.x = x;38this.y = y;39this.z = z;40this.validateParameters(this.x, this.y, this.z);41}4243/**44* Instantiate a DeviceRotation object based on a HashMap object where the keys are the axes x, y,45* and z respectively: x : xVal y : yVal z : zVal46*/47public DeviceRotation(Map<String, Number> map) {48if (map == null || !map.containsKey("x") || !map.containsKey("y") || !map.containsKey("z")) {49throw new IllegalArgumentException(50"Could not initialize DeviceRotation with map given: " + map);51}52this.x = map.get("x").intValue();53this.y = map.get("y").intValue();54this.z = map.get("z").intValue();55this.validateParameters(x, y, z);56}5758private void validateParameters(int x, int y, int z) {59if (x < 0 || y < 0 || z < 0) {60throw new IllegalArgumentException(61String.format(62"DeviceRotation requires positive axis values: %nx = %s%ny = %s%nz = %s", x, y, z));63} else if (x >= 360 || y >= 360 || z >= 360) {64throw new IllegalArgumentException(65String.format(66"DeviceRotation requires positive axis values under 360: %nx = %s%ny = %s%nz = %s",67x, y, z));68}69}7071/**72* @return The x.73*/74public int getX() {75return x;76}7778/**79* @return The y.80*/81public int getY() {82return y;83}8485/**86* @return The z.87*/88public int getZ() {89return z;90}9192/**93* @return All axes mapped to a Map.94*/95public Map<String, Integer> parameters() {96return Map.of("x", this.x, "y", this.y, "z", this.z);97}9899@Override100public boolean equals(Object o) {101if (!(o instanceof DeviceRotation)) {102return false;103}104if (o == this) {105return true;106}107108DeviceRotation obj = (DeviceRotation) o;109return obj.getX() == this.getX() && obj.getY() == this.getY() && obj.getZ() == this.getZ();110}111112@Override113public int hashCode() {114return Objects.hash(getX(), getY(), getZ());115}116}117118119