planet.chord
Class ChordId

java.lang.Object
  extended byplanet.commonapi.Id
      extended byplanet.chord.ChordId
All Implemented Interfaces:
java.lang.Comparable, java.io.Serializable

public class ChordId
extends Id
implements java.io.Serializable

Spedific Chord Id implementation.

Author:
Jordi Pujol 25/02/2005
See Also:
Serialized Form

Field Summary
protected static int bitsKey
          Sets the number of bits used in last building of MAX.
protected  int bitsPerKey
          The number of bits per key as is shown in the current configuration.
protected static Id MAX
          Maximum value for the ChordId implementation.
protected static int MAX_ARRAY_SIZE
          Chord specific constant: Maximum size of array that contains entire identification.
static int MAX_BITS
          Chord specific constant: Maximum number of bits for any Id.
static int MIN_BITS
          Chord specific constant: Number of bits saved at any array position.
protected static int MODULE
          Chord specific constant: Each array position contains a maximum representation of MODULE bits.
static int[] ONE_CHORD
          Chord specific constant: The value of one (1) of internal value of Id.
static int[] TWO_CHORD
          Chord specific constant: The value of two (2) of internal value of Id.
 
Constructor Summary
ChordId()
          Generates an Id initialized to zero.
 
Method Summary
 Id add(Id offset)
          Returns an Id corresponding to this Id plus a given offset
 boolean between(Id ccw, Id cw)
          Checks if this Id is between two given ids ccw (inclusive) and cw (exclusive) on the circle
 void blit(byte[] target)
           
 boolean clockwise(Id nid)
          Checks to see if the Id nid is clockwise or counterclockwise from this, on the ring.
 int compareTo(java.lang.Object obj)
          Comparison operator for Ids.
 byte[] copy()
           
static Id divide(int numberOfNodes)
          Divides the maximum domain of the node Id in numberOfNodes, to offers the offset between two consecutive nodes.
 Id divideOn(int divisor)
          This method returns the arithmetical result of this division:
thisId/divisor
 boolean equals(java.lang.Object obj)
           
 int getDigit(int i, int b)
          Gets the ith digit in base 2^b. i = 0 is the least significant digit.
static Id getMaximum()
          This method returns the maximum value for a ChordId that is possible to build, according the number of bits for key actually in use.
 java.lang.Object getValue()
          Return the int[] with the value of this Id.
 int hashCode()
          Hash codes for Ids.
protected  void parseBigInteger(java.math.BigInteger value)
          Translate the BigInteger value to an Id, partitioning it to fragments of 32 bits.
 void setValue(java.lang.Object newValue)
          Sets the new value with the int[] that appears at newValue.
 Id setValues(java.math.BigInteger newValue)
          Sets the internal int[] value from the BigInteger newValue.
 Id setValues(byte[] newValue)
          Sets the internal int[] value from the byte[] newValue.
 Id setValues(double newValue)
          Always throws a NoSuchMethodError.
 Id setValues(int newValue)
          Sets the newValue to the first position of the internal int[], and the rest of positions to zero.
 Id setValues(int[] newValue)
          Sets the internal value from the int[] newValue
 Id setValues(java.util.Random valueGenerator)
          Uses the valueGenerator to radomly build a new value.
 Id setValues(java.lang.String newValue)
          Copies the string representation of a number to the internal int[].
 Id shift(int cnt, int fill)
          Shift operator. shift(-1,0) multiplies value of this by two, shift(1,0) divides by 2
 Id shift(int cnt, int fill, boolean roundUp)
          Shift operator. shift(-1,0) multiplies value of this by two, shift(1,0) divides by 2
 Id subtract(Id offset)
          Returns an Id corresponding to this Id minus a given distance
 Id subtractWithCarry(Id offset)
           
 byte[] toByteArray()
          Returns the byte array representation of this Id
 java.lang.String toString()
          Returns a string representation of the Id in base 16.
 java.lang.String toStringFull()
          Returns a string representing the full length of this Id.
 java.lang.String toStringHexFull()
          Returns the complete represntation of this Id, in hex.
 
