📄 dictnode.java
字号:
/*
* Copyright 2002-2005 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Created on 2005-12-28
* author 谢骋超
*
*/
package cn.edu.zju.dartsplitter.data;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.log4j.Logger;
import cn.edu.zju.dartsplitter.exceptions.DartSplitterException;
/**
* 树状词库的一个节点,包括当前字的字符值,及它的父亲、儿子等信息
* 它本身是个composite模式,可以循环包含无数子节点
* @author xiecc
* @email xieccy@gmail.com xieccy@yahoo.com
* homepage: http://blog.itpub.net/xiecc
* projectpage: http://ccnt.zju.edu.cn/projects
*/
public class DictNode implements Serializable {
/**
* Logger for this class
*/
private static final Logger logger = Logger.getLogger(DictNode.class);
private int nodeId;
/**
*
*/
private static final long serialVersionUID = 6605791922243654331L;
private String value;
private int level;
private DictNode parent;
private boolean end;
private int useCount = 0;
private Map<String, DictNode> childNodes = new HashMap<String, DictNode>();
/**
* 一个空节点,现在用来表示树根
* 注意:由于DictNode会被序列化,因此EMPTY_NODE并不是singleton的,比较时还是要用equals,而不是==
*/
public static DictNode EMPTY_NODE = new DictNode(0, "empty", null,0);
/**
* @return Returns the useCount.
*/
public int getUseCount() {
return useCount;
}
/**
* @param useCount
* The useCount to set.
*/
public void setUseCount(int useCount) {
this.useCount = useCount;
}
public void incUseCount() {
this.useCount++;
}
public DictNode(int level, String value, DictNode parent,int nodeId) {
super();
this.level = level;
this.value = value;
this.parent = parent;
this.nodeId=nodeId;
}
/**
* @return Returns the childNodes.
*/
public Map<String, DictNode> getChildNodes() {
return childNodes;
}
/**
* @param childNodes
* The childNodes to set.
*/
public void setChildNodes(Map<String, DictNode> childNodes) {
this.childNodes = childNodes;
}
/**
* @return Returns the level.
*/
public int getLevel() {
return level;
}
/**
* @param level
* The level to set.
*/
public void setLevel(int level) {
this.level = level;
}
/**
* @return Returns the parent.
*/
public DictNode getParent() {
return parent;
}
/**
* @param parent
* The parent to set.
*/
public void setParent(DictNode parent) {
this.parent = parent;
}
/**
* @return Returns the value.
*/
public String getValue() {
return value;
}
/**
* @param value
* The value to set.
*/
public void setValue(String value) {
this.value = value;
}
public void addChildNode(DictNode childNode) {
this.childNodes.put(childNode.getValue(), childNode);
}
public DictNode getChildNode(String value) {
return this.childNodes.get(value);
}
/**
* @return Returns the end.
*/
public boolean hasEnd() {
return end;
}
/**
* @param end
* The end to set.
*/
public void setEnd(boolean end) {
this.end = end;
}
public String getTokenValue() {
StringBuilder sb = new StringBuilder();
DictNode curNode = this;
while (curNode.level>0) {
// logger.debug(curNode);
sb.insert(0, curNode.getValue());
curNode = curNode.getParent();
}
return sb.toString();
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(" nodeId: " + getNodeId() + ";");
sb.append(" nodevalue: " + getValue() + ";");
sb.append(" node level:" + getLevel() + ";");
sb.append(" child node size: " + getChildNodes().size() + ";");
sb.append(" token value:" + getTokenValue() + ";");
sb.append(" hasEnd: " + hasEnd() + " \n");
return sb.toString();
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (!(obj instanceof DictNode)) {
throw new DartSplitterException("要比较的对象不是DictNode类型!");
}
if (null == obj) {
return false;
}
if (this == obj) {
return true;
}
DictNode compareNode = (DictNode) obj;
if (this.getNodeId() == compareNode.getNodeId()) {
return true;
} else {
return false;
}
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
return new HashCodeBuilder(15, 19).append(getNodeId()).toHashCode();
}
/**
* @return Returns the nodeId.
*/
public int getNodeId() {
return nodeId;
}
/**
* @param nodeId The nodeId to set.
*/
public void setNodeId(int nodeId) {
this.nodeId = nodeId;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -