通过HashSet迭代清空HashMap条目

时间:2011-12-28 15:16:23

标签: java tomcat

以下方法获取“Route”(类名和类方法):

public Route getRoute(final String method, final String request) {
    if (hasRoutes) {
        for (Map.Entry<Pattern, HashMap<String, String>> entry : routes) {
            Matcher match = entry.getKey().matcher(request);

            if (match.find()) {
                HashMap<String, String> methods = entry.getValue();

                // ISSUE: Returns FALSE after 1st call of Router.getRoute()
                if (methods.containsKey(method)) {
                    return new Route(match.group("interface"), "TRUE (" + method + " - " + match.group("interface") + "): " + methods.get(method));
                } else {
                    return new Route(match.group("interface"), "FALSE (" + method + " - " + match.group("interface") + "): " + methods.values().toString() + ", SIZE: " + entry.getValue().size());
                }

                //return entry.getValue().containsKey(method) ? new Route(match.group("interface"), entry.getValue().get(method)) : null;
            }
        }
    }

    return null;
}

“routes”定义为:

private Set<Entry<Pattern, HashMap<String, String>>> routes;

它是JSON配置文件的缓存表示,用于定义支持的路由,例如:

{
    "^/?(?<interface>threads)/?$": {
        "GET": "list",
        "POST": "create"
    },
    "^/?(?<interface>threads)/(?<id>\\d+)/?$": {
        "GET": "get",
        "POST": "reply",
        "PUT": "edit",
        "PATCH": "edit",
        "DELETE": "delete"
    }
}

编辑,这里是如何从JSON文件的内容填充“路由”:

    try {
        JsonParser parser = JSONFactory.createJsonParser(in);
        JsonNode root = JSONMapper.readTree(parser);
        Iterator base = root.getFieldNames();
        Iterator node;
        String match, method;
        HashMap<Pattern, HashMap<String, String>> routesMap = new HashMap();

        while (base.hasNext()) {
            match = base.next().toString();

            if (match != null) {
                node = root.get(match).getFieldNames();
                HashMap<String, String> methods = new HashMap();

                while (node.hasNext()) {
                    method = node.next().toString();

                    if (method != null) {
                        methods.put(method, root.get(match).get(method).getTextValue());
                    }
                }

                if (!methods.isEmpty()) {
                    routesMap.put(Pattern.compile(match), methods);
                }
            }
        }

        if (!routesMap.isEmpty()) {
            hasRoutes = true;
            routes = routesMap.entrySet();
        }

        // Help garbage collection
        parser = null;
        root = null;
        base = null;
        node = null;
        match = null;
        method = null;
        routesMap = null;
    } catch (Exception ex) {
    }

编辑2,有问题的属性&amp; init()方法:

public final static JsonFactory JSONFactory = new JsonFactory();
public final static ObjectMapper JSONMapper = new ObjectMapper();
public static Router router;
private final Class self = getClass();
private final ClassLoader loader = self.getClassLoader();

public void init(ServletConfig config) throws ServletException {
    super.init(config);

    router = new Router(self.getResourceAsStream("/v1_0/Routes.json"), JSONFactory, JSONMapper);
}

由于某些原因,在第一次HashMap没有值后访问servlet时。 x.size()返回零。

这是一个从头开始重写的PHP应用程序,所以如果问题是平凡的,我会提前道歉。

完整来源:   - Router source   - Route source

2 个答案:

答案 0 :(得分:0)

  

通过HashSet迭代清空HashMap条目

这不会发生。简单地迭代HashSet对集合的内容没有副作用。

此处还有其他问题导致您的问题。

答案 1 :(得分:0)

您的getOptions()方法会在迭代时删除此地图中的所有条目。因此,在调用getOptions()一次后,地图为空。

顺便说一句,为变量分配null “帮助垃圾收集器。”垃圾收集器知道当退出变量的范围时,该变量不再引用该对象。实际上,您通过分配永远无法读取的值(null)以及使用适得其反的噪声混乱代码来减慢速度。像FindBugs这样的好的静态分析工具会警告你这是错误的代码。