net.tomp2p.peers
Class Number160

java.lang.Object
  extended by java.lang.Number
      extended by net.tomp2p.peers.Number160
All Implemented Interfaces:
Serializable, Comparable<Number160>

public final class Number160
extends Number
implements Comparable<Number160>

This class represents a 160 bit number. This class is preferred over BigInteger as we always have 160bit, and thus, methods can be optimized.

Author:
Thomas Bocek
See Also:
Serialized Form

Field Summary
static int BITS
           
static int BYTE_ARRAY_SIZE
           
static int INT_ARRAY_SIZE
           
static Number160 MAX_VALUE
           
static Number160 ONE
           
static Number160 ZERO
           
 
Constructor Summary
Number160()
          Create a Key with value 0.
Number160(byte[] val)
          Creates a new Key using the byte array.
Number160(byte[] val, int offset, int length)
          Creates a new Key using the byte array.
Number160(int... val)
          Create an instance with an integer array.
Number160(int val)
          Creates a Key with the integer value
Number160(long val)
           
Number160(Random random)
          Creates a new Key with random values in it.
Number160(ShortString shortString)
           
Number160(String val)
          Create a Key from a string.
 
Method Summary
 int bitLength()
          Calculates the number of bits used to represent this number.
 int compareTo(Number160 o)
           
static Number160 createHash(int integerValue)
          Create a new Number160 from the integer, which fills all the 160bits.
static Number160 createHash(long longValue)
          Create a new Number160 from the long, which fills all the 160bit.
static Number160 createHash(String string)
          Create a new Number160 using SHA1 on the string.
 double doubleValue()
           
 boolean equals(Object obj)
           
 float floatValue()
           
 int hashCode()
           
 int intValue()
           
 boolean isZero()
          Checks if this number is zero
 long longValue()
           
 Number160 shiftLeft(int nr)
          Shift left the 160bit number.
 Number160 shiftRight(int nr)
          Shift right the 160bit number.
 byte[] toByteArray()
          Returns a byte array, which is always of size 20.
 int toByteArray(byte[] me, int offset)
          Fills the byte array with this number
 int[] toIntArray()
          Returns a copy of the backing array, which is always of size 5.
 String toString()
           
 String toString(boolean removeLeadingZero)
          Shows the content in a human readable manner
 long unsignedInt(int pos)
          For debugging...
 Number160 xor(Number160 key)
          Xor operation.
 
Methods inherited from class java.lang.Number
byteValue, shortValue
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

BITS

public static final int BITS
See Also:
Constant Field Values

MAX_VALUE

public static final Number160 MAX_VALUE

INT_ARRAY_SIZE

public static final int INT_ARRAY_SIZE
See Also:
Constant Field Values

BYTE_ARRAY_SIZE

public static final int BYTE_ARRAY_SIZE
See Also:
Constant Field Values

ZERO

public static final Number160 ZERO

ONE

public static final Number160 ONE
Constructor Detail

Number160

public Number160()
Create a Key with value 0.


Number160

public Number160(ShortString shortString)

Number160

public Number160(int... val)
Create an instance with an integer array. This integer array will be copied into the backing array.

Parameters:
val - The value to copy to the backing array. Since this class stores 160bit numbers, the array needs to be of size 5 or smaller.

Number160

public Number160(String val)
Create a Key from a string. The string has to be of length 40 to fit into the backing array. Note that this string is *always* in hexadecimal, there is no 0x... required before the number.

Parameters:
val - The characters allowed are [0-9a-f], which is in hexadecimal

Number160

public Number160(int val)
Creates a Key with the integer value

Parameters:
val - integer value

Number160

public Number160(long val)

Number160

public Number160(byte[] val)
Creates a new Key using the byte array. The array is copied to the backing int[]

Parameters:
val -

Number160

public Number160(byte[] val,
                 int offset,
                 int length)
Creates a new Key using the byte array. The array is copied to the backing int[] starting at the given offest.

Parameters:
val -
offset - The offset where to start

Number160

public Number160(Random random)
Creates a new Key with random values in it.

Parameters:
random - The object to create pseudo random numbers. For testing and debugging, the seed in the random class can be set to make the random values repeatable.
Method Detail

xor

public Number160 xor(Number160 key)
Xor operation. This operation is ~2.5 times faster than with BigInteger

Parameters:
key - The second operand for the xor operation
Returns:
A new key with the resurt of the xor operation

shiftRight

public Number160 shiftRight(int nr)
Shift right the 160bit number.

Parameters:
nr - The number of bits to be shifted
Returns:
A new 160bit number with the shifted number

shiftLeft

public Number160 shiftLeft(int nr)
Shift left the 160bit number.

Parameters:
nr - The number of bits to be shifted
Returns:
A new 160bit number with the shifted number

toIntArray

public int[] toIntArray()
Returns a copy of the backing array, which is always of size 5.

Returns:
a copy of the backing array

toByteArray

public int toByteArray(byte[] me,
                       int offset)
Fills the byte array with this number

Parameters:
me -

toByteArray

public byte[] toByteArray()
Returns a byte array, which is always of size 20.

Returns:
a byte array

toString

public String toString(boolean removeLeadingZero)
Shows the content in a human readable manner

Parameters:
removeLeadingZero - Indicates of leading zeros should be removed
Returns:
A human readable representation of this key

isZero

public boolean isZero()
Checks if this number is zero

Returns:
True if this number is zero, false otherwise

bitLength

public int bitLength()
Calculates the number of bits used to represent this number. All leading (leftmost) zero bits are ignored

Returns:
The bits used

toString

public String toString()
Overrides:
toString in class Object

doubleValue

public double doubleValue()
Specified by:
doubleValue in class Number

floatValue

public float floatValue()
Specified by:
floatValue in class Number

intValue

public int intValue()
Specified by:
intValue in class Number

unsignedInt

public long unsignedInt(int pos)
For debugging...

Parameters:
pos -
Returns:

longValue

public long longValue()
Specified by:
longValue in class Number

compareTo

public int compareTo(Number160 o)
Specified by:
compareTo in interface Comparable<Number160>

equals

public boolean equals(Object obj)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

createHash

public static Number160 createHash(int integerValue)
Create a new Number160 from the integer, which fills all the 160bits. A new random object will be created.

Parameters:
integerValue - The value to hash from
Returns:
A hash from based on pseudo random, to fill the 160bits

createHash

public static Number160 createHash(long longValue)
Create a new Number160 from the long, which fills all the 160bit. A new random object will be created, thus, its thread safe

Parameters:
longValue - The value to hash from
Returns:
A hash based on pseudo random, to fill the 160bits

createHash

public static Number160 createHash(String string)
Create a new Number160 using SHA1 on the string.

Parameters:
string - The value to hash from
Returns:
A hash based on SHA1 of the string


Copyright © 2013. All Rights Reserved.