org.mockserver.collections.ImmutableEntry Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mockserver-core Show documentation
Show all versions of mockserver-core Show documentation
Functionality used by all MockServer modules for matching and expectations
package org.mockserver.collections;
import org.apache.commons.lang3.tuple.Pair;
import org.mockserver.matchers.RegexStringMatcher;
import org.mockserver.model.NottableString;
import java.util.*;
import static org.mockserver.model.NottableString.string;
public class ImmutableEntry extends Pair implements Map.Entry {
private final RegexStringMatcher regexStringMatcher;
private final NottableString key;
private final NottableString value;
public static ImmutableEntry entry(RegexStringMatcher regexStringMatcher, String key, String value) {
return new ImmutableEntry(regexStringMatcher, key, value);
}
public static ImmutableEntry entry(RegexStringMatcher regexStringMatcher, NottableString key, NottableString value) {
return new ImmutableEntry(regexStringMatcher, key, value);
}
ImmutableEntry(RegexStringMatcher regexStringMatcher, String key, String value) {
this.regexStringMatcher = regexStringMatcher;
this.key = string(key);
this.value = string(value);
}
ImmutableEntry(RegexStringMatcher regexStringMatcher, NottableString key, NottableString value) {
this.regexStringMatcher = regexStringMatcher;
this.key = key;
this.value = value;
}
@Override
public NottableString getLeft() {
return key;
}
@Override
public NottableString getRight() {
return value;
}
@Override
public NottableString setValue(NottableString value) {
throw new UnsupportedOperationException("ImmutableEntry is immutable");
}
@Override
public String toString() {
return "(" + key + ": " + value + ")";
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ImmutableEntry that = (ImmutableEntry) o;
return regexStringMatcher.matches(key, that.key, true) &&
regexStringMatcher.matches(value, that.value, true) ||
(
!regexStringMatcher.matches(key, that.key, true) &&
(
key.isOptional() || that.key.isOptional()
)
);
}
@Override
public int hashCode() {
return Objects.hash(key, value);
}
public static boolean listsEqual(List matcher, List matched) {
boolean matches = false;
if (matcher.size() == matched.size()) {
Set matchedIndexes = new HashSet<>();
Set matcherIndexes = new HashSet<>();
for (int i = 0; i < matcher.size(); i++) {
T matcherItem = matcher.get(i);
for (int j = 0; j < matched.size(); j++) {
T matchedItem = matched.get(j);
if (matcherItem != null && matcherItem.equals(matchedItem)) {
matchedIndexes.add(j);
matcherIndexes.add(i);
}
}
}
matches = matchedIndexes.size() == matched.size() && matcherIndexes.size() == matcher.size();
}
return matches;
}
public static boolean listsEqualWithOptionals(RegexStringMatcher regexStringMatcher, List matcher, List matched) {
Set matchingMatchedIndexes = new HashSet<>();
Set matchingMatcherIndexes = new HashSet<>();
Set matcherKeys = new HashSet<>();
matcher.forEach(matcherItem -> matcherKeys.add(matcherItem.getKey()));
Set matchedKeys = new HashSet<>();
matched.forEach(matchedItem -> matchedKeys.add(matchedItem.getKey()));
for (int i = 0; i < matcher.size(); i++) {
ImmutableEntry matcherItem = matcher.get(i);
if (matcherItem != null) {
for (int j = 0; j < matched.size(); j++) {
ImmutableEntry matchedItem = matched.get(j);
if (matchedItem != null) {
if (matcherItem.equals(matchedItem)) {
matchingMatchedIndexes.add(j);
matchingMatcherIndexes.add(i);
} else if (matcherItem.getKey().isOptional() && !contains(regexStringMatcher, matchedKeys, matcherItem.getKey())) {
matchingMatchedIndexes.add(j);
matchingMatcherIndexes.add(i);
} else if (matchedItem.getKey().isOptional() && !contains(regexStringMatcher, matcherKeys, matchedItem.getKey())) {
matchingMatchedIndexes.add(j);
matchingMatcherIndexes.add(i);
}
}
}
}
}
return matchingMatchedIndexes.size() == matched.size() && matchingMatcherIndexes.size() == matcher.size();
}
private static boolean contains(RegexStringMatcher regexStringMatcher, Set matchedKeys, NottableString matcherItem) {
boolean result = false;
for (NottableString matchedKey : matchedKeys) {
if (regexStringMatcher.matches(matchedKey, matcherItem, true)) {
return true;
}
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy