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

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

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

import org.mockserver.matchers.RegexStringMatcher;

import java.util.*;

/**
 * Map that uses case insensitive regex expression matching for keys
 *
 * @author jamesdbloom
 */
class CaseInsensitiveRegexHashMap extends LinkedHashMap implements Map {
    static final long serialVersionUID = 1530623482381786485L;

    @Override
    public synchronized boolean containsKey(Object key) {
        if (key instanceof String) {
            if (super.containsKey(key)) {
                return true;
            } else {
                for (String keyToCompare : keySet()) {
                    if (RegexStringMatcher.matches((String) key, keyToCompare, true)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    @Override
    public synchronized V get(Object key) {
        if (key instanceof String) {
            if (super.get(key) != null) {
                return super.get(key);
            } else {
                for (String keyToCompare : keySet()) {
                    if (RegexStringMatcher.matches((String) key, keyToCompare, true)) {
                        return super.get(keyToCompare);
                    }
                }
            }
        }
        return null;
    }

    public synchronized Collection getAll(Object key) {
        List values = new ArrayList();
        if (key instanceof String) {
            for (String keyToCompare : keySet()) {
                if (RegexStringMatcher.matches((String) key, keyToCompare, true)) {
                    values.add(super.get(keyToCompare));
                }
            }
        }
        return values;
    }

    @Override
    public synchronized V remove(Object key) {
        if (key instanceof String) {
            if (super.get(key) != null) {
                return super.remove(key);
            } else {
                for (String keyToCompare : keySet()) {
                    if (RegexStringMatcher.matches((String) key, keyToCompare, true)) {
                        return super.remove(keyToCompare);
                    }
                }
            }
        }
        return null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy