?? node.java
字號:
package com.kewf.dirtree;
import java.util.*;
/**
* 目錄樹的一個結點. <br/>
* 它的主要屬性有結點標識、父結點、它在目錄樹中的層次(根結點為0)、結點的值、子結點。
* <p>Copyright: Copyright (c) 2004</p>
* @author flyxxxxx
* @version 1.0
*/
public class Node {
private int id;
private Node parent;
private int level;
private Object value;
protected ArrayList childs = new ArrayList();
/**
* 構造方法
* @param id int 結點ID
* @param parent Node 父結點
*/
Node(int id, Node parent) {
this.id = id;
if (parent != null) {
this.parent = parent;
parent.childs.add(this);
this.level = parent.getLevel() + 1;
}
else {
level = 0;
}
}
/**
* 得到結點ID.
* @return int 結點ID
*/
public int getId() {
return id;
}
/**
* 得到結點在目錄樹中的層次.
* 其中根結點為0,根結點的子結點為1,依次類推
* @return int 結點在目錄樹中的層次
*/
final public int getLevel() {
return level;
}
/**
* 得到結點的值.
* 也就是TreeNode接口所引用的對象
* @return Object 結點的值
*/
final public Object getValue() {
return value;
}
/**
* 設定結點的值.
*/
final void setValue(Object value) {
this.value = value;
}
/**
* 得到子結點列表.
* Iterator中存儲的是Node對象
* @return Iterator 子結點列表
*/
final public Iterator getChilds() {
return childs.iterator();
}
/**
* 得到子結點數量.
* @return int 子結點數量
*/
final public int getChildsNumber() {
return childs.size();
}
/**
* 是否有子結點.
* @return boolean 有子結點返回true
*/
final public boolean hasChilds() {
return childs.size() > 0;
}
/**
* 得到父結點.
* 如果結點為根結點,返回null
* @return Node 父結點
*/
final public Node getParent() {
return parent;
}
/**
* 得到第level級父結點.
* @param level int 父結點的層次(level大于等于0,小于此結點的層次)
* @return Node 第level級父結點
*/
final public Node getParent(int level) {
if (level < 0 || level >= this.level) {
throw new ArrayIndexOutOfBoundsException("level is error.");
}
Node n = parent;
for (int i = 1; i < level; i++) {
n = n.getParent();
}
return n;
}
/**
* 得到結點在同級結點的相對位置.
* @return int 結點在同級結點的相對位置
*/
final public int getPosition() {
if (parent == null) {
return 0;
}
return parent.childs.indexOf(this);
}
/**
* 結點是否是同級結點的最后一個.
* @return boolean 是返回true
*/
final public boolean isLast() {
if (parent == null) {
return true;
}
return getPosition() == parent.childs.size() - 1;
}
/**
* 結點是否同級結點的第一個.
* @return boolean 是返回true
*/
final public boolean isFirst() {
return getPosition() == 0;
}
/**
* 得到目錄樹中下一個結點.
* 如果此結點是目錄樹最后一個結點則返回null
* @return Node 下一個結點
*/
final public Node getNext() {
if (childs.size() > 0) {
return (Node) childs.get(0);
}
Node n = parent;
while (n != null) {
Node node = n.getNextSibling();
if (node != null) {
return node;
}
n = n.getParent();
}
return null;
}
/**
* 得到下一個同級結點.
* 沒有下一個同級結點返回null
* @return Node 下一個同級結點
*/
final public Node getNextSibling() {
if (parent == null) {
return null;
}
int k = getPosition();
if (k == parent.getChildsNumber() - 1) {
return null;
}
return (Node) parent.childs.get(k + 1);
}
/**
* 得到前一個同級結點.
* 沒有前一個同級結點返回null
* @return Node 前一個同級結點
*/
final public Node getPreviousSibling() {
int k = getPosition();
if (k == 0) {
return null;
}
return (Node) parent.childs.get(k - 1);
}
/**
* 得到前一個結點.
* 根結點的前一個結點為null
* @return Node 前一個結點
*/
final public Node getPrevious() {
Node n = getPreviousSibling();
if (n != null) {
return n;
}
return parent;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -