v1.0.0 API Download GitHub Donate
HappyTree API

Plant your tree of Java Objects.

HappyTree API

"Java Object Model API to transform linear objects with tree behavior into real trees. Handle these objects as if they were nodes of a tree."

HappyTree is a data structure API designed for the Java programming language that consists of transforming linear objects into a tree structure, where each object represents a node in the tree and can have none or several other objects of the same type.

Consists of an extremely simple API, focused on the Java Object Model layer of a Java project architecture.

In addition to that, the HappyTree API provides interfaces to handle these objects within trees, as if these objects were tree nodes, all in an easy and intuitive way.

Handle Java Objects as if they were nodes within trees, in order to perform operations such as copying, cutting, removing, creating, persisting or updating;

Transform linear data structures of Java Objects into trees;

Create and manage multiple trees of these objects.

Quick Start

		
/**
 * 1. Annotate your source object that will be
 * transformed into a node in the tree.
 */
@Tree
public class Menu {
	@Id
	private Integer menuId;
	private String menuLabel;
	@Parent
	private Integer menuParentId;
	private String menuDescription;

	public Menu() {
	}

	public Integer getMenuId() {
		return menuId;
	}

	public void setMenuId(Integer menuId) {
		this.menuId = menuId;
	}

	public Integer getMenuParentId() {
		return menuParentId;
	}

	public void setMenuParentId(Integer menuParentId) {
		this.menuParentId = menuParentId;
	}
	//Other getters & setters ...
}
		
		
		
/**
 * 2. Initialize a session passing through a list
 * of the source objects to be transformed.
 */
TreeManager manager = HappyTree.createTreeManager();
TreeTransaction transaction = manager.getTransaction();

Collection<Menu> menus = myObject.getMenuFromDatabase();
		
transaction.initializeSession("MyFirstHappyTree", menus);
		
		
		
/**
 * 3. As a result, the list of Menu objects, which had
 * logical tree behavior, but was structurally arranged
 * in a linear form, now represents a node in a real tree,
 * encapsulated in the form of Element object.
 */
public class Element<Menu> {
	private Object menuId;
	private Object menuParentId;
	
	private Collection<Element<Menu>> menuChildren;
	private Menu wrappedMenu;
	
	//Skeleton methods.
	public Object getId();

	public void setId(Object menuId);

	public Object getParent();

	public void setParent(Object menuParentId);

	public Collection<Element<Menu>> getChildren();

	public Element<Menu> getElementById(Object menuId);
	
	public void addChild(Element<Menu> child);

	public void removeChild(Element<Menu> child);

	public void removeChild(Object menuId);

	public void wrap(Menu menu);

	public Menu unwrap();
}
		
		
		
/**
 * 4. With the elements already generated and the
 * tree built, it is now possible to freely handle
 * each element within the tree.
 */
transaction.initializeSession("MyFirstHappyTree", menus);
		
Element<Menu> administration = manager.getElementById(
		administrationId);
Element<Menu> accessControl = manager.getElementById(
		accessControlId);

/*
 * Access Control menu and its children now will
 * be moved for Administration menu.
 */
manager.cut(accessControl, administration);
		
Menu securityMenu = new Menu();
securityMenu.setMenuId(securityId);
securityMenu.setMenuParentId(administrationId);
		
Element<Menu> security = manager.createElement(securityId,
		administrationId, securityMenu);

//New inserted element in the tree.		
manager.persistElement(security);
		
administration = manager.getElementById(administrationId);
administration.removeChild(configurationId);

/*
 * Updating the Administration menu by removing
 * a child.
 */
manager.updateElement(administration);