|
8 | 8 | // |
9 | 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | 10 | // |
11 | | -// Unless required by applicable law or agreed to in writing, |
| 11 | +// Unless required by applicable law or agreed in writing, |
12 | 12 | // software distributed under the License is distributed on an |
13 | 13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | 14 | // KIND, either express or implied. See the License for the |
|
22 | 22 | import java.util.Map; |
23 | 23 |
|
24 | 24 | public class SetGeolocationOverrideParameters { |
25 | | - private final GeolocationCoordinates coordinates; |
26 | | - private final GeolocationPositionError error; |
27 | | - private final List<String> contexts; |
28 | | - private final List<String> userContexts; |
| 25 | + private final Map<String, Object> map = new HashMap<>(); |
29 | 26 |
|
30 | | - public SetGeolocationOverrideParameters( |
31 | | - GeolocationCoordinates coordinates, |
32 | | - GeolocationPositionError error, |
33 | | - List<String> contexts, |
34 | | - List<String> userContexts) { |
35 | | - |
36 | | - this.coordinates = coordinates; |
37 | | - this.error = error; |
38 | | - this.contexts = contexts; |
39 | | - this.userContexts = userContexts; |
40 | | - |
41 | | - if (this.coordinates != null && this.error != null) { |
42 | | - throw new IllegalArgumentException("Cannot specify both coordinates and error"); |
43 | | - } |
44 | | - if (this.contexts != null && this.userContexts != null) { |
45 | | - throw new IllegalArgumentException("Cannot specify both contexts and userContexts"); |
| 27 | + // Constructor for coordinates - must specify either contexts or userContexts later |
| 28 | + public SetGeolocationOverrideParameters(GeolocationCoordinates coordinates) { |
| 29 | + if (coordinates == null) { |
| 30 | + throw new IllegalArgumentException("GeolocationCoordinates cannot be null"); |
46 | 31 | } |
| 32 | + map.put("coordinates", coordinates.toMap()); |
| 33 | + } |
47 | 34 |
|
48 | | - if (this.contexts == null && this.userContexts == null) { |
49 | | - throw new IllegalArgumentException("Must specify either contexts or userContexts"); |
| 35 | + // Constructor for error - must specify either contexts or userContexts later |
| 36 | + public SetGeolocationOverrideParameters(GeolocationPositionError error) { |
| 37 | + if (error == null) { |
| 38 | + throw new IllegalArgumentException("GeolocationPositionError cannot be null"); |
50 | 39 | } |
| 40 | + map.put("error", error.toMap()); |
51 | 41 | } |
52 | 42 |
|
53 | | - public SetGeolocationOverrideParameters( |
54 | | - GeolocationCoordinates coordinates, GeolocationPositionError error) { |
55 | | - this(coordinates, error, null, null); |
| 43 | + public SetGeolocationOverrideParameters contexts(List<String> contexts) { |
| 44 | + if (contexts == null || contexts.isEmpty()) { |
| 45 | + throw new IllegalArgumentException("Contexts cannot be null or empty"); |
| 46 | + } |
| 47 | + if (map.containsKey("userContexts")) { |
| 48 | + throw new IllegalArgumentException("Cannot specify both contexts and userContexts"); |
| 49 | + } |
| 50 | + map.put("contexts", contexts); |
| 51 | + return this; |
56 | 52 | } |
57 | 53 |
|
58 | | - public Map<String, Object> toMap() { |
59 | | - Map<String, Object> param = new HashMap<>(); |
60 | | - |
61 | | - if (this.coordinates != null) { |
62 | | - param.put("coordinates", this.coordinates.toMap()); |
| 54 | + public SetGeolocationOverrideParameters userContexts(List<String> userContexts) { |
| 55 | + if (userContexts == null || userContexts.isEmpty()) { |
| 56 | + throw new IllegalArgumentException("User contexts cannot be null or empty"); |
63 | 57 | } |
64 | | - |
65 | | - if (this.error != null) { |
66 | | - param.put("error", this.error.toMap()); |
| 58 | + if (map.containsKey("contexts")) { |
| 59 | + throw new IllegalArgumentException("Cannot specify both contexts and userContexts"); |
67 | 60 | } |
| 61 | + map.put("userContexts", userContexts); |
| 62 | + return this; |
| 63 | + } |
68 | 64 |
|
69 | | - if (this.contexts != null) { |
70 | | - param.put("contexts", this.contexts); |
71 | | - } else { |
72 | | - param.put("userContexts", this.userContexts); |
| 65 | + public Map<String, Object> toMap() { |
| 66 | + // Validate that either contexts or userContexts is set |
| 67 | + if (!map.containsKey("contexts") && !map.containsKey("userContexts")) { |
| 68 | + throw new IllegalStateException("Must specify either contexts or userContexts"); |
73 | 69 | } |
74 | | - |
75 | | - return Map.copyOf(param); |
| 70 | + return Map.copyOf(map); |
76 | 71 | } |
77 | 72 | } |
0 commit comments