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

nz.co.gregs.dbvolution.query.TreeNode Maven / Gradle / Ivy

/*
 * Copyright 2014 gregory.graham.
 *
 * 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.
 */
package nz.co.gregs.dbvolution.query;

import java.util.ArrayList;
import java.util.List;
import nz.co.gregs.dbvolution.DBRecursiveQuery;
import nz.co.gregs.dbvolution.DBRow;
import nz.co.gregs.dbvolution.datatypes.QueryableDatatype;

/**
 * Encapsulates a DBRow class for use in {@link DBRecursiveQuery} tree or path.
 *
 * 

* Use {@link #getData() } to retrieve the DBRow contain in the node, {@link #getParent() * } to move up the hierarchy, and {@link #getChildren() } to move down it. * *

Support DBvolution at * Patreon

* * @author Gregory Graham * @param The DBRow class */ public class TreeNode { private final List> children = new ArrayList>(); private final List childrenKeys = new ArrayList(); private TreeNode parent = null; private T data = null; /** * Create a new node for the DBRow * * @param data */ public TreeNode(T data) { this.data = data; } /** * Create a new node the contains the specified DBRow as the data, and the * specified TreeNode as the parent of the node. * * @param data * @param parent */ public TreeNode(T data, TreeNode parent) { this.data = data; this.parent = parent; } /** * Returns a list of all known children of this node, that is all database * rows returned by the recursive query that referenced this row. * *

Support DBvolution at * Patreon

* * @return a list of TreeNodes */ public List> getChildren() { return children; } private void setParent(TreeNode parent) { this.parent = parent; } /** * Append the supplied DBRow to the list of children for this node. * *

* Also adds this node as the parent of the supplied node. * * @param childData */ public void addChild(T childData) { TreeNode child = new TreeNode(childData); this.addChild(child); // child.setParent(this); // if (notAlreadyIncluded(child)) { // this.children.add(child); // this.childrenData.add(data.getPrimaryKeys().stringValue()); // } } /** * Append the supplied DBRow to the list of children for this node. * *

* Also adds this node as the parent of the supplied node. * * @param child */ public void addChild(TreeNode child) { child.setParent(this); if (notAlreadyIncluded(child)) { this.children.add(child); this.childrenKeys.add(child.getKey()); } } private boolean notAlreadyIncluded(TreeNode child) { return !this.children.contains(child) && !childrenKeys.contains(child.getKey()); } /** * Retrieves the DBRow within this node. * *

Support DBvolution at * Patreon

* * @return a DBRow */ public T getData() { return this.data; } /** * Set the DBRow within this node. * * @param data */ public void setData(T data) { this.data = data; } /** * Indicates whether or not this node is a root node, that is it has no known * parent. * *

Support DBvolution at * Patreon

* * @return TRUE if this node is the top of a hierarchy. */ public boolean isRoot() { return (this.parent == null); } /** * Indicates whether or not this node is a leaf node, that is it has no known * children. * *

Support DBvolution at * Patreon

* * @return TRUE if this node is the bottom of a hierarchy. */ public boolean isLeaf() { return this.children.isEmpty(); } /** * Remove the link to this node's parent, if any. * */ public void removeParent() { this.parent = null; } /** * Retrieves the node that is the parent of this node. * *

Support DBvolution at * Patreon

* * @return the TreeNode immediately above this node in the hierarchy */ public TreeNode getParent() { return this.parent; } @Override public String toString() { return this.getData().toString(); } private String getKey() { StringBuilder returnString = new StringBuilder(""); final List> pks = this.getData().getPrimaryKeys(); for (QueryableDatatype pk : pks) { returnString.append("&&").append(pk.stringValue()); } return returnString.toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy