All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.onetwo.common.tree.AbstractTreeModel Maven / Gradle / Ivy

There is a newer version: 4.7.2
Show newest version
package org.onetwo.common.tree;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.onetwo.common.utils.Assert;

@SuppressWarnings({ "unchecked", "serial" })
@XmlType(name="TreeModel")
@XmlAccessorType(XmlAccessType.FIELD)
abstract public class AbstractTreeModel> implements Serializable, TreeModel {
 
	protected Object id;

	protected String name;

	protected List children = new ArrayList();

	protected Object parentId;

	protected T parent;
	
	protected Comparable sort;
	
	protected int level = -1;
	protected int index;
	
	public AbstractTreeModel(){
	}

	public AbstractTreeModel(Object id, String name) {
		this(id, name, null);
	}

	public AbstractTreeModel(Object id, String name, Object parentId) {
		Assert.notNull(id, "id must not be null!");
		this.id = id;
		this.name = name;
		this.parentId = parentId;
		if(id instanceof Comparable)
			this.sort = (Comparable) id;
	}

	public AbstractTreeModel(Object id, String name, Object parentId, Comparable sort) {
		Assert.notNull(id, "id must not be null!");
		this.id = id;
		this.name = name;
		this.parentId = parentId;
		this.sort = sort;
	}

	public List getChildren() {
		return children;
	}

	public void setChildren(List children) {
		if(children==null)
			return ;
		for(T c : children){
			this.addChild(c);
		}
	}

	@Override
	public void addChild(T node) {
		node.setParent((T) this);
		node.setIndex(this.children.size());
		this.children.add(node);
	}
	
	public boolean isFirst(){
		return this.index==0;
	}
	
	public boolean isLast(){
		if(this.getParent()==null)
			return true;
		else
			return (this.index+1) == this.getParent().getChildren().size();
	}
	
	public T getChild(Object id){
		if(this.isLeafage())
			return null;
		for(T node : (List)this.children){
			if(node.getId().equals(id))
				return node;
		}
		return null;
	}
	
	public T getNodeById(Object id){
		if(this.getId().toString().equals(id.toString()))
			return (T) this;
		
		if(this.isLeafage())
			return null;

		T n = null;
		for(T node : (List)this.children){
			n = node.getNodeById(id);
			if(n!=null)
				return n;
		}
		return null;
	}

	@Override
	public Object getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public Comparable getSort() {
		return (Comparable) sort;
	}

	public Object getParentId() {
		return parentId;
	}

	public boolean isLeafage() {
		return this.children.isEmpty();
	}

	public T getParent() {
		return parent;
	}
	
	public void setSort(Comparable sort){
		this.sort = sort;
	}

	public void setParent(T parent) {
		this.parent = parent;
		if(parent!=null){
			this.parentId = parent.getId();
		}
	}
	
	public boolean hasChildren(){
		return this.children!=null && !this.children.isEmpty();
	}

	@Override
	public boolean equals(Object o) {
		if(this==o)
			return true;
		if(!(o instanceof AbstractTreeModel))
			return false;
		AbstractTreeModel obj = (AbstractTreeModel) o;
		return new EqualsBuilder().append(this.getId(), obj.getId()).isEquals();
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(id).toHashCode();
	}
	
	
	public int getLevel() {
		if(this.level!=-1)
			return this.level;
		int lv = 1;
		if(this.getParent()!=null)
			lv += this.getParent().getLevel();
		this.level = lv;
		return level;
	}
	
	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public String toString(){
		StringBuilder sb = new StringBuilder();
		for(int i=0; i").append(getId()).append("|").append(getName()).append("\n");
		if(this.children==null || this.children.isEmpty())
			return sb.toString();
		for(T child : (List)this.children){
			sb.append(child.toString());
		}
		return sb.toString();
	}
	
	public List toList(){
		List list = new ArrayList();
		list.add((T) this);
		if(this.children==null)
			return list;
		for(T tm : this.children){
			list.addAll(tm.toList());
		}
		return list;
	}
	
}