Methods inherited from class planet.commonapi.Id
betweenE, Ebetween, setValue, setValues
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

MAX_BITS

public static final int MAX_BITS
Chord specific constant: Maximum number of bits for any Id.

See Also:
Constant Field Values

MIN_BITS

public static final int MIN_BITS
Chord specific constant: Number of bits saved at any array position.

See Also:
Constant Field Values

ONE_CHORD

public static final int[] ONE_CHORD
Chord specific constant: The value of one (1) of internal value of Id.


TWO_CHORD

public static final int[] TWO_CHORD
Chord specific constant: The value of two (2) of internal value of Id.


MODULE

protected static final int MODULE
Chord specific constant: Each array position contains a maximum representation of MODULE bits.

See Also:
Constant Field Values

MAX_ARRAY_SIZE

protected static final int MAX_ARRAY_SIZE
Chord specific constant: Maximum size of array that contains entire identification.

See Also:
Constant Field Values

bitsKey

protected static int bitsKey
Sets the number of bits used in last building of MAX.


MAX

protected static Id MAX
Maximum value for the ChordId implementation.


bitsPerKey

protected int bitsPerKey
The number of bits per key as is shown in the current configuration.

Constructor Detail

ChordId

public ChordId()
Generates an Id initialized to zero. It is not required for the framework.

Method Detail

parseBigInteger

protected void parseBigInteger(java.math.BigInteger value)
Translate the BigInteger value to an Id, partitioning it to fragments of 32 bits.


add

public Id add(Id offset)
Returns an Id corresponding to this Id plus a given offset

Specified by:
add in class Id
Parameters:
offset - the offset to add
Returns:
the new Id

subtractWithCarry

public Id subtractWithCarry(Id offset)

subtract

public Id subtract(Id offset)
Description copied from class: Id
Returns an Id corresponding to this Id minus a given distance

Specified by:
subtract in class Id
Parameters:
offset - the distance to subtract
Returns:
the new Id

equals

public boolean equals(java.lang.Object obj)

blit

public void blit(byte[] target)

copy

public byte[] copy()

compareTo

public int compareTo(java.lang.Object obj)
Comparison operator for Ids. The comparison that occurs is a numerical comparison.

Specified by:
compareTo in interface java.lang.Comparable
Parameters:
obj - the Id to compare with.
Returns:
negative if this < obj, 0 if they are equal and positive if this > obj.

toByteArray

public byte[] toByteArray()
Returns the byte array representation of this Id

Specified by:
toByteArray in class Id
Returns:
The byte array representation of this id

hashCode

public int hashCode()
Hash codes for Ids.

Returns:
a hash code.

toStringHexFull

public java.lang.String toStringHexFull()
Returns the complete represntation of this Id, in hex.

Returns:
The complete representation of this Id, in hexadecimal

toStringFull

public java.lang.String toStringFull()
Description copied from class: Id
Returns a string representing the full length of this Id.

Specified by:
toStringFull in class Id
Returns:
A string with all of this Id

toString

public java.lang.String toString()
Returns a string representation of the Id in base 16. The string is a byte string from most to least significant.

Returns:
A String representation of this Id, abbreviated

getDigit

public int getDigit(int i,
                    int b)
Gets the ith digit in base 2^b. i = 0 is the least significant digit.

Parameters:
i - which digit to get.
b - which power of 2 is the base to get it in.
Returns:
the ith digit in base 2^b.

shift

public Id shift(int cnt,
                int fill)
Shift operator. shift(-1,0) multiplies value of this by two, shift(1,0) divides by 2

Specified by:
shift in class Id
Parameters:
cnt - the number of bits to shift, negative shifts left, positive shifts right
fill - value of bit shifted in (0 if fill == 0, 1 otherwise)
Returns:
this

shift

public Id shift(int cnt,
                int fill,
                boolean roundUp)
Shift operator. shift(-1,0) multiplies value of this by two, shift(1,0) divides by 2

