Example usage for java.math BigInteger intValue

List of usage examples for java.math BigInteger intValue

Introduction

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

Prototype

public int intValue() 

Source Link

Document

Converts this BigInteger to an int .

Usage

From source file:jp.co.ntts.vhut.util.Ipv4ConversionUtil.java

/**
 * (byte[])??????./*from  w w  w.j  a va2  s.co m*/
 * @param baddr 
 * @return ?
 */
static int convertToInt(byte[] baddr) {
    BigInteger order = new BigInteger(baddr);
    return order.intValue();
}

From source file:com.github.jessemull.microflex.util.BigIntegerUtil.java

/**
 * Converts a list of BigIntegers to a list of integers.
 * @param    List<BigInteger>    list of BigIntegers
 * @return                       list of shorts
 *//*  ww  w. j av  a  2 s  . c  om*/
public static List<Integer> toIntList(List<BigInteger> list) {

    List<Integer> intList = new ArrayList<Integer>();

    for (BigInteger val : list) {
        if (!OverFlowUtil.intOverflow(val)) {
            OverFlowUtil.overflowError(val);
        }
        intList.add(val.intValue());
    }

    return intList;

}

From source file:org.alfresco.solr.AbstractAlfrescoSolrTests.java

/**
 * Builds and asserts that query returns a collection in the correct order by
 * checking the dbid value of each document is in the correct order.
 * //w ww . j a  v  a 2  s .c om
 * @author Michael Suzuki
 * @param query query used to search
 * @param dbids collection of dbids to compare
 * @throws Exception if error
 */
public static void assertQueryCollection(String query, Integer[] dbids) throws Exception {
    SolrQueryRequest solrReq = req(params("rows", "20", "qt", "/cmis", "q", query, "wt", "json"));
    try {
        String response = h.query(solrReq);
        JSONObject json = (JSONObject) JSONValue.parse(response);
        JSONObject res = (JSONObject) json.get("response");
        JSONArray docs = (JSONArray) res.get("docs");
        Assert.assertTrue(dbids.length == docs.size());
        int count = 0;
        for (Object doc : docs) {
            JSONObject item = (JSONObject) doc;
            BigInteger val = (BigInteger) item.get("DBID");
            Assert.assertEquals(dbids[count].intValue(), val.intValue());
            count++;
        }
    } finally {
        solrReq.close();
    }
}

From source file:cc.redberry.core.number.Exponentiation.java

static BigInteger findIntegerRoot(BigInteger base, BigInteger power) {
    BigInteger maxBits = BigInteger.valueOf(base.bitLength() + 1); // base < 2 ^ (maxBits + 1)
    // => base ^ ( 1 / power ) < 2 ^ ( (maxBits + 1) / power )

    BigInteger[] divResult = maxBits.divideAndRemainder(power);
    if (divResult[1].signum() == 0) // i.e. divResult[1] == 0
        maxBits = divResult[0];/*from   w  w  w.  j av a2 s.c o m*/
    else
        maxBits = divResult[0].add(BigInteger.ONE);

    if (maxBits.bitLength() > 31)
        throw new RuntimeException("Too many bits...");

    int targetBitsNumber = maxBits.intValue();
    int resultLengthM1 = targetBitsNumber / 8 + 1; //resultLength minus one
    byte[] result = new byte[resultLengthM1];
    resultLengthM1--;

    int bitNumber = targetBitsNumber;

    int cValue;
    BigInteger testValue;

    while ((--bitNumber) >= 0) {
        //setting bit
        result[resultLengthM1 - (bitNumber >> 3)] |= 1 << (bitNumber & 0x7);

        //Testing
        testValue = new BigInteger(result);
        cValue = ArithmeticUtils.pow(testValue, power).compareTo(base);
        if (cValue == 0)
            return testValue;
        if (cValue > 0)
            result[resultLengthM1 - (bitNumber >> 3)] &= ~(1 << (bitNumber & 0x7));
    }

    return null;
}

From source file:com.github.jessemull.microflex.util.IntegerUtil.java

/**
 * Safely converts a number to an integer. Loss of precision may occur. Throws
 * an arithmetic exception upon overflow.
 * @param    Object    number to parse/*from www.j a v  a 2 s .  com*/
 * @return             parsed number
 * @throws   ArithmeticException    on overflow
 */
