org.clazzes.optional.lang
Class BigInt

java.lang.Object
  extended by org.clazzes.optional.lang.BigInt
All Implemented Interfaces:
java.io.Serializable, DataCodeable

public final class BigInt
extends java.lang.Object
implements Serializable, DataCodeable

A variable length integer class.

See Also:
Serialized Form

Constructor Summary
BigInt(int i)
           
BigInt(int sign, byte[] raw)
          Create a big integer from a MSB raw value.
BigInt(int sign, int[] v)
          Construct a string from an internal representation.
BigInt(long i)
           
 
Method Summary
 BigInt add(BigInt a)
          Add a to this and return the result.
 int compareTo(BigInt a)
          Compare this number with a by taking signs into account.
 int compareTo(int a)
          Compare this number with a by taking signs into account.
 boolean equals(java.lang.Object o)
           
 byte[] getASN1()
          Convert this BigInt to an ASN1 MSB first integer representation.
 int getByteLength()
           
 int getHighestBit()
          Calculate the highest bit set in the magnitude of this number.
 byte[] getMsbMagnitude()
           
 int getSign()
           
static BigInt hexValueOf(java.lang.String s)
          Parse the given hexadecimal string and return the resulting BigInt.
 BigInt mod(BigInt b)
          Calculate this modulo b.
 BigInt mul(BigInt a)
          Multiply a and this and return the result.
 BigInt mulMod(BigInt a, BigInt b)
          Multiply a and this and return the result modulo b.
 BigInt powMod(BigInt n, BigInt b)
          Calculate this^n and return the result modulo b.
 BigInt powMod(int n, BigInt b)
          Calculate this^n and return the result modulo b.
static BigInt readDataInstance(java.io.DataInput in)
          Read a positive BigInt from the given data input using a two byte length prefix.
 BigInt sub(BigInt a)
          Subtract a from this and return the result.
 java.lang.String toHexString()
          Convert a BigInt to a hexadecimal string.
 java.lang.String toJavaInitializer(java.lang.String identifier)
          Return a jString, which may be inserted in JAVA source code to initialize a static instance in the form static final BigInt identifier = new BigInt(sign, new int[] { 0xdeabbeef,0xadbedba,.... });
 java.lang.String toString()
           
static BigInt valueOf(byte[] asn1)
          Parse an ASN.1 INTEGER representation.
static BigInt valueOf(java.lang.String s)
          Parse the given decimal string and return the resulting BigInt.
 void writeData(java.io.DataOutput out)
          Write the MSB magnitude of this BigInt to the given data output using a two byte length prefix.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BigInt

public BigInt(int sign,
              int[] v)
Construct a string from an internal representation. Using this constructor may break the the immutability of the generated instance, when the passed array is modified after calling this constructor. This constructor is however very useful, when initializing static instances in a memory optimized version.

Parameters:
sign - The sign of the returned instance. Should be 1 or -1.
v - The internal integer array starting with the least significant 32 bits.
See Also:
toJavaInitializer(String)

BigInt

public BigInt(int i)

BigInt

public BigInt(long i)

BigInt

public BigInt(int sign,
              byte[] raw)
Create a big integer from a MSB raw value.

Parameters:
sign - If negative, this number is negative.
raw - An array of raw bytes starting with the most significant one.
Method Detail

valueOf

public static BigInt valueOf(java.lang.String s)
Parse the given decimal string and return the resulting BigInt.

Parameters:
s - A string consisting of an optional - sign followed by a sequence of digits 0...9.
Returns:
A BigInt representing the parsed sequence.

hexValueOf

public static BigInt hexValueOf(java.lang.String s)
Parse the given hexadecimal string and return the resulting BigInt.

Parameters:
s - A string consisting of an optional - sign followed by a sequence of digits 0...9,A...F.
Returns:
A BigInt representing the parsed sequence.

valueOf

