|
| 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.bidi.emulation; |
| 19 | + |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +public class GeolocationCoordinates { |
| 24 | + private final Map<String, Object> map = new HashMap<>(); |
| 25 | + |
| 26 | + public GeolocationCoordinates(double latitude, double longitude) { |
| 27 | + if (latitude < -90.0 || latitude > 90.0) { |
| 28 | + throw new IllegalArgumentException("Latitude must be between -90.0 and 90.0"); |
| 29 | + } |
| 30 | + if (longitude < -180.0 || longitude > 180.0) { |
| 31 | + throw new IllegalArgumentException("Longitude must be between -180.0 and 180.0"); |
| 32 | + } |
| 33 | + |
| 34 | + map.put("latitude", latitude); |
| 35 | + map.put("longitude", longitude); |
| 36 | + map.put("accuracy", 1.0); // Default accuracy |
| 37 | + } |
| 38 | + |
| 39 | + public GeolocationCoordinates accuracy(double accuracy) { |
| 40 | + if (accuracy < 0.0) { |
| 41 | + throw new IllegalArgumentException("Accuracy must be >= 0.0"); |
| 42 | + } |
| 43 | + map.put("accuracy", accuracy); |
| 44 | + return this; |
| 45 | + } |
| 46 | + |
| 47 | + public GeolocationCoordinates altitude(Double altitude) { |
| 48 | + if (altitude != null) { |
| 49 | + map.put("altitude", altitude); |
| 50 | + } |
| 51 | + return this; |
| 52 | + } |
| 53 | + |
| 54 | + public GeolocationCoordinates altitudeAccuracy(Double altitudeAccuracy) { |
| 55 | + if (altitudeAccuracy != null) { |
| 56 | + if (!map.containsKey("altitude")) { |
| 57 | + throw new IllegalArgumentException("altitudeAccuracy cannot be set without altitude"); |
| 58 | + } |
| 59 | + if (altitudeAccuracy < 0.0) { |
| 60 | + throw new IllegalArgumentException("Altitude accuracy must be >= 0.0"); |
| 61 | + } |
| 62 | + map.put("altitudeAccuracy", altitudeAccuracy); |
| 63 | + } |
| 64 | + return this; |
| 65 | + } |
| 66 | + |
| 67 | + public GeolocationCoordinates heading(Double heading) { |
| 68 | + if (heading != null) { |
| 69 | + if (heading < 0.0 || heading >= 360.0) { |
| 70 | + throw new IllegalArgumentException("Heading must be between 0.0 and 360.0"); |
| 71 | + } |
| 72 | + map.put("heading", heading); |
| 73 | + } |
| 74 | + return this; |
| 75 | + } |
| 76 | + |
| 77 | + public GeolocationCoordinates speed(Double speed) { |
| 78 | + if (speed != null) { |
| 79 | + if (speed < 0.0) { |
| 80 | + throw new IllegalArgumentException("Speed must be >= 0.0"); |
| 81 | + } |
| 82 | + map.put("speed", speed); |
| 83 | + } |
| 84 | + return this; |
| 85 | + } |
| 86 | + |
| 87 | + public double getLatitude() { |
| 88 | + return (Double) map.get("latitude"); |
| 89 | + } |
| 90 | + |
| 91 | + public double getLongitude() { |
| 92 | + return (Double) map.get("longitude"); |
| 93 | + } |
| 94 | + |
| 95 | + public double getAccuracy() { |
| 96 | + return (Double) map.get("accuracy"); |
| 97 | + } |
| 98 | + |
| 99 | + public Double getAltitude() { |
| 100 | + return (Double) map.get("altitude"); |
| 101 | + } |
| 102 | + |
| 103 | + public Double getAltitudeAccuracy() { |
| 104 | + return (Double) map.get("altitudeAccuracy"); |
| 105 | + } |
| 106 | + |
| 107 | + public Double getHeading() { |
| 108 | + return (Double) map.get("heading"); |
| 109 | + } |
| 110 | + |
| 111 | + public Double getSpeed() { |
| 112 | + return (Double) map.get("speed"); |
| 113 | + } |
| 114 | + |
| 115 | + public Map<String, Object> toMap() { |
| 116 | + return Map.copyOf(map); |
| 117 | + } |
| 118 | +} |
0 commit comments