public static int toInteger(Object obj) {

    /* Switch on class and convert to an int */

    String type = obj.getClass().getSimpleName();
    int parsed;

    switch (type) {

    case "Byte":
        Byte by = (Byte) obj;
        parsed = by.intValue();
        break;

    case "Short":
        Short sh = (Short) obj;
        parsed = sh.intValue();
        break;

    case "Integer":
        Integer in = (Integer) obj;
        parsed = in.intValue();
        break;

    case "Long":
        Long lo = (Long) obj;
        if (!OverFlowUtil.intOverflow(lo)) {
            throw new ArithmeticException("Overflow casting " + obj + " to an int.");
        }
        parsed = lo.intValue();
        break;

    case "Float":
        Float fl = (Float) obj;
        if (!OverFlowUtil.intOverflow(fl)) {
            throw new ArithmeticException("Overflow casting " + obj + " to an int.");
        }
        parsed = fl.intValue();
        break;

    case "BigInteger":
        BigInteger bi = (BigInteger) obj;
        if (!OverFlowUtil.intOverflow(bi)) {
            throw new ArithmeticException("Overflow casting " + obj + " to an int.");
        }
        parsed = bi.intValue();
        break;

    case "BigDecimal":
        BigDecimal bd = (BigDecimal) obj;
        if (!OverFlowUtil.intOverflow(bd)) {
            throw new ArithmeticException("Overflow casting " + obj + " to an int.");
        }
        parsed = bd.intValue();
        break;

    case "Double":
        Double db = (Double) obj;
        if (!OverFlowUtil.intOverflow(db)) {
            throw new ArithmeticException("Overflow casting " + obj + " to an int.");
        }
        parsed = db.intValue();
        break;

    default:
        throw new IllegalArgumentException(
                "Invalid type: " + type + "\nData values " + "must extend the abstract Number class.");

    }

    return parsed;
}

From source file:com.github.jessemull.microflex.util.IntegerUtil.java

/**
 * Safely converts a number to an integer. Loss of precision may occur. Throws
 * an arithmetic exception upon overflow.
 * @param    Number    number to parse//w  ww .  ja  va 2 s.  co m
 * @return             parsed number
 * @throws   ArithmeticException    on overflow
 */
public static int toInteger(Number number) {

    /* Switch on class and convert to an int */

    String type = number.getClass().getSimpleName();
    int parsed;

    switch (type) {

    case "Byte":
        Byte by = (Byte) number;
        parsed = by.intValue();
        break;

    case "Short":
        Short sh = (Short) number;
        parsed = sh.intValue();
        break;

    case "Integer":
        Integer in = (Integer) number;
        parsed = in.intValue();
        break;

    case "Long":
        Long lo = (Long) number;
        if (!OverFlowUtil.intOverflow(lo)) {
            throw new ArithmeticException("Overflow casting " + number + " to an int.");
        }
        parsed = lo.intValue();
        break;

    case "Float":
        Float fl = (Float) number;
        if (!OverFlowUtil.intOverflow(fl)) {
            throw new ArithmeticException("Overflow casting " + number + " to an int.");
        }
        parsed = fl.intValue();
        break;

    case "BigInteger":
        BigInteger bi = (BigInteger) number;
        if (!OverFlowUtil.intOverflow(bi)) {
            throw new ArithmeticException("Overflow casting " + number + " to an int.");
        }
        parsed = bi.intValue();
        break;

    case "BigDecimal":
        BigDecimal bd = (BigDecimal) number;
        if (!OverFlowUtil.intOverflow(bd)) {
            throw new ArithmeticException("Overflow casting " + number + " to an int.");
        }
        parsed = bd.intValue();
        break;

    case "Double":
        Double db = (Double) number;
        if (!OverFlowUtil.intOverflow(db)) {
            throw new ArithmeticException("Overflow casting " + number + " to an int.");
        }
        parsed = db.intValue();
        break;

    default:
        throw new IllegalArgumentException(
                "Invalid type: " + type + "\nData values " + "must extend the abstract Number class.");

    }

    return parsed;
}

From source file:org.globus.gsi.util.CertificateUtil.java

/**
 * Return CA Path constraint/*from ww w . j a v a2 s.  c o m*/
 *
 * @param crt
 * @return the CA path constraint
 * @throws IOException
 */
public static int getCAPathConstraint(TBSCertificateStructure crt) throws IOException {

    X509Extensions extensions = crt.getExtensions();
    if (extensions == null) {
        return -1;
    }
    X509Extension proxyExtension = extensions.getExtension(X509Extension.basicConstraints);
    if (proxyExtension != null) {
        BasicConstraints basicExt = getBasicConstraints(proxyExtension);
        if (basicExt.isCA()) {
            BigInteger pathLen = basicExt.getPathLenConstraint();
            return (pathLen == null) ? Integer.MAX_VALUE : pathLen.intValue();
        } else {
            return -1;
        }
    }
    return -1;
}

From source file:Main.java

/**
 * Computes the Window NAF (non-adjacent Form) of an integer.
 * @param width The width <code>w</code> of the Window NAF. The width is
 * defined as the minimal number <code>w</code>, such that for any
 * <code>w</code> consecutive digits in the resulting representation, at
 * most one is non-zero.//  w  w w.j  av  a 2s  . com
 * @param k The integer of which the Window NAF is computed.
 * @return The Window NAF of the given width, such that the following holds:
 * <code>k = &sum;<sub>i=0</sub><sup>l-1</sup> k<sub>i</sub>2<sup>i</sup>
 * </code>, where the <code>k<sub>i</sub></code> denote the elements of the
 * returned <code>byte[]</code>.
 */