public static BigInt valueOf(byte[] asn1)
Parse an ASN.1 INTEGER representation. This representation is in MSB-first two's complement format with the following restriction, that the bits of the first octet and the first bit of the second octet may not all be zero or one.

Parameters:
asn1 - The content octets of an ASN.1 INTEGER.
Returns:
A new BigInt instance.

readDataInstance

public static BigInt readDataInstance(java.io.DataInput in)
                               throws java.io.IOException
Read a positive BigInt from the given data input using a two byte length prefix.

Parameters:
in - The input stream to read from.
Throws:
java.io.IOException

writeData

public void writeData(java.io.DataOutput out)
               throws java.io.IOException
Write the MSB magnitude of this BigInt to the given data output using a two byte length prefix.

Specified by:
writeData in interface DataCodeable
Parameters:
out - The output stream to write to.
Throws:
java.io.IOException - Upon write errors.

getByteLength

public int getByteLength()
Returns:
The length of the unsigned magnitude as returned by getMsbMagnitude() in bytes.

getMsbMagnitude

public byte[] getMsbMagnitude()
Returns:
The raw MSB magnitude of this integer.

getASN1

public byte[] getASN1()
Convert this BigInt to an ASN1 MSB first integer representation.

Returns:
A MSB first two's complement representation of this BigInt.

toHexString

public java.lang.String toHexString()
Convert a BigInt to a hexadecimal string.

Returns:
An uppercase hexadecimal string with an optional minus sign and an even number of hexadecimal, uppercase digits.

getSign

public int getSign()
Returns:
The sign of this integer.

compareTo

public int compareTo(BigInt a)
Compare this number with a by taking signs into account.

Parameters:
a - the number to compare this instance with.
Returns:
-1,0 or 1 if this number is less than, equal to or greater than a.

compareTo

public int compareTo(int a)
Compare this number with a by taking signs into account.

Parameters:
a - the number to compare this instance with.
Returns:
-1,0 or 1 if this number is less than, equal to or greater than a.

equals

public boolean equals(java.lang.Object o)
Overrides:
equals in class java.lang.Object

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object
Returns:
A decimal representation of this BigInt.

toJavaInitializer

public java.lang.String toJavaInitializer(java.lang.String identifier)
Return a jString, which may be inserted in JAVA source code to initialize a static instance in the form
   static final BigInt identifier = new BigInt(sign,
     new int[] {
       0xdeabbeef,0xadbedba,....
     });
 

Parameters:
identifier - The name of the identifier to use.

add

public BigInt add(BigInt a)
Add a to this and return the result.

Parameters:
a - The number to add.
Returns:
this+a.

sub

public BigInt sub(BigInt a)
Subtract a from this and return the result.

Parameters:
a - The number to subtract.
Returns:
this-a.

mul

public BigInt mul(BigInt a)
Multiply a and this and return the result.

Parameters:
a - The number to multiply.
Returns:
this*a.

getHighestBit

public int getHighestBit()
Calculate the highest bit set in the magnitude of this number. The bit with value 1 is counted as bit 0, the bit with value 1<<x is counted as bit x. For a value of zero, -1 is returned.

Returns:
The number of the highest bit set, starting with 0 at the least significant bit.

mod

public BigInt mod(BigInt b)
Calculate this modulo b.

Parameters:
b -
Returns:
this % b.

mulMod

public BigInt mulMod(BigInt a,
                     BigInt b)
Multiply a and this and return the result modulo b.

Parameters:
a - The number to multiply.
b - The modulus.
Returns:
(this*a)%b.

powMod

public BigInt powMod(int n,
                     BigInt b)
Calculate this^n and return the result modulo b.

Parameters:
n - The exponent.
b - The modulus.
Returns:
(this^n)%b.

powMod

public BigInt powMod(BigInt n,
                     BigInt b)
Calculate this^n and return the result modulo b.

Parameters:
n - The exponent.
b - The modulus.
Returns:
(this^n)%b.


Copyright © 2010. All Rights Reserved.