Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/java/src/org/openqa/selenium/PersistentCapabilities.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.Map;
21
import java.util.Set;
22
import java.util.function.Function;
23
import java.util.stream.Collectors;
24
import java.util.stream.Stream;
25
import org.jspecify.annotations.NullMarked;
26
import org.jspecify.annotations.Nullable;
27
import org.openqa.selenium.internal.Require;
28
29
@NullMarked
30
public class PersistentCapabilities implements Capabilities {
31
32
private final ImmutableCapabilities caps;
33
private final ImmutableCapabilities overrides;
34
private final int hashCode;
35
36
public PersistentCapabilities() {
37
this(new ImmutableCapabilities());
38
}
39
40
public PersistentCapabilities(Capabilities source) {
41
this(source, new ImmutableCapabilities());
42
}
43
44
private PersistentCapabilities(Capabilities previousValues, Capabilities newValues) {
45
Require.nonNull("Source capabilities", previousValues, "may be empty, but must be set.");
46
Require.nonNull("Additional capabilities", newValues, "may be empty, but must be set.");
47
this.caps = ImmutableCapabilities.copyOf(previousValues);
48
this.overrides = ImmutableCapabilities.copyOf(newValues);
49
this.hashCode = SharedCapabilitiesMethods.hashCode(this);
50
}
51
52
public PersistentCapabilities setCapability(String name, Object value) {
53
Require.nonNull("Name", name);
54
Require.nonNull("Value", value);
55
56
return new PersistentCapabilities(this, new ImmutableCapabilities(name, value));
57
}
58
59
@Override
60
public Map<String, Object> asMap() {
61
return getCapabilityNames().stream()
62
.collect(Collectors.toUnmodifiableMap(Function.identity(), this::getCapability));
63
}
64
65
@Override
66
public @Nullable Object getCapability(String capabilityName) {
67
Require.nonNull("Capability name", capabilityName);
68
Object capability = overrides.getCapability(capabilityName);
69
if (capability != null) {
70
return capability;
71
}
72
return caps.getCapability(capabilityName);
73
}
74
75
@Override
76
public Capabilities merge(Capabilities other) {
77
Require.nonNull("Other capabilities", other, "may be empty, but must be set.");
78
return new PersistentCapabilities(this, other);
79
}
80
81
@Override
82
public Set<String> getCapabilityNames() {
83
return Stream.concat(
84
caps.getCapabilityNames().stream(), overrides.getCapabilityNames().stream())
85
.collect(Collectors.toUnmodifiableSet());
86
}
87
88
@Override
89
public String toString() {
90
return SharedCapabilitiesMethods.toString(this);
91
}
92
93
@Override
94
public int hashCode() {
95
return hashCode;
96
}
97
98
@Override
99
public boolean equals(@Nullable Object o) {
100
if (!(o instanceof Capabilities)) {
101
return false;
102
}
103
return SharedCapabilitiesMethods.equals(this, (Capabilities) o);
104
}
105
}
106
107