All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mockserver.collections.CaseInsensitiveRegexMultiMap Maven / Gradle / Ivy

There is a newer version: 5.15.0
Show newest version
package org.mockserver.collections;

import org.mockserver.matchers.RegexStringMatcher;
import org.mockserver.model.EqualsHashCodeToString;

import java.util.*;

/**
 * MultiMap that uses case insensitive regex expression matching for keys and values
 *
 * @author jamesdbloom
 */
public class CaseInsensitiveRegexMultiMap extends EqualsHashCodeToString implements Map {
    private final CaseInsensitiveRegexHashMap> backingMap = new CaseInsensitiveRegexHashMap>();

    @Override
    public synchronized int size() {
        return backingMap.size();
    }

    @Override
    public synchronized boolean isEmpty() {
        return backingMap.isEmpty();
    }

    @Override
    public synchronized boolean containsKey(Object key) {
        return backingMap.containsKey(key);
    }

    public synchronized boolean containsKeyValue(String key, String value) {
        for (String matcherKey : backingMap.keySet()) {
            for (List allMatcherKeyValues : backingMap.getAll(matcherKey)) {
                for (String matcherKeyValue : allMatcherKeyValues) {
                    if (RegexStringMatcher.matches(matcherKey, key, true) && RegexStringMatcher.matches(matcherKeyValue, value, false)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    @Override
    public synchronized boolean containsValue(Object value) {
        if (value instanceof String) {
            for (String key : backingMap.keySet()) {
                for (List allKeyValues : backingMap.getAll(key)) {
                    for (String keyValue : allKeyValues) {
                        if (RegexStringMatcher.matches(keyValue, (String) value, false)) {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }

    public boolean containsAll(CaseInsensitiveRegexMultiMap subSet) {
        for (String subSetKey : subSet.keySet()) {
            if (!containsKey(subSetKey)) { // check if sub-set key exists in super-set
                return false;
            } else { // check if sub-set value matches at least one super-set value using regex
                for (String subSetValue : subSet.getAll(subSetKey)) {
                    if (!containsKeyValue(subSetKey, subSetValue)) {
                        return false;
                    }
                }
            }
        }
        return true;
    }

    @Override
    public synchronized String get(Object key) {
        List values = backingMap.get(key);
        if (values != null && values.size() > 0) {
            return values.get(0);
        } else {
            return null;
        }
    }

    public synchronized List getAll(Object key) {
        List all = new ArrayList();
        for (List subList : backingMap.getAll(key)) {
            all.addAll(subList);
        }
        return all;
    }

    @Override
    public synchronized String put(String key, String value) {
        List list = Collections.synchronizedList(new ArrayList());
        if (containsKey(key)) {
            list.addAll(backingMap.get(key));
        }
        list.add(value);
        backingMap.put(key, list);
        return value;
    }

    public synchronized List put(String key, List values) {
        if (containsKey(key)) {
            for (String value : values) {
                put(key, value);
            }
        } else {
            backingMap.put(key, values);
        }
        return values;
    }

    public void putValuesForNewKeys(CaseInsensitiveRegexMultiMap multiMap) {
        for (String key : multiMap.keySet()) {
            if (!containsKey(key)) {
                backingMap.put(key, multiMap.getAll(key));
            }
        }
    }

    @Override
    public synchronized String remove(Object key) {
        List values = backingMap.get(key);
        if (values != null && values.size() > 0) {
            return values.remove(0);
        } else {
            return null;
        }
    }

    public synchronized List removeAll(String key) {
        return backingMap.remove(key);
    }

    @Override
    public synchronized void putAll(Map map) {
        for (Entry 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 (String value : entry.getValue()) {
                entrySet.add(new ImmutableEntry(entry.getKey(), value));
            }
        }
        return entrySet;
    }

    class ImmutableEntry extends EqualsHashCodeToString implements Entry {
        private final String key;
        private final String value;

        ImmutableEntry(String key, String value) {
            this.key = key;
            this.value = value;
        }

        @Override
        public String getKey() {
            return key;
        }

        @Override
        public String getValue() {
            return value;
        }

        @Override
        public String setValue(String value) {
            throw new UnsupportedOperationException("ImmutableEntry is immutable");
        }
    }

}







© 2015 - 2025 Weber Informatics LLC | Privacy Policy