org.mockserver.collections.CaseInsensitiveRegexMultiMap 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.builder.EqualsBuilder;
import org.mockserver.matchers.RegexStringMatcher;
import org.mockserver.model.NottableString;
import org.mockserver.model.ObjectWithReflectiveEqualsHashCodeToString;
import java.util.*;
import static org.mockserver.model.NottableString.string;
import static org.mockserver.model.NottableString.strings;
/**
* MultiMap that uses case insensitive regex expression matching for keys and values
*
* @author jamesdbloom
*/
public class CaseInsensitiveRegexMultiMap extends ObjectWithReflectiveEqualsHashCodeToString implements Map {
private final CaseInsensitiveNottableRegexListHashMap backingMap = new CaseInsensitiveNottableRegexListHashMap();
public static CaseInsensitiveRegexMultiMap multiMap(String[]... keyAndValues) {
CaseInsensitiveRegexMultiMap multiMap = new CaseInsensitiveRegexMultiMap();
for (String[] keyAndValue : keyAndValues) {
for (int i = 1; i < keyAndValue.length; i++) {
multiMap.put(keyAndValue[0], keyAndValue[i]);
}
}
return multiMap;
}
public static CaseInsensitiveRegexMultiMap multiMap(NottableString[]... keyAndValues) {
CaseInsensitiveRegexMultiMap multiMap = new CaseInsensitiveRegexMultiMap();
for (NottableString[] keyAndValue : keyAndValues) {
for (int i = 1; i < keyAndValue.length; i++) {
multiMap.put(keyAndValue[0], keyAndValue[i]);
}
}
return multiMap;
}
public static Entry entry(String key, String value) {
return new ImmutableEntry(key, value);
}
public boolean containsAll(CaseInsensitiveRegexMultiMap subSet) {
if (size() == 0 && subSet.allKeysNotted()) {
return true;
} else {
for (Entry entry : subSet.entryList()) {
if ((entry.getKey().isNot() || entry.getValue().isNot()) && containsKeyValue(entry.getKey().getValue(), entry.getValue().getValue())) {
return false;
} else if (!containsKeyValue(entry.getKey(), entry.getValue())) {
return false;
}
}
}
return true;
}
private boolean allKeysNotted() {
for (NottableString key : keySet()) {
if (!key.isNot()) {
return false;
}
}
return true;
}
public synchronized boolean containsKeyValue(String key, String value) {
return containsKeyValue(string(key), string(value));
}
public synchronized boolean containsKeyValue(NottableString key, NottableString value) {
boolean result = false;
for (Entry matcherEntry : entryList()) {
if (RegexStringMatcher.matches(value, matcherEntry.getValue(), true)
&& RegexStringMatcher.matches(key, matcherEntry.getKey(), true)) {
result = true;
break;
}
}
return result;
}
@Override
public synchronized boolean containsValue(Object value) {
if (value instanceof NottableString) {
for (NottableString key : backingMap.keySet()) {
for (List allKeyValues : backingMap.getAll(key)) {
for (NottableString keyValue : allKeyValues) {
if (RegexStringMatcher.matches(keyValue, (NottableString) value, false)) {
return true;
}
}
}
}
} else if (value instanceof String) {
return containsValue(string((String) value));
}
return false;
}
@Override
public synchronized boolean containsKey(Object key) {
return backingMap.containsKey(key);
}
@Override
public synchronized NottableString get(Object key) {
if (key instanceof String) {
return get(string((String) key));
} else {
List values = backingMap.get(key);
if (values != null && values.size() > 0) {
return values.get(0);
} else {
return null;
}
}
}
public synchronized List getAll(String key) {
return getAll(string(key));
}
public synchronized List getAll(NottableString key) {
List all = new ArrayList();
for (List subList : backingMap.getAll(key)) {
all.addAll(subList);
}
return all;
}
public synchronized NottableString put(String key, String value) {
return put(string(key), string(value));
}
@Override
public synchronized NottableString put(NottableString key, NottableString value) {
List list = Collections.synchronizedList(new ArrayList());
for (Entry entry : entryList()) {
if (EqualsBuilder.reflectionEquals(entry.getKey(), key)) {
list.add(entry.getValue());
}
}
list.add(value);
backingMap.put(key, list);
return value;
}
public synchronized List put(String key, List values) {
return put(string(key), strings(values));
}
public synchronized List put(NottableString key, List values) {
if (containsKey(key)) {
for (NottableString value : values) {
put(key, value);
}
} else {
backingMap.put(key, values);
}
return values;
}
public void putValuesForNewKeys(CaseInsensitiveRegexMultiMap multiMap) {
for (NottableString key : multiMap.keySet()) {
if (!containsKey(key)) {
backingMap.put(key, multiMap.getAll(key));
}
}
}
@Override
public synchronized NottableString remove(Object key) {
if (key instanceof String) {
return remove(string((String) key));
} else {
List values = backingMap.get(key);
if (values != null && values.size() > 0) {
NottableString removed = values.remove(0);
if (values.size() == 0) {
backingMap.remove(key);
}
return removed;
} else {
return null;
}
}
}
public synchronized List removeAll(NottableString key) {
return backingMap.remove(key);
}
public synchronized List removeAll(String key) {
return backingMap.remove(key);
}
@Override
public synchronized void putAll(Map extends NottableString, ? extends NottableString> map) {
for (Entry extends NottableString, ? extends NottableString> entry : map.entrySet()) {
put(entry.getKey(), entry.getValue());
}
}
@Override
public synchronized void clear() {
backingMap.clear();
}
@Override
public synchronized Set keySet() {
return backingMap.keySet();
}
@Override
public synchronized Collection values() {
Collection values = new ArrayList();
for (List valuesForKey : backingMap.values()) {
values.addAll(valuesForKey);
}
return values;
}
@Override
public synchronized Set> entrySet() {
Set> entrySet = new LinkedHashSet>();
for (Entry> entry : backingMap.entrySet()) {
for (NottableString value : entry.getValue()) {
entrySet.add(new ImmutableEntry(entry.getKey(), value));
}
}
return entrySet;
}
public synchronized List> entryList() {
List> entrySet = new ArrayList>();
for (Entry> entry : backingMap.entrySet()) {
for (NottableString value : entry.getValue()) {
entrySet.add(new ImmutableEntry(entry.getKey(), value));
}
}
return entrySet;
}
@Override
public synchronized int size() {
return backingMap.size();
}
@Override
public synchronized boolean isEmpty() {
return backingMap.isEmpty();
}
static class ImmutableEntry extends ObjectWithReflectiveEqualsHashCodeToString implements Entry {
private final NottableString key;
private final NottableString value;
ImmutableEntry(String key, String value) {
this.key = string(key);
this.value = string(value);
}
ImmutableEntry(NottableString key, NottableString value) {
this.key = key;
this.value = value;
}
@Override
public NottableString getKey() {
return key;
}
@Override
public NottableString getValue() {
return value;
}
@Override
public NottableString setValue(NottableString value) {
throw new UnsupportedOperationException("ImmutableEntry is immutable");
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy