Example usage for java.math BigInteger compareTo

List of usage examples for java.math BigInteger compareTo

Introduction

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

Prototype

public int compareTo(BigInteger val) 

Source Link

Document

Compares this BigInteger with the specified BigInteger.

Usage

From source file:com.amazonaws.services.kinesis.clientlibrary.proxies.KinesisLocalFileProxy.java

@Override
public GetRecordsResult get(String serializedKinesisIterator, int maxRecords)
        throws ResourceNotFoundException, InvalidArgumentException, ExpiredIteratorException {
    IteratorInfo iterator = deserializeIterator(serializedKinesisIterator);

    BigInteger startingPosition = new BigInteger(iterator.sequenceNumber);
    BigInteger lastRecordsSeqNo = BigInteger.ONE;
    List<Record> recordsToReturn = new ArrayList<Record>();
    List<Record> shardRecords = shardedDataRecords.get(iterator.shardId);
    if (shardRecords == null) {
        throw new ResourceNotFoundException(iterator.shardId + " does not exist");
    }// ww w.  j a  v a  2  s.  co  m

    boolean isHasMoreShards = false;

    for (int i = 0; i < shardRecords.size(); i++) {
        Record record = shardRecords.get(i);
        BigInteger recordSequenceNumber = new BigInteger(record.getSequenceNumber());
        // update lastRecordsSeqNo so if we return no records, it will be the seqNo of the last record.
        lastRecordsSeqNo = recordSequenceNumber;
        if (recordSequenceNumber.compareTo(startingPosition) >= 0) {
            // Set endIndex (of sublist) to cap at either maxRecords or end of list.
            int endIndex = Math.min(i + maxRecords, shardRecords.size());
            recordsToReturn.addAll(shardRecords.subList(i, endIndex));

            lastRecordsSeqNo = new BigInteger(shardRecords.get(endIndex - 1).getSequenceNumber());
            if (endIndex < shardRecords.size()) {
                isHasMoreShards = true;
            }

            break;
        }
    }

    GetRecordsResult response = new GetRecordsResult();
    response.setRecords(recordsToReturn);

    // Set iterator only if the shard is not closed.
    if (isHasMoreShards || (!closedShards.contains(iterator.shardId))) {
        /*
         * Use the sequence number of the last record returned + 1 to compute the next iterator.
         */
        response.setNextShardIterator(
                serializeIterator(iterator.shardId, lastRecordsSeqNo.add(BigInteger.ONE).toString()));
        LOG.debug("Returning a non null iterator for shard " + iterator.shardId);
    } else {
        LOG.info("Returning null iterator for shard " + iterator.shardId);
    }

    return response;
}

From source file:cn.iie.haiep.hbase.value.Bytes.java

/**
 * Iterate over keys within the passed inclusive range.
 */// w ww.ja v a2s  . c  o  m
public static Iterable<byte[]> iterateOnSplits(final byte[] a, final byte[] b, final int num) {
    byte[] aPadded;
    byte[] bPadded;
    if (a.length < b.length) {
        aPadded = padTail(a, b.length - a.length);
        bPadded = b;
    } else if (b.length < a.length) {
        aPadded = a;
        bPadded = padTail(b, a.length - b.length);
    } else {
        aPadded = a;
        bPadded = b;
    }
    if (compareTo(aPadded, bPadded) >= 0) {
        throw new IllegalArgumentException("b <= a");
    }
    if (num <= 0) {
        throw new IllegalArgumentException("num cannot be < 0");
    }
    byte[] prependHeader = { 1, 0 };
    final BigInteger startBI = new BigInteger(add(prependHeader, aPadded));
    final BigInteger stopBI = new BigInteger(add(prependHeader, bPadded));
    final BigInteger diffBI = stopBI.subtract(startBI);
    final BigInteger splitsBI = BigInteger.valueOf(num + 1);
    if (diffBI.compareTo(splitsBI) < 0) {
        return null;
    }
    final BigInteger intervalBI;
    try {
        intervalBI = diffBI.divide(splitsBI);
    } catch (Exception e) {
        LOG.error("Exception caught during division", e);
        return null;
    }

    final Iterator<byte[]> iterator = new Iterator<byte[]>() {
        private int i = -1;

        @Override
        public boolean hasNext() {
            return i < num + 1;
        }

        @Override
        public byte[] next() {
            i++;
            if (i == 0)
                return a;
            if (i == num + 1)
                return b;

            BigInteger curBI = startBI.add(intervalBI.multiply(BigInteger.valueOf(i)));
            byte[] padded = curBI.toByteArray();
            if (padded[1] == 0)
                padded = tail(padded, padded.length - 2);
            else
                padded = tail(padded, padded.length - 1);
            return padded;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }

    };

    return new Iterable<byte[]>() {
        @Override
        public Iterator<byte[]> iterator() {
            return iterator;
        }
    };
}

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

@Override
public Complex multiply(BigInteger bg) {
    return bg.compareTo(BigInteger.ONE) == 0 ? this : new Complex(real.multiply(bg), imaginary.multiply(bg));
}

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

@Override
public Complex divide(BigInteger bg) {
    return bg.compareTo(BigInteger.ONE) == 0 ? this : new Complex(real.divide(bg), imaginary.divide(bg));
}

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

