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

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

public final class ID
extends Object
implements Comparable<ID>, 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.

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(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.
 String toBinaryString()
          Returns a string of the binary representation of this ID, including leading zeros.
 String toBinaryString(int numberOfBytes)
          Returns a string of the binary representation of the first n bytes of this ID, including leading zeros.
 String toDecimalString()
          Returns a string of the decimal representation of this ID, including leading zeros.
 String toDecimalString(int numberOfBytes)
          Returns a string of the decimal representation of the first n bytes of this ID, including leading zeros.
 String toHexString()
          Returns a string of the hexadecimal representation of this ID, including leading zeros.
 String toHexString(int numberOfBytes)
          Returns a string of the hexadecimal representation of the first n bytes of this ID, including leading zeros.
 String toString()
          Returns a string of the decimal representation of this ID, including leading zeros.
 
Methods inherited from class java.lang.Object
clone, finalize, 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 String toString()
Returns a string of the decimal representation of this ID, including leading zeros.

Overrides:
toString in class Object
Returns:
Decimal string of ID

toHexString

public final 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 String toHexString()
Returns a string of the hexadecimal representation of this ID, including leading zeros.

Returns:
Hex string of ID

toDecimalString

public final 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 String toDecimalString()
Returns a string of the decimal representation of this ID, including leading zeros.

Returns:
Decimal string of ID

toBinaryString

public final 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 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(Object equalsTo)
Checks the given object for equality with this ID.

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

compareTo

public final int compareTo(ID otherKey)
                    throws 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 Comparable<ID>
Returns:
-1, 0, or 1, if this ID is smaller, same size, or greater than the given object, respectively.
Throws:
ClassCastException

hashCode

public final int hashCode()
Overrides:
hashCode in class 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.


Copyright © 2004-2008 Karsten Loesing, Sven Kaffille - Distributed and Mobile Systems Group, Lehrstuhl für Praktische Informatik, Universität Bamberg. All Rights Reserved.