sg.edu.nus.util
Class PeerMath

java.lang.Object
  extended by sg.edu.nus.util.PeerMath

public class PeerMath
extends java.lang.Object

A utility used for constructing the BATON tree and checking the correctness of the BATON tree.

Version:
1.0 2006-2-6
Author:
Vu Quang Hieu, (Modified by) Xu Linhao

Constructor Summary
PeerMath()
           
 
Method Summary
static java.lang.String addElementToList(java.lang.String element, java.lang.String list, boolean special)
          Returns the string representation of all elements after a new element is added into the existing list separated by an underscore.
static java.lang.String calculateNewPosition(java.lang.String ip, int level, int number, boolean direction)
          Returns the new position of a node.
static boolean checkAncesstor(int number, int level, boolean direction)
          Returns true if a node has an ancestor on the specified side.
static boolean checkNeighbor(int number1, int number2)
          Returns true if two nodes are neighbor.
static boolean checkNode(int level, int number, int leftmostLevel, int leftmostNumber, int rightmostLevel, int rightmostNumber)
          Returns true if a node falls in a subtree specified by its leftmost node and its rightmost node.
static boolean checkNodesSameTree(int leftLevel, int leftNumber, int rightLevel, int rightNumber)
          Returns true if the left node and the right node fall in the leftmost branch and the rightmost branch of the same subtree.
static boolean compareNodePosition(int level1, int number1, int level2, int number2)
          Returns true if the first node is at the left side of the second node.
static java.lang.String findPosition(java.lang.String knowledge, int level, int number, int type)
          Returns the string representation of the IP address of the parent node, the left child node or the right child node from the existing knowledge.
static int getNumberOfExpectedRTReply(boolean isLeftChild, boolean hasSibling, RoutingTableInfo leftRT, RoutingTableInfo rightRT)
          Returns the number of the expected reply message for a new node in the course of constructing its routing table.
static int getPowerDistance(int number1, int number2)
          Returns the power distance between two nodes that lies in the same level of the tree.
static int getRoutingTableSize(int number, int level, boolean direction)
          Returns the size of the routing table of a peer that is specified by the position and level of the BATON tree.
static boolean isLowerCase(char c)
          Returns true if a character is a lower case.
static boolean isNumeric(char c)
          Returns true if a character is a numeric value.
static boolean isOverlapped(int min1, int max1, int min2, int max2)
          Returns true if two ranges are overlapped.
static boolean isUpperCase(char c)
          Returns true if a character is a upper case.
static int pow(int x, int p)
          Calculate the power of two integer numbers.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PeerMath

public PeerMath()
Method Detail

pow

public static int pow(int x,
                      int p)
Calculate the power of two integer numbers.

Parameters:
x - the base number
p - the power number
Returns:
the result of the power of two integer number

isUpperCase

public static boolean isUpperCase(char c)
Returns true if a character is a upper case.

Parameters:
c - the character to be checked
Returns:
if the character is a upper case, return true; otherwise, return false

isLowerCase

public static boolean isLowerCase(char c)
Returns true if a character is a lower case.

Parameters:
c - the character to be checked
Returns:
if the character is a lower case, return true; otherwise, return false

isNumeric

public static boolean isNumeric(char c)
Returns true if a character is a numeric value.

Parameters:
c - the character to be checked
Returns:
if the character is a numeric value, return true; otherwise, return false

getRoutingTableSize

public static int getRoutingTableSize(int number,
                                      int level,
                                      boolean direction)
Returns the size of the routing table of a peer that is specified by the position and level of the BATON tree.

Parameters:
number - the position of the node in the specified level of the tree
level - the level of the node in the tree
direction - true for calculating the size of the right routing table; false for calculating the size of the left routing table
Returns:
the size of the routing table

getPowerDistance

public static int getPowerDistance(int number1,
                                   int number2)
Returns the power distance between two nodes that lies in the same level of the tree.

Parameters:
number1 - the number of the first node
number2 - the number of the second node
Returns:
the power distance between two nodes

isOverlapped

public static boolean isOverlapped(int min1,
                                   int max1,
                                   int min2,
                                   int max2)
Returns true if two ranges are overlapped.

