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.kactech.otj.Utils.java

public static String base62Encode(BigInteger number) {
    if (number.compareTo(BigInteger.ZERO) == -1) { // number < 0
        throw new IllegalArgumentException("number must not be negative");
    }//from   w  ww  .  ja v a  2  s . c om
    StringBuilder result = new StringBuilder();
    while (number.compareTo(BigInteger.ZERO) == 1) { // number > 0
        BigInteger[] divmod = number.divideAndRemainder(B62_BASE);
        number = divmod[0];
        int digit = divmod[1].intValue();
        result.insert(0, B62_DIGITS.charAt(digit));
    }
    return (result.length() == 0) ? B62_DIGITS.substring(0, 1) : result.toString();
}

From source file:com.livinglogic.ul4.FunctionFormat.java

public static String call(BigInteger obj, String formatString, Locale locale) {
    IntegerFormat format = new IntegerFormat(formatString);

    if (locale == null)
        locale = Locale.ENGLISH;/*from   ww  w  . java 2 s . com*/

    String output = null;

    boolean neg = obj.signum() < 0;
    if (neg)
        obj = obj.negate();

    switch (format.type) {
    case 'b':
        output = obj.toString(2);
        break;
    case 'c':
        if (neg || obj.compareTo(new BigInteger("65535")) > 0)
            throw new RuntimeException("value out of bounds for c format");
        output = Character.toString((char) obj.intValue());
        break;
    case 'd':
        output = obj.toString();
        break;
    case 'o':
        output = obj.toString(8);
        break;
    case 'x':
        output = obj.toString(16);
        break;
    case 'X':
        output = obj.toString(16).toUpperCase();
        break;
    case 'n':
        // FIXME: locale formatting
        output = obj.toString();
        break;
    }
    return formatIntegerString(output, neg, format);
}

From source file:NumberInRange.java

public static boolean isInRange(Number number, BigInteger min, BigInteger max) {
    try {/*from w ww .j  av a2s .  com*/
        BigInteger bigInteger = null;
        if (number instanceof Byte || number instanceof Short || number instanceof Integer
                || number instanceof Long) {
            bigInteger = BigInteger.valueOf(number.longValue());
        } else if (number instanceof Float || number instanceof Double) {
            bigInteger = new BigDecimal(number.doubleValue()).toBigInteger();
        } else if (number instanceof BigInteger) {
            bigInteger = (BigInteger) number;
        } else if (number instanceof BigDecimal) {
            bigInteger = ((BigDecimal) number).toBigInteger();
        } else {
            // not a standard number
            bigInteger = new BigDecimal(number.doubleValue()).toBigInteger();
        }
        return max.compareTo(bigInteger) >= 0 && min.compareTo(bigInteger) <= 0;
    } catch (NumberFormatException e) {
        return false;
    }
}

From source file:com.ephesoft.dcma.tablefinder.share.TableExtractionResultModifierUtility.java

/**
 * Checks if new column data is in same alignment with previous column data
 * /*from  www. j a  v  a  2 s .com*/
 * @param prevColumnX0 {@link BigInteger}
 * @param prevColumnX1 {@link BigInteger}
 * @param columnX0 {@link BigInteger}
 * @param columnX1 {@link BigInteger}
 * @return {@code boolean} True if new column data value coordinates.
 */
private static boolean isColumnValidWithPrevColumnCoordinate(final BigInteger prevColumnX0,
        final BigInteger prevColumnX1, final BigInteger columnX0, final BigInteger columnX1) {
    LOGGER.trace("Entering method isColumnValidWithPrevColumnCoordinate.....");
    boolean isValid = false;
    LOGGER.debug("New Column Coordinates: X0=", columnX0, " ,X1=", columnX1);
    LOGGER.debug("Previous column Coordinates: X0=", prevColumnX0, " ,X1=", prevColumnX1);
    if (prevColumnX0 != null && prevColumnX1 != null && columnX0 != null && columnX0 != null) {
        if ((columnX0.compareTo(prevColumnX0) == 1 && columnX0.compareTo(prevColumnX1) == -1)
                || (columnX1.compareTo(prevColumnX0) == 1 && columnX1.compareTo(prevColumnX1) == -1)
                || (columnX0.compareTo(prevColumnX0) == -1 && columnX1.compareTo(prevColumnX1) == 1)
                || columnX0.compareTo(prevColumnX0) == 0 || columnX1.compareTo(prevColumnX1) == 0
                || TableRowFinderUtility.isApproxEqual(columnX1.intValue(), prevColumnX0.intValue())
                || TableRowFinderUtility.isApproxEqual(columnX0.intValue(), prevColumnX1.intValue())) {
            isValid = true;
        }
    }
    LOGGER.info("Is value coordinates valid with column coordinates : ", isValid);
    LOGGER.trace("Exiting method isColumnValidWithColCoord.....");
    return isValid;
}

