Path: blob/trunk/java/src/org/openqa/selenium/MutableCapabilities.java
3990 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.HashSet;21import java.util.Map;22import java.util.Set;23import java.util.TreeMap;24import org.jspecify.annotations.Nullable;25import org.openqa.selenium.internal.Require;2627public class MutableCapabilities implements Capabilities {2829private static final Set<String> OPTION_KEYS;3031static {32HashSet<String> keys = new HashSet<>();33keys.add("goog:chromeOptions");34keys.add("ms:edgeOptions");35keys.add("moz:firefoxOptions");36keys.add("se:ieOptions");37OPTION_KEYS = Collections.unmodifiableSet(keys);38}3940private final Map<String, Object> caps = new TreeMap<>();4142public MutableCapabilities() {43// no-arg constructor44}4546public MutableCapabilities(Capabilities other) {47this(other.asMap());48}4950public MutableCapabilities(Map<String, ?> capabilities) {51capabilities.forEach(52(key, value) -> {53if (value != null) {54setCapability(key, value);55}56});57}5859/**60* Merge two {@link Capabilities} together and return the union of the two as a new {@link61* Capabilities} instance. Capabilities from {@code other} will override those in {@code this}.62*/63@Override64public MutableCapabilities merge(Capabilities other) {65MutableCapabilities newInstance = new MutableCapabilities(this);66if (other != null) {67other.asMap().forEach(newInstance::setCapability);68}69return newInstance;70}7172public void setCapability(String capabilityName, boolean value) {73setCapability(capabilityName, (Object) value);74}7576public void setCapability(String capabilityName, String value) {77setCapability(capabilityName, (Object) value);78}7980public void setCapability(String capabilityName, Platform value) {81setCapability(capabilityName, (Object) value);82}8384public void setCapability(String key, @Nullable Object value) {85Require.nonNull("Capability name", key);8687// We have to special-case some keys and values because of the popular idiom of calling88// something like "capabilities.setCapability(SafariOptions.CAPABILITY, new SafariOptions());"89// and this is no longer needed as options are capabilities. There will be a large amount of90// legacy code that will always try and follow this pattern, however.91if (OPTION_KEYS.contains(key) && value instanceof Capabilities) {92((Capabilities) value).asMap().forEach(this::setCapability);93return;94}9596if (value == null) {97caps.remove(key);98return;99}100101SharedCapabilitiesMethods.setCapability(caps, key, value);102}103104@Override105public Map<String, Object> asMap() {106return Collections.unmodifiableMap(caps);107}108109@Override110public @Nullable Object getCapability(String capabilityName) {111return caps.get(capabilityName);112}113114@Override115public Set<String> getCapabilityNames() {116return Collections.unmodifiableSet(caps.keySet());117}118119public Map<String, Object> toJson() {120return asMap();121}122123@Override124public int hashCode() {125return SharedCapabilitiesMethods.hashCode(this);126}127128@Override129public boolean equals(@Nullable Object o) {130if (!(o instanceof Capabilities)) {131return false;132}133return SharedCapabilitiesMethods.equals(this, (Capabilities) o);134}135136@Override137public String toString() {138return SharedCapabilitiesMethods.toString(this);139}140}141142143