Parameters:
min1 - the minimum value of the first range
max1 - the maximum value of the first range
min2 - the minimum value of the second range
max2 - the maximum value of the second range
Returns:
true if two ranges are overlapped; otherwise, return false

checkAncesstor

public static boolean checkAncesstor(int number,
                                     int level,
                                     boolean direction)
Returns true if a node has an ancestor on the specified side.

Parameters:
number - the number of the node in the level of the tree
level - the level of the node in the tree
direction - true indicates the right hand side; otherwise indicates the left hand side
Returns:
true if the ancestor of the node is found; otherwise, return false

checkNode

public static boolean checkNode(int level,
                                int number,
                                int leftmostLevel,
                                int leftmostNumber,
                                int rightmostLevel,
                                int rightmostNumber)
Returns true if a node falls in a subtree specified by its leftmost node and its rightmost node.

Parameters:
level - the level of the node to be checked
number - the number of the node to be checked
leftmostLevel - the level of the leftmost node
leftmostNumber - the number of the leftmost node
rightmostLevel - the level of the rightmost node
rightmostNumber - the number of the rightmost node
Returns:
if the node falls in the subtree, return true; otherwise, return false

calculateNewPosition

public static java.lang.String calculateNewPosition(java.lang.String ip,
                                                    int level,
                                                    int number,
                                                    boolean direction)
Returns the new position of a node.

Parameters:
ip - the IP address of the node
level - the level of the node
number - the number of the node
direction - true if the node is shifted to the left; otherwise, if the node is shifted to the right
Returns:
the new position of the node with the format "Level&Number&IPAddress"

findPosition

public static java.lang.String findPosition(java.lang.String knowledge,
                                            int level,
                                            int number,
                                            int type)
Returns the string representation of the IP address of the parent node, the left child node or the right child node from the existing knowledge.

Parameters:
knowledge - the existing knowledge of the position and IP address of all nodes
level - the level of the node
number - the number of the node
type - 1 indicates the IP address of the parent node is found; 2 indicates the IP address of the left child node is found; 3 indicates the IP address of the right child node is found
Returns:
if found, return the IP address; otherwise, return "-1"

addElementToList

public static java.lang.String addElementToList(java.lang.String element,
                                                java.lang.String list,
                                                boolean special)
Returns the string representation of all elements after a new element is added into the existing list separated by an underscore.

Parameters:
element - the element to be added
list - the existing list
special - false if the new element is added to first position; otherwise, it is added to last position
Returns:
the string representation of all elements after a new element is added into the existing list separated by an underscore

checkNodesSameTree

public static boolean checkNodesSameTree(int leftLevel,
                                         int leftNumber,
                                         int rightLevel,
                                         int rightNumber)
Returns true if the left node and the right node fall in the leftmost branch and the rightmost branch of the same subtree.

Parameters:
leftLevel - the level of the left node
leftNumber - the number of the left node
rightLevel - the level of the right node
rightNumber - the number of the right node
Returns:
if in the same subtree, return true; otherwise, return false

compareNodePosition

public static boolean compareNodePosition(int level1,
                                          int number1,
                                          int level2,
                                          int number2)
Returns true if the first node is at the left side of the second node.

Parameters:
level1 - the level of the first node
number1 - the number of the first node
level2 - the level of the second node
number2 - the number of the second node
Returns:
if the first node is at the left side of the second node, return true; otherwise, return false

getNumberOfExpectedRTReply

public static int getNumberOfExpectedRTReply(boolean isLeftChild,
                                             boolean hasSibling,
                                             RoutingTableInfo leftRT,
                                             RoutingTableInfo rightRT)
Returns the number of the expected reply message for a new node in the course of constructing its routing table.

Parameters:
isLeftChild - true indicates the new node is the left child; otherwise indicates the right child
hasSibling - true indicates the new node has a sibling; otherwise indicates none
leftRT - the left routing table of the new node
rightRT - the right routing table of the new node
Returns:
the number of the expected reply message for a new node in the course of constructing its routing table

checkNeighbor

public static boolean checkNeighbor(int number1,
                                    int number2)
Returns true if two nodes are neighbor.

Parameters:
number1 - the number of the first node
number2 - the number of the second node
Returns:
if they are neighbor, return true; otherwise, return false