|
17 | 17 |
|
18 | 18 | package org.openqa.selenium;
|
19 | 19 |
|
| 20 | +import java.util.Arrays; |
20 | 21 | import java.util.Collection;
|
21 | 22 | import java.util.Comparator;
|
22 | 23 | import java.util.IdentityHashMap;
|
23 | 24 | import java.util.Map;
|
24 |
| -import java.util.Objects; |
25 | 25 | import java.util.stream.Collectors;
|
26 | 26 | import java.util.stream.Stream;
|
27 | 27 | import org.openqa.selenium.logging.LogLevelMapping;
|
28 | 28 | import org.openqa.selenium.logging.LoggingPreferences;
|
29 | 29 |
|
30 | 30 | class SharedCapabilitiesMethods {
|
31 | 31 |
|
| 32 | + private static final String[] EMPTY_ARRAY = new String[0]; |
| 33 | + |
32 | 34 | private SharedCapabilitiesMethods() {
|
33 | 35 | // Utility class
|
34 | 36 | }
|
35 | 37 |
|
36 | 38 | static int hashCode(Capabilities caps) {
|
37 |
| - return caps.getCapabilityNames().stream() |
38 |
| - .sorted() |
39 |
| - .mapToInt(name -> Objects.hash(name, caps.getCapability(name))) |
40 |
| - .reduce(0, (l, r) -> l ^ r); |
| 39 | + String[] sortedNames = caps.getCapabilityNames().toArray(EMPTY_ARRAY); |
| 40 | + Arrays.sort(sortedNames, String::compareTo); |
| 41 | + // we only use the names to generate a hash code, this might result in hash collisions. thanks to the |
| 42 | + // moz:firefoxOptions, goog:chromeOptions and ms:edgeOptions, these hash collisions should not happen too often. |
| 43 | + return Arrays.hashCode(sortedNames); |
41 | 44 | }
|
42 | 45 |
|
43 | 46 | static boolean equals(Capabilities left, Capabilities right) {
|
44 |
| - return left.hashCode() == right.hashCode(); |
| 47 | + if (left == right) { |
| 48 | + return true; |
| 49 | + } |
| 50 | + // deeply compare the keys & values, usually only called when the hash codes of two objects are identical. |
| 51 | + // note: there should be no arrays (directly or nested) inside the map, otherwise the equals will not work. |
| 52 | + return left.asMap().equals(right.asMap()); |
45 | 53 | }
|
46 | 54 |
|
47 | 55 | static void setCapability(Map<String, Object> caps, String key, Object value) {
|
|
0 commit comments