001import java.math.BigInteger; 002 003/** Wrapper for {@code java.math.}{@link java.math.BigInteger}. 004 * Required for mixed arithmetic with {@code int} or {@code long} operands because {@code java.math.}{@link java.math.BigInteger} 005 * does not implement a {@link java.math.BigInteger#valueOf(long) BigInteger(long)} constructor.<br> 006 * <br> 007 * Note: As class {@code BigIntegerAlgebra} extends class {@code java.math.BigInteger} methods of the later class can be called 008 * on {@code BigIntegerAlgebra} instances. The methods however will not return an {@code BigIntegerAlgebra} instance but an instance of the super 009 * class as result. If needed, call the {@link #BigIntegerAlgebra(BigInteger)} constructor on the result to get a {@code BigIntegerAlgebra} 010 * instance again. If this is considered inconvenient, wrapper methods for all relevant methods of {@code BigInteger} need to 011 * be implemented in the Algebra wrapper class. 012 * @see <a target="_top" href="../programmer_examples/MyFractionAlgebra_UserDefinedAlgebra.html">user defined algebra</a> 013 * @see <a target="_top" href="../programmer_examples/ProgrammerExamples.html#biginteger">BigIntegerAlgebra_Demo</a> 014 */ 015public class BigIntegerAlgebra extends BigInteger 016{ 017 static final boolean DEBUG = false; 018 019// private BigInteger itsBigInteger = null; 020 021 022 /** Constructs a new BigIntegerAlgebra object from a {@code java.math.BigInteger}. */ 023 public BigIntegerAlgebra( BigInteger value ) 024 { 025 // itsBigInteger = value; 026 super( value.toByteArray() ); 027 } 028 029 /** Constructs a new BigIntegerAlgebra object. <br> 030 * Calls {@link java.math.BigInteger#BigInteger(java.lang.String) java.math.BigInteger(String)} but allows 031 * arbitrary {@code ' '} or {@code '_'} characters in {@code value} to improve readability. 032 */ 033 public BigIntegerAlgebra( String value ) 034 { 035 super( KmgStaticUtilities.squeeze( value, "_ " ) ); 036 } 037 038 /** Constructs a new BigIntegerAlgebra object. <br> 039 * Calls {@link java.math.BigInteger#BigInteger(java.lang.String, int) java.math.BigInteger( value, radix )} 040 * but allows arbitrary {@code ' '} or {@code '_'} characters in {@code value}. 041 */ 042 public BigIntegerAlgebra( String value, int radix ) 043 { 044 super( KmgStaticUtilities.squeeze( value, "_ " ), radix ); 045 } 046 047 /** Constructs a new BigIntegerAlgebra object. <br> 048 * Required for mixed operations: int <i>op</i> BigInteger, BigInteger <i>op</i> int 049 */ 050 public BigIntegerAlgebra( long value ) 051 { 052 // itsBigInteger = BigInteger.valueOf( value ); 053 // super( "" + value ); 054 this( BigInteger.valueOf( value ) ); 055 } 056 057 058// --- reflection methods --- 059 060// arithmetic methods are required to return BigIntegerAlgebra 061 062 /** Calls {@link java.math.BigInteger#negate}. */ 063 public BigIntegerAlgebra neg() 064 { 065 return new BigIntegerAlgebra( negate() ); 066 } 067 068 /** Adds {@code opnd} to this. <br> 069 * Calls {@link java.math.BigInteger#add}. 070 */ 071 public BigIntegerAlgebra add( BigInteger opnd ) 072 { 073 return new BigIntegerAlgebra( super.add( opnd ) ); 074 } 075 076 /** Subtracts {@code opnd} from this. <br> 077 * Calls {@link java.math.BigInteger#subtract(java.math.BigInteger)}. 078 */ 079 public BigIntegerAlgebra sub( BigInteger opnd ) 080 { 081 return new BigIntegerAlgebra( subtract( opnd ) ); 082 } 083 084 /** (Post)Multiplies this by {@code opnd}. <br> 085 * Calls {@link java.math.BigInteger#multiply}. 086 */ 087 public BigIntegerAlgebra mul( BigInteger opnd ) 088 { 089 return new BigIntegerAlgebra( multiply( opnd ) ); 090 } 091 092 /** Divides this by {@code opnd}. <br> 093 * Calls {@link java.math.BigInteger#divide}. 094 */ 095 public BigIntegerAlgebra div( BigInteger opnd ) 096 { 097 return new BigIntegerAlgebra( divide( opnd ) ); 098 } 099 100 /** Returns the remainder of this divided by {@code opnd}. <br> 101 * Calls {@link java.math.BigInteger#remainder}. 102 */ 103 public BigIntegerAlgebra remainder( BigInteger opnd ) 104 { 105 return new BigIntegerAlgebra( super.remainder( opnd ) ); 106 } 107 108 /** Returns the power of {@code exponent} of this. <br> 109 * Calls {@link java.math.BigInteger#pow}. 110 */ 111 public BigIntegerAlgebra pow( int exponent ) 112 { 113 return new BigIntegerAlgebra( super.pow( exponent ) ); 114 } 115 116 117// --- other reflection methods --- 118 119}