public static byte[] generateWindowNaf(int width, BigInteger k) {
    if (width == 2) {
        return generateNaf(k);
    }

    if (width < 2 || width > 8) {
        throw new IllegalArgumentException("'width' must be in the range [2, 8]");
    }

    byte[] wnaf = new byte[k.bitLength() + 1];

    // 2^width and a mask and sign bit set accordingly
    int pow2 = 1 << width;
    int mask = pow2 - 1;
    int sign = pow2 >>> 1;

    boolean carry = false;
    int length = 0, pos = 0;

    while (pos <= k.bitLength()) {
        if (k.testBit(pos) == carry) {
            ++pos;
            continue;
        }

        k = k.shiftRight(pos);

        int digit = k.intValue() & mask;
        if (carry) {
            ++digit;
        }

        carry = (digit & sign) != 0;
        if (carry) {
            digit -= pow2;
        }

        length += (length > 0) ? pos - 1 : pos;
        wnaf[length++] = (byte) digit;
        pos = width;
    }

    // Reduce the WNAF array to its actual length
    if (wnaf.length > length) {
        wnaf = trim(wnaf, length);
    }

    return wnaf;
}

From source file:Main.java

/**
 * Computes the Window NAF (non-adjacent Form) of an integer.
 * @param width The width <code>w</code> of the Window NAF. The width is
 * defined as the minimal number <code>w</code>, such that for any
 * <code>w</code> consecutive digits in the resulting representation, at
 * most one is non-zero.//from   w w w .  j  av  a2s.  co m
 * @param k The integer of which the Window NAF is computed.
 * @return The Window NAF of the given width, such that the following holds:
 * <code>k = &sum;<sub>i=0</sub><sup>l-1</sup> k<sub>i</sub>2<sup>i</sup>
 * </code>, where the <code>k<sub>i</sub></code> denote the elements of the
 * returned <code>byte[]</code>.
 */
public static byte[] generateWindowNaf(int width, BigInteger k) {
    if (width == 2) {
        return generateNaf(k);
    }

    if (width < 2 || width > 8) {
        throw new IllegalArgumentException("'width' must be in the range [2, 8]");
    }
    if (k.signum() == 0) {
        return EMPTY_BYTES;
    }

    byte[] wnaf = new byte[k.bitLength() + 1];

    // 2^width and a mask and sign bit set accordingly
    int pow2 = 1 << width;
    int mask = pow2 - 1;
    int sign = pow2 >>> 1;

    boolean carry = false;
    int length = 0, pos = 0;

    while (pos <= k.bitLength()) {
        if (k.testBit(pos) == carry) {
            ++pos;
            continue;
        }

        k = k.shiftRight(pos);

        int digit = k.intValue() & mask;
        if (carry) {
            ++digit;
        }

        carry = (digit & sign) != 0;
        if (carry) {
            digit -= pow2;
        }

        length += (length > 0) ? pos - 1 : pos;
        wnaf[length++] = (byte) digit;
        pos = width;
    }

    // Reduce the WNAF array to its actual length
    if (wnaf.length > length) {
        wnaf = trim(wnaf, length);
    }

    return wnaf;
}

From source file:Main.java

public static int[] generateCompactWindowNaf(int width, BigInteger k) {
    if (width == 2) {
        return generateCompactNaf(k);
    }// ww w . j  a v a 2s .c o  m

    if (width < 2 || width > 16) {
        throw new IllegalArgumentException("'width' must be in the range [2, 16]");
    }
    if ((k.bitLength() >>> 16) != 0) {
        throw new IllegalArgumentException("'k' must have bitlength < 2^16");
    }

    int[] wnaf = new int[k.bitLength() / width + 1];

    // 2^width and a mask and sign bit set accordingly
    int pow2 = 1 << width;
    int mask = pow2 - 1;
    int sign = pow2 >>> 1;

    boolean carry = false;
    int length = 0, pos = 0;

    while (pos <= k.bitLength()) {
        if (k.testBit(pos) == carry) {
            ++pos;
            continue;
        }

        k = k.shiftRight(pos);

        int digit = k.intValue() & mask;
        if (carry) {
            ++digit;
        }

        carry = (digit & sign) != 0;
        if (carry) {
            digit -= pow2;
        }

        int zeroes = length > 0 ? pos - 1 : pos;
        wnaf[length++] = (digit << 16) | zeroes;
        pos = width;
    }

    // Reduce the WNAF array to its actual length
    if (wnaf.length > length) {
        wnaf = trim(wnaf, length);
    }

    return wnaf;
}