Path: blob/trunk/java/src/org/openqa/selenium/PersistentCapabilities.java
1865 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.Map;20import java.util.Set;21import java.util.function.Function;22import java.util.stream.Collectors;23import java.util.stream.Stream;24import org.jspecify.annotations.NullMarked;25import org.jspecify.annotations.Nullable;26import org.openqa.selenium.internal.Require;2728@NullMarked29public class PersistentCapabilities implements Capabilities {3031private final ImmutableCapabilities caps;32private final ImmutableCapabilities overrides;33private final int hashCode;3435public PersistentCapabilities() {36this(new ImmutableCapabilities());37}3839public PersistentCapabilities(Capabilities source) {40this(source, new ImmutableCapabilities());41}4243private PersistentCapabilities(Capabilities previousValues, Capabilities newValues) {44Require.nonNull("Source capabilities", previousValues, "may be empty, but must be set.");45Require.nonNull("Additional capabilities", newValues, "may be empty, but must be set.");46this.caps = ImmutableCapabilities.copyOf(previousValues);47this.overrides = ImmutableCapabilities.copyOf(newValues);48this.hashCode = SharedCapabilitiesMethods.hashCode(this);49}5051public PersistentCapabilities setCapability(String name, Object value) {52Require.nonNull("Name", name);53Require.nonNull("Value", value);5455return new PersistentCapabilities(this, new ImmutableCapabilities(name, value));56}5758@Override59public Map<String, Object> asMap() {60return getCapabilityNames().stream()61.collect(Collectors.toUnmodifiableMap(Function.identity(), this::getCapability));62}6364@Override65public @Nullable Object getCapability(String capabilityName) {66Require.nonNull("Capability name", capabilityName);67Object capability = overrides.getCapability(capabilityName);68if (capability != null) {69return capability;70}71return caps.getCapability(capabilityName);72}7374@Override75public Capabilities merge(Capabilities other) {76Require.nonNull("Other capabilities", other, "may be empty, but must be set.");77return new PersistentCapabilities(this, other);78}7980@Override81public Set<String> getCapabilityNames() {82return Stream.concat(83caps.getCapabilityNames().stream(), overrides.getCapabilityNames().stream())84.collect(Collectors.toUnmodifiableSet());85}8687@Override88public String toString() {89return SharedCapabilitiesMethods.toString(this);90}9192@Override93public int hashCode() {94return hashCode;95}9697@Override98public boolean equals(@Nullable Object o) {99if (!(o instanceof Capabilities)) {100return false;101}102return SharedCapabilitiesMethods.equals(this, (Capabilities) o);103}104}105106107