package com.wizzardo.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @author: wizzardo
 * Date: 31.10.14
 */
public class UrlMapping<T> {

    protected HashMap<String, T> mapping = new HashMap<>();
    protected LinkedHashMap<Pattern, T> regexpMapping = new LinkedHashMap<>();

    public T get(String path) throws IOException {
        T handler = mapping.get(path);
        if (handler != null)
            return handler;

        for (Map.Entry<Pattern, T> entry : regexpMapping.entrySet()) {
            if (entry.getKey().matcher(path).matches())
                return entry.getValue();
        }

        return null;
    }

    public UrlMapping append(String url, T handler) {
        if (url.contains("*")) {
            regexpMapping.put(Pattern.compile(url.replace("*", ".*")), handler);
        } else {
            mapping.put(url, handler);
        }

        return this;
    }
}
package com.wizzardo.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @author: wizzardo
 * Date: 31.10.14
 */
public class UrlMapping<T> implements Iterable<T> {

    protected HashMap<String, T> mapping = new HashMap<>();
    protected LinkedHashMap<Pattern, T> regexpMapping = new LinkedHashMap<>();

    public T get(String path) throws IOException {
        T handler = mapping.get(path);
        if (handler != null)
            return handler;

        for (Map.Entry<Pattern, T> entry : regexpMapping.entrySet()) {
            if (entry.getKey().matcher(path).matches())
                return entry.getValue();
        }

        return null;
    }

    public UrlMapping append(String url, T handler) {
        if (url.contains("*")) {
            regexpMapping.put(Pattern.compile(url.replace("*", ".*")), handler);
        } else {
            mapping.put(url, handler);
        }

        return this;
    }

    @Override
    public Iterator<T> iterator() {
        return new Iterator<T>() {
            Iterator<T> mapping = UrlMapping.this.mapping.values().iterator();
            Iterator<T> regexpMapping = UrlMapping.this.regexpMapping.values().iterator();

            @Override
            public boolean hasNext() {
                return mapping.hasNext() || regexpMapping.hasNext();
            }

            @Override
            public T next() {
                return mapping.hasNext() ? mapping.next() : regexpMapping.next();
            }
        };
    }
}
make urlMapping Iterable