package trees;
import person.MileStonePlace;
import java.io.Serializable;
import java.util.List;
import java.util.Stack;
import java.util.Vector;
import nodes.IntervalNode;
import nodes.MileStone;
import nodes.Node;
/**
* Milestone tree class is an extension for Interval tree, holding milestone
* nodes.
*
* @author Eugene Krapivin
*
*/
public class MileStoneTree extends IntervalTree implements Serializable
{
private static final long serialVersionUID = 6570625767857641226L;
/**
* Class constructor.
* <p>
* Constructor receives a node and sets it as the root
*
* @param node
*/
public MileStoneTree(MileStone node)
{
super(node);
}
@SuppressWarnings("deprecation")
public MileStoneTree(Vector<MileStone> list)
{
if (!list.isEmpty())
{
setRoot(list.elementAt(0));
for (int i = 1; i < list.size(); i++)
{
this.insert(list.elementAt(i));
}
}
}
/**
* Setter method.
* <p>
* this method receives a node and sets it as the root
*/
@Override
public void setRoot(Node root)
{
if (root instanceof MileStone)
{
this.root = (MileStone) root.clone();
}
else
{
throw new ClassCastException(root.getClass()
+ " is an unsutable root for and interval tree.");
}
}
/**
* Copy constructor.
* <p>
* receives a milestone tree and clones it
*
* @param mileStoneTree
* the tree to clone
*/
public MileStoneTree(MileStoneTree mileStoneTree)
{
super(mileStoneTree);
}
/**
* Insertion method.
* <p>
* This method receives all the data needed to create a mile stone after
* creating it, it sends it to the other insert method
*
* @param summary
* The summary of the milestone
* @param place
* The place the milestone took place at
* @param description
* The description of the milestone
* @param begining
* The beginning of the interval
* @param end
* The end of the interval
*/
public void insert(String summary, MileStonePlace place,
String description, Comparable begining, Comparable end)
{
insert(new MileStone(begining, end, place, description));
}
/**
* Insertion method.
* <p>
* This method receives a milestone and inserts it to the tree using the
* insertion method of the interval tree.
*
* @param m
* The milestone to insert into the tree
*/
public void insert(MileStone m)
{
synchronized (this)
{
insert(root, m);
}
}
public void insert(Vector<MileStone> list)
{
synchronized (this)
{
for (MileStone m : list)
{
this.insert(m);
}
}
}
/**
* Search method
* <p>
* This methods searches the tree for overlapping intervals return those
* overlapping interval in a list.
*
* @param low
* The low boundary of the search
* @param high
* The high boundary of the search
*
* @return List of overlapping interval to the search interval
*/
public Vector<MileStone> search(Comparable low, Comparable high)
{
Vector<IntervalNode> result = new Vector<IntervalNode>();
super.search(new IntervalNode(low, high), result);
Vector<MileStone> finalResult = new Vector<MileStone>();
for (IntervalNode i : result)
{
finalResult.add(((MileStone)i).clone());
}
return finalResult;
}
/**
* to string method
*
* @return the string representation of this tree
*/
@Override
public String toString()
{
return super.toString();
}
/**
* This method returns a stack of lists. Each list holds the serialization
* of each node to strings.
*
* @return stack of lists of strings that hold each of the nodes
*/
public Vector<MileStone> returnNodes()
{
MileStone current = (MileStone) this.root;
Stack<MileStone> stack = new Stack<MileStone>();
Vector<MileStone> mileStoneList = new Vector<MileStone>();
while (stack.isEmpty() == false || current != null)
{
if (current != null)
{
stack.push(current);
current = (MileStone) current.getLeft();
}
else
{
current = stack.pop();
mileStoneList.add(current);
current = (MileStone) current.getRight();
}
}
return mileStoneList;
}
}