de.uniba.wiai.lspi.chord.data
Class ID

java.lang.Object
  extended by de.uniba.wiai.lspi.chord.data.ID
All Implemented Interfaces:
java.io.Serializable, java.lang.Comparable<ID>

public final class ID
extends java.lang.Object
implements java.lang.Comparable<ID>, java.io.Serializable

Identifier for nodes and user-defined objects. New instances of this class are created either when a node joins the network, or by the local node inserting a user-defined object. Once created, an ID instance is unmodifiable. IDs of same length can be compared as this class implements java.lang.Comparable. IDs of different length cannot be compared.

Version:
1.0.5
Author:
Sven Kaffille, Karsten Loesing
See Also:
Serialized Form

Constructor Summary
ID(byte[] id1)
          Creates a new ID consisting of the given byte[] array.
 
Method Summary
 ID addPowerOfTwo(int powerOfTwo)
          Calculates the ID which is 2^powerOfTwo bits greater than the current ID modulo the maximum ID and returns it.
 int compareTo(ID otherKey)
          Compare current ID with the given object.
 boolean equals(java.lang.Object equalsTo)
          Checks the given object for equality with this ID.
 int getLength()
          Returns length of this ID measured in bits.
 int hashCode()
           
 boolean isInInterval(ID fromID, ID toID)
          Checks if this ID is in the interval determined by the two given IDs.
 java.lang.String toBinaryString()
          Returns a string of the binary representation of this ID, including leading zeros.
 java.lang.String toBinaryString(int numberOfBytes)
          Returns a string of the binary representation of the first n bytes of this ID, including leading zeros.
 java.lang.String toDecimalString()
          Returns a string of the decimal representation of this ID, including leading zeros.
 java.lang.String toDecimalString(int numberOfBytes)
          Returns a string of the decimal representation of the first n bytes of this ID, including leading zeros.
 java.lang.String toHexString()
          Returns a string of the hexadecimal representation of this ID, including leading zeros.
 java.lang.String toHexString(int numberOfBytes)
          Returns a string of the hexadecimal representation of the first n bytes of this ID, including leading zeros.
 java.lang.String toString()
          Returns a string of the decimal representation of this ID, including leading zeros.
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ID

public ID(byte[] id1)
Creates a new ID consisting of the given byte[] array. The ID is assumed to have (ID.length * 8) bits. It must have leading zeros if its value has fewer digits than its maximum length.

Parameters:
id1 - Byte array containing the ID.
Method Detail

toString

public final java.lang.String toString()
Returns a string of the decimal representation of this ID, including leading zeros.

Overrides:
toString in class java.lang.Object
Returns:
Decimal string of ID

toHexString

public final java.lang.String toHexString(int numberOfBytes)
Returns a string of the hexadecimal representation of the first n bytes of this ID, including leading zeros.

Parameters:
numberOfBytes -
Returns:
Hex string of ID

toHexString

public final java.lang.String toHexString()
Returns a string of the hexadecimal representation of this ID, including leading zeros.

Returns:
Hex string of ID

toDecimalString

public final java.lang.String toDecimalString(int numberOfBytes)
Returns a string of the decimal representation of the first n bytes of this ID, including leading zeros.

Parameters:
numberOfBytes -
Returns:
Hex string of ID

toDecimalString

public final java.lang.String toDecimalString()
Returns a string of the decimal representation of this ID, including leading zeros.

Returns:
Decimal string of ID

toBinaryString

public final java.lang.String toBinaryString(int numberOfBytes)
Returns a string of the binary representation of the first n bytes of this ID, including leading zeros.

Parameters:
numberOfBytes -
Returns:
Hex string of ID

toBinaryString

public final java.lang.String toBinaryString()
Returns a string of the binary representation of this ID, including leading zeros.

Returns:
Binary string of ID

getLength

public final int getLength()
Returns length of this ID measured in bits. ID length is determined by the length of the stored byte[] array, i.e. leading zeros have to be stored in the array.

Returns:
Length of this ID measured in bits.

addPowerOfTwo

public final ID addPowerOfTwo(int powerOfTwo)
Calculates the ID which is 2^powerOfTwo bits greater than the current ID modulo the maximum ID and returns it.

Parameters:
powerOfTwo - Power of two which is added to the current ID. Must be a value of the interval [0, length-1], including both extremes.
Returns:
ID which is 2^powerOfTwo bits greater than the current ID modulo the maximum ID.

equals

public final boolean equals(java.lang.Object equalsTo)
Checks the given object for equality with this ID.

Overrides:
equals in class java.lang.Object
Parameters:
equalsTo - Object to check equality with this ID.

compareTo

public final int compareTo(ID otherKey)
                    throws java.lang.ClassCastException
Compare current ID with the given object. If either the object is not a ID or both IDs' lengths do not match, a ClassCastException is thrown. Otherwise both IDs are compared byte by byte.

Specified by:
compareTo in interface java.lang.Comparable<ID>
Returns:
-1, 0, or 1, if this ID is smaller, same size, or greater than the given object, respectively.
Throws:
java.lang.ClassCastException

hashCode

public final int hashCode()
Overrides:
hashCode in class java.lang.Object
See Also:
Object.hashCode()

isInInterval

public final boolean isInInterval(ID fromID,
                                  ID toID)
Checks if this ID is in the interval determined by the two given IDs. Neither of the boundary IDs is included in the interval. If both IDs match, the interval is assumed to span the whole ID ring.

Parameters:
fromID - Lower bound of interval.
toID - Upper bound of interval.
Returns:
If this key is included in the given interval.