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;
    }
}
@@ -70,32 +70,59 @@
 a.util.HashMap;%0A
+import java.util.Iterator;%0A
 import java.util
@@ -129,32 +129,32 @@
 .LinkedHashMap;%0A
-
 import java.util
@@ -265,16 +265,39 @@
 pping%3CT%3E
+ implements Iterable%3CT%3E
  %7B%0A%0A    
@@ -1014,32 +1014,32 @@
 er);%0A        %7D%0A%0A
-
         return t
@@ -1048,10 +1048,577 @@
 s;%0A    %7D
+%0A%0A    @Override%0A    public Iterator%3CT%3E iterator() %7B%0A        return new Iterator%3CT%3E() %7B%0A            Iterator%3CT%3E mapping = UrlMapping.this.mapping.values().iterator();%0A            Iterator%3CT%3E regexpMapping = UrlMapping.this.regexpMapping.values().iterator();%0A%0A            @Override%0A            public boolean hasNext() %7B%0A                return mapping.hasNext() %7C%7C regexpMapping.hasNext();%0A            %7D%0A%0A            @Override%0A            public T next() %7B%0A                return mapping.hasNext() ? mapping.next() : regexpMapping.next();%0A            %7D%0A        %7D;%0A    %7D
 %0A%7D

make urlMapping Iterable