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

com.github.easydoc.model.DocTree Maven / Gradle / Ivy

package com.github.easydoc.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import com.github.easydoc.model.criteria.DocSearchCriteria;

public class DocTree implements Serializable {
	private static final long serialVersionUID = 1L;
	
	private List roots = new LinkedList();
	
	public void addRoots(Collection docs) {
		roots.addAll(docs);
	}
	
	public void addRoot(Doc doc) {
		roots.add(doc);
	}
	
	public Collection getRoots() {
		return roots;
	}
	
	public void removeRoot(Doc doc) {
		roots.remove(doc);
	}
	
	public List find(DocSearchCriteria criteria) {
		return find(roots, criteria);
	}

	private List find(List docs, DocSearchCriteria criteria) {
		List ret = new ArrayList();
		for(Doc doc : docs) {
			if(criteria.satisfies(doc)) {
				ret.add(doc);
			}
			ret.addAll(find(doc.getChildren(), criteria));
		}
		return ret;
	}
	
	public boolean isEmpty() {
		return roots.isEmpty();
	}

	@Override
	public String toString() {
		return String.format("DocTree [\n%s\n]", printTree(new StringBuilder(), "", roots).toString());
	}
	
	private StringBuilder printTree(StringBuilder sb, String indent, List docs) {
		for(Doc doc : docs) {
			sb.append(indent).append(doc.toShortString()).append("\n");
			sb = printTree(sb, indent + "\t", doc.getChildren());
		}
		
		return sb;
	}

	public void sort() {
		sort(roots);
	}
	
	private void sort(List docs) {
		Collections.sort(docs);
		for(Doc doc : docs) {
			sort(doc.getChildren());
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy