public static Number optimizeBigint ( BigInteger bigint ) { // final BigInteger MAXINT=BigInteger.valueOf(Integer.MAX_VALUE); // final BigInteger MININT=BigInteger.valueOf(Integer.MIN_VALUE); final BigInteger MAXLONG = BigInteger . valueOf ( Long . MAX_VALUE ) ; final BigInteger MINLONG = BigInteger . valueOf ( Long . MIN_VALUE ) ; switch ( bigint . signum ( ) ) { case 0 : return 0L ; case 1 : // >0 // if(bigint.compareTo(MAXINT)<=0) return bigint.intValue(); if ( bigint . compareTo ( MAXLONG ) <= 0 ) return bigint . longValue ( ) ; break ; case - 1 : // <0 // if(bigint.compareTo(MININT)>=0) return bigint.intValue(); if ( bigint . compareTo ( MINLONG ) >= 0 ) return bigint . longValue ( ) ; break ; } return bigint ; }
Optimize a biginteger if possible return a long primitive datatype .