@Override
public Complex pow(BigInteger exponent) {
    if (exponent.compareTo(BigInteger.ZERO) < 0)
        return reciprocal().pow(exponent.negate());

    Complex result = Complex.ONE;//  www .j av  a2  s .c  o m
    Complex k2p = this;
    while (!BigInteger.ZERO.equals(exponent)) {
        if (exponent.testBit(0))
            result = result.multiply(k2p);

        k2p = k2p.multiply(k2p);
        exponent = exponent.shiftRight(1);
    }

    return result;
}

From source file:com.keylesspalace.tusky.fragment.NotificationsFragment.java

private boolean isBiggerThan(BigInteger newId, BigInteger lastShownNotificationId) {
    return lastShownNotificationId.compareTo(newId) < 0;
}

From source file:com.cloud.utils.net.NetUtils.java

public static BigInteger countIp6InRange(final String ip6Range) {
    if (ip6Range == null) {
        return null;
    }/*from w ww  .  j a  v  a  2s  .co m*/
    final String[] ips = ip6Range.split("-");
    final String startIp = ips[0];
    String endIp = ips[0];
    if (ips.length > 1) {
        endIp = ips[1];
    }
    try {
        final BigInteger startInt = convertIPv6AddressToBigInteger(IPv6Address.fromString(startIp));
        final BigInteger endInt = convertIPv6AddressToBigInteger(IPv6Address.fromString(endIp));
        if (endInt != null && startInt != null && startInt.compareTo(endInt) <= 0) {
            return endInt.subtract(startInt).add(BigInteger.ONE);
        }
    } catch (final IllegalArgumentException ex) {
        s_logger.error("Failed to convert a string to an IPv6 address", ex);
    }
    return null;
}

From source file:org.jbpm.formModeler.core.processing.formRendering.FormRenderingFormatter.java

protected BigInteger calculateMax(List colspans) {
    BigInteger max = new BigInteger("0");
    for (int i = 0; i < colspans.size(); i++) {
        BigInteger number = (BigInteger) colspans.get(i);
        max = max.compareTo(number) < 0 ? number : max;
    }/*from   w w w  . j  av a2s .  c  om*/
    return max;
}

From source file:org.tolven.config.model.CredentialManager.java

private BigInteger getNextSerialNumber() {
    byte[] randomLong = new byte[8];
    getSecureRandom().nextBytes(randomLong);
    BigInteger serialNumber = new BigInteger(randomLong);
    if (serialNumber.compareTo(BigInteger.valueOf(0)) > 0) {
        return serialNumber;
    } else if (serialNumber.compareTo(BigInteger.valueOf(0)) < 0) {
        return BigInteger.valueOf(0).subtract(serialNumber);
    } else {//from   w  w w . j  ava  2 s.  c om
        return BigInteger.valueOf(1).add(serialNumber);
    }
}

From source file:edu.ku.brc.af.core.db.AutoNumberGeneric.java

/**
 * Returns the increment portion as an Integer.
 * @param formatter the formatter//w w w.  j av a2  s  .co m
 * @param highestValue the value that the incrementer portion will be extracted from (this value came from the database.
 * @param formValue value from the form
 * @return the integer portion for the incrementer part
 */
protected Pair<BigInteger, BigInteger> getYearAndIncVal(final UIFieldFormatterIFace formatter,
        final String highestValue, final String formValue) {

    UIFieldFormatterField yearField = formatter.getYear();
    boolean isByYear = yearField != null && yearField.isByYear();

    BigInteger valToBeInc = null;
    BigInteger yearToUse = null;
    String strToUseForInc = highestValue; // This is the string where the incrementer value will be extracted from

    if (isByYear) {
        BigInteger highestYear = getYearValue(formatter, highestValue);
        BigInteger formYear = getYearValue(formatter, formValue);

        boolean calcNewValue = false; // Caused by year changing

        if (highestYear != null && formYear != null) {
            // Since the Form Value had a good 'Year' number we will always use that
            // and only when it is greater than the database number do we calculate a new value
            yearToUse = formYear;

            if (formYear.compareTo(highestYear) > 0) {
                calcNewValue = true;
            }

        } else if (highestYear != null && formYear == null) {
            // The form value was empty or bad form some reason
            // use the database's value for year
            yearToUse = highestYear;

        } else if (highestYear == null && formYear != null) {
            // Here the database might have been empty
            // so we will use the form's value
            // and we will need to calculate a brand new increment number
            yearToUse = formYear;
            calcNewValue = true;

        } else {
            // Here both are bad so use the current year and 
            // calculate a brand new increment number
            Calendar cal = Calendar.getInstance();
            yearToUse = new BigInteger(String.valueOf(cal.get(Calendar.YEAR)));

            // If this is a "by Year" and the year was null for both than we need to start from scratch
            if (isByYear) {
                strToUseForInc = null;
                calcNewValue = true;
            }
        }

        // Now get the incrementer number portion if we aren't creating a new number
        if (!calcNewValue) {
            if (strToUseForInc == null) {
                valToBeInc = new BigInteger("0");
            } else {
                valToBeInc = extractIntegerValue(formatter.getIncPosition(), strToUseForInc);
            }
        } else {
            valToBeInc = new BigInteger("0");
        }
    } else {
        if (formatter.getYear() != null) {
            yearToUse = getYearValue(formatter, formValue);
        }

        if (StringUtils.isNotEmpty(highestValue)) {
            valToBeInc = extractIntegerValue(formatter.getIncPosition(), strToUseForInc);
        } else {
            valToBeInc = new BigInteger("0");
        }
    }

    return new Pair<BigInteger, BigInteger>(yearToUse, valToBeInc);
}