From source file:com.amazonaws.services.kinesis.clientlibrary.lib.worker.ShardSyncer.java

private static synchronized void assertHashRangeOfClosedShardIsCovered(Shard closedShard,
        Map<String, Shard> shardIdToShardMap, Set<String> childShardIds) throws KinesisClientLibIOException {

    BigInteger startingHashKeyOfClosedShard = new BigInteger(
            closedShard.getHashKeyRange().getStartingHashKey());
    BigInteger endingHashKeyOfClosedShard = new BigInteger(closedShard.getHashKeyRange().getEndingHashKey());
    BigInteger minStartingHashKeyOfChildren = null;
    BigInteger maxEndingHashKeyOfChildren = null;

    for (String childShardId : childShardIds) {
        Shard childShard = shardIdToShardMap.get(childShardId);
        BigInteger startingHashKey = new BigInteger(childShard.getHashKeyRange().getStartingHashKey());
        if ((minStartingHashKeyOfChildren == null)
                || (startingHashKey.compareTo(minStartingHashKeyOfChildren) < 0)) {
            minStartingHashKeyOfChildren = startingHashKey;
        }/* w  w w . j  a  v a2  s. c o m*/
        BigInteger endingHashKey = new BigInteger(childShard.getHashKeyRange().getEndingHashKey());
        if ((maxEndingHashKeyOfChildren == null) || (endingHashKey.compareTo(maxEndingHashKeyOfChildren) > 0)) {
            maxEndingHashKeyOfChildren = endingHashKey;
        }
    }

    if ((minStartingHashKeyOfChildren == null) || (maxEndingHashKeyOfChildren == null)
            || (minStartingHashKeyOfChildren.compareTo(startingHashKeyOfClosedShard) > 0)
            || (maxEndingHashKeyOfChildren.compareTo(endingHashKeyOfClosedShard) < 0)) {
        throw new KinesisClientLibIOException("Incomplete shard list: hash key range of shard "
                + closedShard.getShardId() + " is not covered by its child shards.");
    }

}

From source file:com.wms.utils.DataUtil.java

public static boolean checkValidateIPv4(String fromIPAddress, String toIPAddress, int mask) {

    BigInteger fromIP = ipv4ToNumber(fromIPAddress);
    BigInteger toIP = ipv4ToNumber(toIPAddress);
    BigInteger subnet = new BigInteger("FFFFFFFF", 16);

    fromIP = fromIP.shiftRight(32 - mask).shiftLeft(32 - mask);
    subnet = subnet.shiftRight(mask);// ww w .j  av  a  2 s .co m

    BigInteger broadcastIP = fromIP.xor(subnet);

    if (toIP.compareTo(broadcastIP) == 1) {
        return false;
    }

    return true;
}

From source file:org.dbrain.data.jackson.serializers.JsonBigDecimalSerializer.java

@Override
public void serialize(BigDecimal value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
    if (value != null) {
        // Does it have more that 15 significant digits ?
        BigInteger unscaled = value.unscaledValue();
        if (unscaled.compareTo(MIN_VALUE) >= 0 && unscaled.compareTo(MAX_VALUE) <= 0) {
            jgen.writeNumber(value);//from  w  w  w  .  j a v a  2 s.c  om
        } else {
            jgen.writeString(value.toString());
        }
    } else {
        jgen.writeNull();
    }
}

From source file:com.redhat.lightblue.crud.validator.MinMaxChecker.java

private int cmp(BigInteger nodeValue, BigInteger fieldValue) {
    return nodeValue.compareTo(fieldValue);
}

From source file:org.opensingular.form.type.core.STypeLong.java

@Override
protected Long convertNotNativeNotString(Object valor) {
    if (valor instanceof Number) {
        BigInteger bigIntegerValue = new BigInteger(String.valueOf(valor));
        if (bigIntegerValue.compareTo(new BigInteger(String.valueOf(Long.MAX_VALUE))) > 0) {
            throw createConversionError(valor, Long.class, " Valor muito grande.", null);
        }//w w w  . jav a2 s. c o  m
        if (bigIntegerValue.compareTo(new BigInteger(String.valueOf(Long.MIN_VALUE))) < 0) {
            throw createConversionError(valor, Long.class, " Valor muito pequeno.", null);
        }
        return bigIntegerValue.longValue();
    }
    throw createConversionError(valor);
}

From source file:org.btc4j.ws.BtcDaemonServiceTest.java

@Test
public void getConnectionCount() throws BtcWsException {
    System.out.println("BtcDaemonServiceTest.getConnectionCount");
    BigInteger connections = BTCWS_SVC.getConnectionCount();
    assertTrue(connections.compareTo(BigInteger.ZERO) >= 0);
}