Example usage for java.math BigInteger add

List of usage examples for java.math BigInteger add

Introduction

In this page you can find the example usage for java.math BigInteger add.

Prototype

BigInteger add(long val) 

Source Link

Document

Package private methods used by BigDecimal code to add a BigInteger with a long.

Usage

From source file:com.kactech.otj.Utils.java

public static BigInteger base62Decode(final String string) {
    if (string.length() == 0) {
        throw new IllegalArgumentException("string must not be empty");
    }//from   ww  w . j  a  va  2  s . c  o m
    BigInteger result = BigInteger.ZERO;
    int digits = string.length();
    for (int index = 0; index < digits; index++) {
        int digit = B62_DIGITS.indexOf(string.charAt(digits - index - 1));
        result = result.add(BigInteger.valueOf(digit).multiply(B62_BASE.pow(index)));
    }
    return result;
}

From source file:com.trsst.Common.java

public static byte[] fromBase58(String s) {
    try {/* ww  w  .  j  a v a 2s . c o  m*/
        boolean leading = true;
        int lz = 0;
        BigInteger b = BigInteger.ZERO;
        for (char c : s.toCharArray()) {
            if (leading && c == '1') {
                ++lz;
            } else {
                leading = false;
                b = b.multiply(BigInteger.valueOf(58));
                b = b.add(BigInteger.valueOf(r58[c]));
            }
        }
        byte[] encoded = b.toByteArray();
        if (encoded[0] == 0) {
            if (lz > 0) {
                --lz;
            } else {
                byte[] e = new byte[encoded.length - 1];
                System.arraycopy(encoded, 1, e, 0, e.length);
                encoded = e;
            }
        }
        byte[] result = new byte[encoded.length + lz];
        System.arraycopy(encoded, 0, result, lz, encoded.length);

        return result;
    } catch (ArrayIndexOutOfBoundsException e) {
        throw new IllegalArgumentException("Invalid character in address");
    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:net.spfbl.whois.SubnetIPv6.java

public static String getNextIPv6(String ip) {
    if (ip == null) {
        return null;
    } else if (SubnetIPv6.isValidIPv6(ip)) {
        BigInteger address = new BigInteger(1, address(ip));
        if (address.equals(ADDRESS_MAX)) {
            return null;
        } else {/*w w  w  .j a va  2s.c om*/
            address = address.add(ADDRESS_UNIT);
            int p8 = address.and(ADDRESS_OCTET).intValue();
            address = address.shiftRight(16);
            int p7 = address.and(ADDRESS_OCTET).intValue();
            address = address.shiftRight(16);
            int p6 = address.and(ADDRESS_OCTET).intValue();
            address = address.shiftRight(16);
            int p5 = address.and(ADDRESS_OCTET).intValue();
            address = address.shiftRight(16);
            int p4 = address.and(ADDRESS_OCTET).intValue();
            address = address.shiftRight(16);
            int p3 = address.and(ADDRESS_OCTET).intValue();
            address = address.shiftRight(16);
            int p2 = address.and(ADDRESS_OCTET).intValue();
            address = address.shiftRight(16);
            int p1 = address.and(ADDRESS_OCTET).intValue();
            return Integer.toHexString(p1) + ":" + Integer.toHexString(p2) + ":" + Integer.toHexString(p3) + ":"
                    + Integer.toHexString(p4) + ":" + Integer.toHexString(p5) + ":" + Integer.toHexString(p6)
                    + ":" + Integer.toHexString(p7) + ":" + Integer.toHexString(p8);
        }
    } else {
        return null;
    }
}

From source file:Bytes.java

/**
 * Convert the specified amount into a human readable (though slightly less accurate)
 * result. IE:/*  www .  ja  v  a 2  s  . co  m*/
 * '4096 B' to '4 KB'
 * '5080 B' to '5 KB' even though it is really '4 KB + 984 B'
 */
public static String friendly(Bytes type, BigInteger value) {
    /**
     * Logic:
     * Loop from YB to B
     * If result = 0, continue
     * Else, round off
     *
     * NOTE: BigIntegers are not reusable, so not point in caching them outside the loop
     */
    for (Bytes newType : reversed) {
        BigInteger newAmount = newType.convertFrom(value, type);
        if (newAmount.equals(BigInteger.ZERO))
            continue;
        // Found the right one. Now to round off
        BigInteger unitBytes = Bytes.B.convertFrom(BigInteger.ONE, newType);
        BigInteger usedBytes = newAmount.multiply(unitBytes);
        BigInteger remainingBytes = Bytes.B.convertFrom(value, type).subtract(usedBytes);
        if (remainingBytes.equals(BigInteger.ZERO))
            return String.format(friendlyFMT, newAmount.toString(), newType);
        if (remainingBytes.equals(value))
            return String.format(friendlyFMT, newAmount.toString(), newType);

        BigInteger halfUnit = unitBytes.divide(TWO);
        if ((remainingBytes.subtract(halfUnit)).signum() < 0)
            return String.format(friendlyFMT, newAmount.toString(), newType);

        return String.format(friendlyFMT, (newAmount.add(BigInteger.ONE)).toString(), newType);
    }

    // Give up
    return String.format(friendlyFMT, value.toString(), type);
}

From source file:jp.co.acroquest.endosnipe.report.converter.util.calc.BigIntegerCalculator.java

public Object add(Object obj1, Object obj2) {
    BigInteger integerData1 = (BigInteger) obj1;
    BigInteger integerData2 = (BigInteger) obj2;

    return (Object) (integerData1.add(integerData2));
}

From source file:de.tudarmstadt.ukp.teaching.uima.nounDecompounding.ranking.TotalFreqAmout.java

/**
 * Adds all frequency values//from w w  w  .  j  a  v  a2  s  .  c  o  m
 * @return
 * @throws IOException
 */
public BigInteger countFreq() throws IOException {
    BigInteger count = BigInteger.valueOf(0);
    for (FSDirectory dir : this.dirs) {
        count = count.add(this.countFreq(dir));
    }
    return count;
}

From source file:io.curly.advisor.model.Review.java

private BigDecimal fixPrecision(BigDecimal rate) {
    if (rate == null)
        return BigDecimal.ZERO;
    BigDecimal decimal = rate.remainder(BigDecimal.ONE);
    BigInteger integer = rate.toBigInteger();
    if (decimal.compareTo(new BigDecimal(0.5)) > 0) {
        return new BigDecimal(integer.add(new BigInteger("1")));
    } else if (decimal.compareTo(new BigDecimal(0.5)) < 0) {
        return new BigDecimal(integer);
    }/*  w  ww.j ava2s.c  o  m*/
    return rate;
}

From source file:io.curly.advisor.model.Review.java

/**
 * Function to round up when the decimal part is more than a half, and down when its less than, if it is a half keep it
 *
 * @return fixed rate//www. java2 s. c  om
 */
public BigDecimal fixPrecision() {
    if (rate == null)
        return null;
    BigDecimal decimal = rate.remainder(BigDecimal.ONE);
    BigInteger integer = rate.toBigInteger();
    if (decimal.compareTo(new BigDecimal(0.5)) > 0) {
        rate = new BigDecimal(integer.add(new BigInteger("1")));
    } else if (decimal.compareTo(new BigDecimal(0.5)) < 0) {
        rate = new BigDecimal(integer);
    }
    return rate;
}

From source file:de.tudarmstadt.ukp.teaching.uima.nounDecompounding.ranking.AbstractRanker.java

/**
 * Returns the frequency for a array of words
 * @param words/*from  w ww . j  a v  a 2s .c  o  m*/
 * @return
 */
protected BigInteger freq(String[] words) {
    BigInteger total = BigInteger.valueOf(0l);

    for (NGram gram : finder.find(words)) {
        total = total.add(BigInteger.valueOf(gram.getFreq()));
    }

    return total;
}

From source file:UInt64.java

/**
 * Create a UInt64 from two longs.//  w ww.  ja  v a2  s .  c o m
 * 
 * @param top
 *          Most significant 4 bytes.
 * @param bottom
 *          Least significant 4 bytes.
 */
public UInt64(long top, long bottom) {
    BigInteger a = new BigInteger("" + top);
    a = a.shiftLeft(32);
    a = a.add(new BigInteger("" + bottom));
    // if (0 > a.compareTo(BigInteger.ZERO)) { throw new NumberFormatException(MessageFormat.format(
    //    _("{0} is not between {1} and {2}."), new Object[] { a, MIN_VALUE, MAX_BIG_VALUE })); }
    //if (0 < a.compareTo(MAX_BIG_VALUE)) { throw new NumberFormatException(MessageFormat.format(
    //   _("{0} is not between {1} and {2}."), new Object[] { a, MIN_VALUE, MAX_BIG_VALUE })); }
    value = a;
    this.top = top;
    this.bottom = bottom;
}