Beispiel #1
0
        public V get(String key)
        {
            BaseNode branch = this;

            char[] chars = key.toCharArray();
            for (char aChar : chars)
            {
                if (branch == null)
                {
                    return(null);
                }
                branch = branch.getChild(aChar);
            }

            if (branch == null)
            {
                return(null);
            }
            // 下面这句可以保证只有成词的节点被返回
            if (!(branch.status == Status.WORD_END_3 || branch.status == Status.WORD_MIDDLE_2))
            {
                return(null);
            }
            return((V)branch.getValue());
        }
Beispiel #2
0
        protected boolean addChild(BaseNode node)
        {
            boolean  add    = false;
            char     c      = node.getChar();
            BaseNode target = getChild(c);

            if (target == null)
            {
                child[c] = node;
                add      = true;
            }
            else
            {
                switch (node.status)
                {
                case UNDEFINED_0:
                    if (target.status != Status.NOT_WORD_1)
                    {
                        target.status = Status.NOT_WORD_1;
                        add           = true;
                    }
                    break;

                case NOT_WORD_1:
                    if (target.status == Status.WORD_END_3)
                    {
                        target.status = Status.WORD_MIDDLE_2;
                    }
                    break;

                case WORD_END_3:
                    if (target.status == Status.NOT_WORD_1)
                    {
                        target.status = Status.WORD_MIDDLE_2;
                    }
                    if (target.getValue() == null)
                    {
                        add = true;
                    }
                    target.setValue(node.getValue());
                    break;
                }
            }
            return(add);
        }