zoukankan      html  css  js  c++  java
  • Better ChildFactory

    http://blogs.oracle.com/geertjan/entry/better_childfactory

    ————————————————————————————————————————————————————————————————————————

    Thanks to Jesse, here's a corrected & improved ChildFactory from a few days ago, using ChildFactory.Detachable so that you have addNotify/removeNotify to add/remove the LookupListener and thus avoid a memory leak, together with a createKeys/resultChanged that is simplified too.
    private class FirstLevelChildFactory extends ChildFactory.Detachable<ResultType> implements LookupListener {

        Result<ResultSet> resultRetrievedFromYahoo;

        @Override
        protected void addNotify() {
            resultRetrievedFromYahoo = Utilities.actionsGlobalContext().lookupResult(ResultSet.class);
            resultRetrievedFromYahoo.addLookupListener(this);
        }

        @Override
        protected void removeNotify() {
            resultRetrievedFromYahoo.removeLookupListener(this);
            resultRetrievedFromYahoo = null;
        }

        @Override
        protected boolean createKeys(List<ResultType> list) {
            for (ResultSet rs : resultRetrievedFromYahoo.allInstances()) {
                list.addAll(rs.getResult());
            }
            return true;
        }

        @Override
        protected Node createNodeForKey(ResultType key) {
            String[] split = key.getTitle().split(" ");
            String theNextWordToBeSearched = split[split.length - 1];
            AbstractNode firstLevelNode =
                    new AbstractNode(
                    Children.create(new GenericChildFactory(theNextWordToBeSearched), true),
                    Lookups.singleton(key));
            firstLevelNode.setShortDescription(key.getSummary());
            firstLevelNode.setDisplayName(key.getTitle());
            return firstLevelNode;
        }

        @Override
        public void resultChanged(LookupEvent le) {
            refresh(true);
        }

    }
    This is a very simple & elegant solution.

  • 相关阅读:
    AC自动机
    HDU
    2020牛客寒假算法基础集训营3 B 牛牛的DRB迷宫II
    POJ 3784 Running Median【维护动态中位数】
    CodeForces
    HDU 2444 The Accomodation of Students【二分图最大匹配问题】
    POJ 1201 Intervals【差分约束】
    POJ 2976 Dropping tests【0/1分数规划模板】
    2019牛客暑期多校训练营(第七场)A.String【最小表示法】
    POJ 1287 Networking【kruskal模板题】
  • 原文地址:https://www.cnblogs.com/cuizhf/p/2189950.html
Copyright © 2011-2022 走看看