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