Optimize a biginteger, if possible return a long primitive datatype.
430081
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;
	}
1240311
static byte[] toIntegerBytes(final BigInteger bigInt) {
        int bitlen = bigInt.bitLength();
        // round bitlen
        bitlen = ((bitlen + 7) >> 3) << 3;
        final byte[] bigBytes = bigInt.toByteArray();

        if (((bigInt.bitLength() % 8) != 0) && (((bigInt.bitLength() / 8) + 1) == (bitlen / 8))) {
            return bigBytes;
        }
        // set up params for copying everything but sign bit
        int startSrc = 0;
        int len = bigBytes.length;

        // if bigInt is exactly byte-aligned, just skip signbit in copy
        if ((bigInt.bitLength() % 8) == 0) {
            startSrc = 1;
            len--;
        }
        final int startDst = bitlen / 8 - len; // to pad w/ nulls as per spec
        final byte[] resizedBytes = new byte[bitlen / 8];
        System.arraycopy(bigBytes, startSrc, resizedBytes, startDst, len);
        return resizedBytes;
    }
1363828
public static long getLong(ByteBuffer buf, int index) {
        return buf.order() == BIG_ENDIAN ? getLongB(buf, index) : getLongL(buf, index);
    }
889462
public static Integer toNullableInteger(Object value) {
		Long result = LongConverter.toNullableLong(value);
		return result != null ? (int) ((long) result) : null;
	}
783712
public static BigInteger read(String value) {
		if (!StringUtils.hasText(value)) return null;
		
		return new BigInteger(value);
	}
581351
public static Function<Object,BigInteger> bigInteger(final String methodName, final Object... optionalParameters) {
        return methodForBigInteger(methodName, optionalParameters);
    }
961911
public long getLong(int index)
  {
    Object value = _values[index - 1];

    if (value instanceof Long) {
      return (Long) value;
    }
    else if (value instanceof Integer) {
      return (Integer) value;
    }
    else {
      return Long.valueOf(value.toString());
    }
  }
814243
public BigInteger getBigInteger(int index) throws JSONException {
		Object object = this.get(index);
		try {
			return new BigInteger(object.toString());
		} catch (Exception e) {
			throw new JSONException("JSONArray[" + index +
					"] could not convert to BigInteger.");
		}
	}
1362900
private void optimize(long fileLength) {

		final int availableProcessors = Runtime.getRuntime().availableProcessors();

		final long free = Runtime.getRuntime().freeMemory() / 2;

		int workerSize = availableProcessors / 2;
		if (workerSize < 2) {
			workerSize = 2;
		}

		long bufferSize = free / workerSize;
		if (bufferSize > 1 * 1024 * 1024) {
			bufferSize = 1 * 1024 * 1024;
		}

		long blockSize = fileLength / workerSize;
		if (blockSize > 1 * 1024 * 1024) {
			blockSize = 1 * 1024 * 1024;
		}
		int iBlockSize = (int) blockSize;

		if (bufferSize > blockSize) {
			bufferSize = blockSize;
		}
		int iBufferSize = (int) bufferSize;

		this.setBlockSize(iBlockSize);
		this.setMaxNumOfThreads(workerSize);
		this.setBufferSizePerWorker(iBufferSize);
		this.setSubBufferSize(256);

	}
517589
def _int_size(x):
    """"""
    if -0x80 <= x <= 0x7F:
        return 1
    elif -0x8000 <= x <= 0x7FFF:
        return 2
    elif -0x80000000 <= x <= 0x7FFFFFFF:
        return 4
    elif long(-0x8000000000000000) <= x <= long(0x7FFFFFFFFFFFFFFF):
        return 8
    else:
        raise RuntimeError("Cannot represent value: " + str(x))
1366700
private BigInteger bigIntegerProduct()
    {
        return this.injectInto(BigInteger.valueOf(1L), new Function2<BigInteger, Integer, BigInteger>()
        {
            public BigInteger value(BigInteger result, Integer each)
            {
                return result.multiply(BigInteger.valueOf(each.longValue()));
            }
        });
    }
1113028
private BigInteger toBigInteger(Object value) {
		if (value instanceof Long) {
			return BigInteger.valueOf((Long) value);
		} else if(value instanceof Boolean) {
			return BigInteger.valueOf((boolean)value ? 1 : 0);
		}
		final String valueStr = convertToStr(value);
		if (StrUtil.isBlank(valueStr)) {
			return null;
		}
		return new BigInteger(valueStr);
	}
101236
public static BigInteger decode(String s) {
        byte[] bytes = Base64.decodeBase64(s);

        BigInteger bi = new BigInteger(1, bytes);
        return bi;
    }
826238
public BigInteger toBigInteger(Object val) {
    if (val == null) {
      return BigInteger.ZERO;
    }
    else if (val instanceof BigInteger) {
      return (BigInteger) val;
    }
    else if (val instanceof Double) {
      if (!Double.isNaN(((Double) val).doubleValue())) {
        return new BigInteger(val.toString());
      }
      else {
        return BigInteger.ZERO;
      }
    }
    else if (val instanceof Number) {
      return new BigInteger(val.toString());
    }
    else if (val instanceof String) {
      String string = (String) val;
      if ("".equals(string.trim())) {
        return BigInteger.ZERO;
      }
      else {
        return new BigInteger(string);
      }
    }
    else if (val instanceof Character) {
      int i = ((Character) val).charValue();
      return BigInteger.valueOf(i);
    }

    throw new ArithmeticException("BigInteger coercion: " + val.getClass().getName() + ":(" + val + ")");
  }
874986
BigInteger retrieve(BigInteger initValue, int certainty, java.util.Random random) {
        // Examine the sieve one long at a time to find possible primes
        int offset = 1;
        for (int i=0; i<bits.length; i++) {
            long nextLong = ~bits[i];
            for (int j=0; j<64; j++) {
                if ((nextLong & 1) == 1) {
                    BigInteger candidate = initValue.add(
                                           BigInteger.valueOf(offset));
                    if (candidate.primeToCertainty(certainty, random))
                        return candidate;
                }
                nextLong >>>= 1;
                offset+=2;
            }
        }
        return null;
    }
1120669
private Long getIntegerValue(Map<String, Object> map, String key) {
		try {
			return Long.valueOf(String.valueOf(map.get(key))); // normalize to String before creating integer value;			
		} catch (NumberFormatException e) {
			return null;
		}
	}
41779
public static long getLong(final DirectBuffer buffer, final int index, final Encoding encoding)
    {
        switch (encoding.primitiveType())
        {
            case CHAR:
                return buffer.getByte(index);

            case INT8:
                return buffer.getByte(index);

            case INT16:
                return buffer.getShort(index, encoding.byteOrder());

            case INT32:
                return buffer.getInt(index, encoding.byteOrder());

            case INT64:
                return buffer.getLong(index, encoding.byteOrder());

            case UINT8:
                return (short)(buffer.getByte(index) & 0xFF);

            case UINT16:
                return buffer.getShort(index, encoding.byteOrder()) & 0xFFFF;

            case UINT32:
                return buffer.getInt(index, encoding.byteOrder()) & 0xFFFF_FFFFL;

            case UINT64:
                return buffer.getLong(index, encoding.byteOrder());

            default:
                throw new IllegalArgumentException("Unsupported type for long: " + encoding.primitiveType());
        }
    }