Package nodes

Source Code of nodes.IntervalNode

/* ************************
* Name : Eugene Krapivin *
* ID   : 306255084       *
* ***********************/

package nodes;

import java.io.Serializable;

import exceptions.InvalidIntervalException;

/**
* Class IntervalNode represents a node for an Interval Tree ADT.
* <p>
* This class extends the class Node and uses its value as a key while adding
* another field that represents the interval.
* <p>
* Just as the Node and the Interval, values (key and interval) are immutable.
*
* @see Node
* @author Eugene Krapivin
* @version 1.0 27/04/12
*/
public class IntervalNode extends Node implements Serializable
{
  /**
   *
   */
  private static final long serialVersionUID = -1860999006645368497L;
  private Comparable high;
  private Comparable max;

  /**
   * Class constructor
   * <p>
   * This is a default constructor for this class. Objects build by this
   * constructor will have default values of [0,0] key = 0.
   * <p>
   * Warning: Since the values of the node are immutable its recommended not
   * to use this constructor
   */
  public IntervalNode()
  {
    super(0);
    high = 0;
    max = 0;
  }

  /**
   * Class constructor
   * <p>
   * This constructor gets 2 values to initiate the interval with (the key's
   * initiation is dependent on those values). The constructor of the interval
   * checks if it is possible to create an interval using those values
   *
   * @param low
   *            Minimal boundary for the root interval
   * @param high
   *            Maximal boundary for the root interval
   * @throws InvalidIntervalException
   */
  public IntervalNode(Comparable low, Comparable high)
      throws InvalidIntervalException
  {
    if (low.compareTo(high) == 1)
    {
      throw new InvalidIntervalException("Values " + low.toString()
          + " and " + high.toString()
          + " can not compose an interval.");
    }
    super.setData(low);
    this.high = high;
    this.max = high;
  }

  /**
   * Class constructor
   * <p>
   * This is a copy constructor for the class.
   *
   * @param intervalNode
   *            The node to copy from
   */
  public IntervalNode(IntervalNode intervalNode)
  {
    super(intervalNode.data);

    this.high = intervalNode.high;
    this.max = intervalNode.max;
  }

  /**
   * Getter method for the max value of the interval
   *
   * @return The maximal value of the interval
   */
  public Comparable getMax()
  {
    return max;
  }

  /**
   * Getter method for the high-end of the interval
   * <p>
   *
   * @return The high end of the interval
   */
  public Comparable getHigh()
  {
    return high;
  }

  /**
   * Getter method for the low-end of the interval
   * <p>
   *
   * @return The low end of the interval
   */
  public Comparable getLow()
  {
    return data;
  }

  /**
   * Setter method for the high-end of the interval
   * <p>
   *
   * @param high
   *            The high end of the interval
   */
  public void setHigh(Comparable high)
  {
    this.high = high;
  }

  /**
   * Setter method for the low-end of the interval
   * <p>
   *
   * @param low
   *            The low end of the interval
   */
  public void setLow(Comparable low)
  {
    setData(low);
  }

  /**
   * Setter method for the max of the interval
   * <p>
   *
   * @param max
   *            The max of high ends in sub trees
   */
  public void setMax(Comparable max)
  {
    this.max = max;
  }

  /**
   * Checks if some IntervalNode has an overlapping interval with this node
   *
   * @param other
   * @return true if there is an overlapping, otherwise false.
   */
  public boolean overlapsWith(IntervalNode other)
  {
    return getLow().compareTo(other.getHigh()) <= 0
        && getHigh().compareTo(other.getLow()) >= 0;
  }

  /**
   * Comparator method needed be interface Comparable.
   * <p>
   * The comparison between 2 IntervalNodes (not intervals) is based on the
   * minimal values of each interval. Using this method it is possible to
   * build a Binary search tree that allows duplicate value.
   *
   * @param o
   *            Object to compare with.
   * @return 1 if this minimal value is bigger then the other, -1 if the
   *         others minimal value is bigger, otherwise 0.
   */
  @Override
  public int compareTo(Object o)
  {
    if (o instanceof IntervalNode)
    {
      return getLow().compareTo(((IntervalNode) o).getLow());
    }
    else
    {
      throw new ClassCastException("Can't compare class "
          + this.getClass() + " to class " + o.getClass() + ".");
    }
  }

  /**
   * Method to check if 2 nodes are equal
   *
   * @param o
   *            Object to compare with
   * @return true if objects are identical, false otherwise
   */
  public boolean equals(Object o)
  {
    if (o instanceof IntervalNode)
    {
      return getLow() == ((IntervalNode) o).getLow()
          && getMax() == ((IntervalNode) o).getMax();
    }
    else
    {
      throw new ClassCastException("Can't cast class " + this.getClass()
          + " to class " + o.getClass() + ".");
    }
  }

  /**
   * Cloning method.
   * <p>
   * This method create a deep clone of this node and its values, including
   * sub-trees and their values.
   *
   * @return Identical tree
   */
  public IntervalNode clone()
  {
    return new IntervalNode(this);
  }

  /**
   * To String method returns a string describing the node.
   *
   * @return A string describing the node
   */
  public String toString()
  {
    return "[" + getLow() + ", " + getHigh() + "]" + " " + "Max: "
        + getMax();
  }
}
TOP

Related Classes of nodes.IntervalNode

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.