Parameters:
cnt - the number of bits to shift, negative shifts left, positive shifts right
fill - value of bit shifted in (0 if fill == 0, 1 otherwise)
roundUp - if true, round up the results after right shifting
Returns:
this

between

public boolean between(Id ccw,
                       Id cw)
Description copied from class: Id
Checks if this Id is between two given ids ccw (inclusive) and cw (exclusive) on the circle

Specified by:
between in class Id
Parameters:
ccw - the counterclockwise id
cw - the clockwise id
Returns:
true if this is between ccw (inclusive) and cw (exclusive), false otherwise

clockwise

public boolean clockwise(Id nid)
Checks to see if the Id nid is clockwise or counterclockwise from this, on the ring. An Id is clockwise if it is within the half circle clockwise from this on the ring. An Id is considered counter-clockwise from itself.

Specified by:
clockwise in class Id
Parameters:
nid - The Id we are comparing to
Returns:
true if clockwise, false otherwise.

getValue

public java.lang.Object getValue()
Return the int[] with the value of this Id.

Specified by:
getValue in class Id
Returns:
int[] with the value of this Id.
See Also:
Id.getValue()

setValue

public void setValue(java.lang.Object newValue)
Sets the new value with the int[] that appears at newValue.

Specified by:
setValue in class Id
Parameters:
newValue - New value as int[] representation.
See Also:
Id.setValue(java.lang.Object)

getMaximum

public static Id getMaximum()
This method returns the maximum value for a ChordId that is possible to build, according the number of bits for key actually in use. To return this maximum value use the design pattern Singleton, reviewing the number of bits in use.

Returns:
The maximum value sets to an Id, always according the number of bits actually in use.
See Also:
Id.getMaximum()

divide

public static Id divide(int numberOfNodes)
Divides the maximum domain of the node Id in numberOfNodes, to offers the offset between two consecutive nodes.

Parameters:
numberOfNodes - Number of nodes in the network.
Returns:
The offset between two consecutive nodes, to get a network with equidistance nodes, as an object value.
See Also:
Id.divide(int)

divideOn

public Id divideOn(int divisor)
This method returns the arithmetical result of this division:
thisId/divisor

Specified by:
divideOn in class Id
Parameters:
divisor - The number of parts to divide this Id.
Returns:
The result of division.

setValues

public Id setValues(java.math.BigInteger newValue)
Sets the internal int[] value from the BigInteger newValue.

Specified by:
setValues in class Id
Parameters:
newValue - The new value
Returns:
The Id itself
See Also:
Id.setValues(java.math.BigInteger)

setValues

public Id setValues(byte[] newValue)
Sets the internal int[] value from the byte[] newValue.

Specified by:
setValues in class Id
Parameters:
newValue - The new value
Returns:
The Id itself
See Also:
Id.setValues(byte[])

setValues

public Id setValues(double newValue)
Always throws a NoSuchMethodError.

Specified by:
setValues in class Id
Parameters:
newValue - The new value
Returns:
The Id itself
See Also:
Id.setValues(double)

setValues

public Id setValues(int newValue)
Sets the newValue to the first position of the internal int[], and the rest of positions to zero.

Specified by:
setValues in class Id
Parameters:
newValue - The new value.
Returns:
The Id itself
See Also:
Id.setValues(int)

setValues

public Id setValues(int[] newValue)
Sets the internal value from the int[] newValue

Specified by:
setValues in class Id
Parameters:
newValue - The new value
Returns:
The Id itself
See Also:
Id.setValues(int[])

setValues

public Id setValues(java.util.Random valueGenerator)
Uses the valueGenerator to radomly build a new value.

Specified by:
setValues in class Id
Parameters:
valueGenerator - A Random number generator.
Returns:
The Id itself
See Also:
Id.setValues(java.util.Random)

setValues

public Id setValues(java.lang.String newValue)
Copies the string representation of a number to the internal int[].

Specified by:
setValues in class Id
Parameters:
newValue - The new value.
Returns:
The Id itself
See Also:
Id.setValues(java.lang.String)