Path: blob/trunk/java/src/org/openqa/selenium/DeviceRotation.java
1865 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.Collections;20import java.util.HashMap;21import java.util.Map;22import java.util.Objects;2324/**25* Defines an object which represents the three dimensional plane and how a device can be rotated26* about it. Each of the axes is in positive degrees on the real number scale (0 <= deg <=27* 360).28*29* <p>Example instantiation to rotate device to "Landscape Right": DeviceRotation(0, 0, 90);30*/31public class DeviceRotation {32// The default orientation is portrait.33private int x = 0;34private int y = 0;35private int z = 0;3637/** Instantiate a DeviceRotation object based on three integers. */38public DeviceRotation(int x, int y, int z) {39this.x = x;40this.y = y;41this.z = z;42this.validateParameters(this.x, this.y, this.z);43}4445/**46* Instantiate a DeviceRotation object based on a HashMap object where the keys are the axes x, y,47* and z respectively: x : xVal y : yVal z : zVal48*/49public DeviceRotation(Map<String, Number> map) {50if (map == null || !map.containsKey("x") || !map.containsKey("y") || !map.containsKey("z")) {51throw new IllegalArgumentException(52"Could not initialize DeviceRotation with map given: " + map);53}54this.x = map.get("x").intValue();55this.y = map.get("y").intValue();56this.z = map.get("z").intValue();57this.validateParameters(x, y, z);58}5960private void validateParameters(int x, int y, int z) {61if (x < 0 || y < 0 || z < 0) {62throw new IllegalArgumentException(63String.format(64"DeviceRotation requires positive axis values: %nx = %s%ny = %s%nz = %s", x, y, z));65} else if (x >= 360 || y >= 360 || z >= 360) {66throw new IllegalArgumentException(67String.format(68"DeviceRotation requires positive axis values under 360: %nx = %s%ny = %s%nz = %s",69x, y, z));70}71}7273/**74* @return The x.75*/76public int getX() {77return x;78}7980/**81* @return The y.82*/83public int getY() {84return y;85}8687/**88* @return The z.89*/90public int getZ() {91return z;92}9394/**95* @return All axes mapped to a Map.96*/97public Map<String, Integer> parameters() {98HashMap<String, Integer> values = new HashMap<>();99values.put("x", this.x);100values.put("y", this.y);101values.put("z", this.z);102return Collections.unmodifiableMap(values);103}104105@Override106public boolean equals(Object o) {107if (!(o instanceof DeviceRotation)) {108return false;109}110if (o == this) {111return true;112}113114DeviceRotation obj = (DeviceRotation) o;115return obj.getX() == this.getX() && obj.getY() == this.getY() && obj.getZ() == this.getZ();116}117118@Override119public int hashCode() {120return Objects.hash(getX(), getY(), getZ());121}122}123124125