Path: blob/trunk/java/src/org/openqa/selenium/PersistentCapabilities.java
4004 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.Nullable;25import org.openqa.selenium.internal.Require;2627public class PersistentCapabilities implements Capabilities {2829private final ImmutableCapabilities caps;30private final ImmutableCapabilities overrides;31private final int hashCode;3233public PersistentCapabilities() {34this(new ImmutableCapabilities());35}3637public PersistentCapabilities(Capabilities source) {38this(source, new ImmutableCapabilities());39}4041private PersistentCapabilities(Capabilities previousValues, Capabilities newValues) {42Require.nonNull("Source capabilities", previousValues, "may be empty, but must be set.");43Require.nonNull("Additional capabilities", newValues, "may be empty, but must be set.");44this.caps = ImmutableCapabilities.copyOf(previousValues);45this.overrides = ImmutableCapabilities.copyOf(newValues);46this.hashCode = SharedCapabilitiesMethods.hashCode(this);47}4849public PersistentCapabilities setCapability(String name, Object value) {50Require.nonNull("Name", name);51Require.nonNull("Value", value);5253return new PersistentCapabilities(this, new ImmutableCapabilities(name, value));54}5556@Override57public Map<String, Object> asMap() {58return getCapabilityNames().stream()59.collect(Collectors.toUnmodifiableMap(Function.identity(), this::getCapability));60}6162@Override63public @Nullable Object getCapability(String capabilityName) {64Require.nonNull("Capability name", capabilityName);65Object capability = overrides.getCapability(capabilityName);66if (capability != null) {67return capability;68}69return caps.getCapability(capabilityName);70}7172@Override73public Capabilities merge(Capabilities other) {74Require.nonNull("Other capabilities", other, "may be empty, but must be set.");75return new PersistentCapabilities(this, other);76}7778@Override79public Set<String> getCapabilityNames() {80return Stream.concat(81caps.getCapabilityNames().stream(), overrides.getCapabilityNames().stream())82.collect(Collectors.toUnmodifiableSet());83}8485@Override86public String toString() {87return SharedCapabilitiesMethods.toString(this);88}8990@Override91public int hashCode() {92return hashCode;93}9495@Override96public boolean equals(@Nullable Object o) {97if (!(o instanceof Capabilities)) {98return false;99}100return SharedCapabilitiesMethods.equals(this, (Capabilities) o);101}102}103104105