Example usage for java.math BigInteger subtract

List of usage examples for java.math BigInteger subtract

Introduction

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

Prototype

public BigInteger subtract(BigInteger val) 

Source Link

Document

Returns a BigInteger whose value is (this - val) .

Usage

From source file:org.apache.accumulo.core.client.mapreduce.RangeInputSplit.java

public static float getProgress(ByteSequence start, ByteSequence end, ByteSequence position) {
    int maxDepth = Math.min(Math.max(end.length(), start.length()), position.length());
    BigInteger startBI = new BigInteger(extractBytes(start, maxDepth));
    BigInteger endBI = new BigInteger(extractBytes(end, maxDepth));
    BigInteger positionBI = new BigInteger(extractBytes(position, maxDepth));
    return (float) (positionBI.subtract(startBI).doubleValue() / endBI.subtract(startBI).doubleValue());
}

From source file:Bytes.java

/**
 * Convert the specified amount into a human readable (though slightly less accurate)
 * result. IE:/*from  w  w  w.j a v  a  2s . 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:io.syndesis.model.integration.IntegrationRevision.java

public static IntegrationRevision deployedRevision(Integration integration) {
    BigInteger totalTimesUsed = integration.getTimesUsed().orElse(BigInteger.ZERO);
    BigInteger parentUses = integration.getRevisions().stream()
            .map(i -> i.getTimesUsed().orElse(BigInteger.ZERO)).reduce((n1, n2) -> n1.add(n2))
            .orElse(BigInteger.ZERO);

    return new IntegrationRevision.Builder()
            .createFrom(integration.getDeployedRevision()
                    .orElseGet(() -> IntegrationRevision.createNewRevision(integration)))
            .timesUsed(Optional.of(totalTimesUsed.subtract(parentUses))).lastUpdated(new Date()).build();

}

From source file:Util.java

public static BigInteger bitwiseGcd(BigInteger u, BigInteger v) {
    if (u.equals(BigInteger.ZERO))
        return v;
    if (v.equals(BigInteger.ZERO))
        return u;

    //      System.out.format("u=%s=%s\nu.getLowestSetBit()=%s\n%s>>%s=%s = %s\n", u, u.toString(2), u.getLowestSetBit(), u, u.getLowestSetBit(), u.shiftRight(u.getLowestSetBit()), u.shiftRight(u.getLowestSetBit()).toString(2));

    int uBit = u.getLowestSetBit();
    int vBit = v.getLowestSetBit();
    int k = (uBit <= vBit ? uBit : vBit);

    while (u.signum() > 0) {
        // first ensure that both are odd
        u = u.shiftRight(u.getLowestSetBit());
        v = v.shiftRight(v.getLowestSetBit());
        if (u.subtract(v).signum() >= 0) {
            u = (u.subtract(v)).shiftRight(1);
        } else {/*from   ww w.j av  a  2  s  .c  o  m*/
            v = (v.subtract(u)).shiftRight(1);
        }
    }

    return v.shiftLeft(k);
}

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

public static boolean checkLengthIPV4numberRange(String fromIPAddress, String toIPAddress) {
    BigInteger fromIP = ipv4ToNumber(fromIPAddress);
    BigInteger toIP = ipv4ToNumber(toIPAddress);
    BigInteger limit = toIP.subtract(fromIP);
    if (limit.compareTo(new BigInteger(MAX_NUMBER_RANGE)) == 1) {
        return false;
    }/*ww  w. j  a v a 2s.c o m*/
    return true;
}

From source file:org.multibit.utils.CSMiscUtils.java

public static boolean canSafelySpendWhileRespectingMigrationFee(BitcoinController bitcoinController,
        Wallet wallet, BigInteger amountSatoshis) {
    BigInteger migrationFee = calcMigrationFeeSatoshis(bitcoinController, wallet);
    if (migrationFee == null)
        return false;

    BigInteger availableBalance = wallet.getBalance(Wallet.BalanceType.AVAILABLE);
    BigInteger spendingLimit = availableBalance.subtract(migrationFee);

    //   System.out.println(">>>> Available     = "+availableBalance);
    //   System.out.println(">>>> Migration fee = "+migrationFee);
    //   System.out.println(">>>> Spending limit= "+spendingLimit);   
    //   System.out.println(">>>> Spend amount  = "+amountSatoshis);

    return (amountSatoshis.compareTo(spendingLimit) <= 0);
}

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

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

    BigInteger fromIP = ipv6ToNumber(fromIPAddress);
    BigInteger toIP = ipv6ToNumber(toIPAddress);
    BigInteger limit = toIP.subtract(fromIP);
    if (limit.compareTo(new BigInteger(MAX_NUMBER_RANGE)) == 1) {
        return false;
    }/*w  w  w  .j a  v a2s .  co m*/
    BigInteger subnet = new BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 16);

    fromIP = fromIP.shiftRight(128 - mask).shiftLeft(128 - mask);
    subnet = subnet.shiftRight(mask);

    BigInteger broadcastIP = fromIP.xor(subnet);

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

    return true;
}

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

public static String getPreviousIPv6(String ip) {
    if (ip == null) {
        return null;
    } else if (SubnetIPv6.isValidIPv6(ip)) {
        BigInteger address = new BigInteger(1, address(ip));
        if (address.equals(ADDRESS_MIN)) {
            return null;
        } else {/*from   w  ww. j  a v  a  2s. c  om*/
            address = address.subtract(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:io.syndesis.model.integration.IntegrationRevision.java

public static IntegrationRevision createNewRevision(Integration integration) {
    int version = integration.getRevisions().stream().map(i -> i.getVersion().orElse(0)).reduce(Integer::max)
            .orElse(0);//  w  w  w.j a va 2 s.c  o m

    BigInteger totalTimesUsed = integration.getTimesUsed().orElse(BigInteger.ZERO);
    BigInteger parentUses = integration.getRevisions().stream()
            .map(i -> i.getTimesUsed().orElse(BigInteger.ZERO)).reduce((n1, n2) -> n1.add(n2))
            .orElse(BigInteger.ZERO);

    return new IntegrationRevision.Builder().version(version + 1).parentVersion(version)
            .spec(new IntegrationRevisionSpec.Builder().configuration(integration.getConfiguration())
                    .connections(integration.getConnections()).steps(integration.getSteps()).build())
            .currentState(IntegrationRevisionState
                    .from(integration.getCurrentStatus().orElse(Integration.Status.Draft)))
            .currentMessage(integration.getStatusMessage())
            .targetState(IntegrationRevisionState
                    .from(integration.getDesiredStatus().orElse(Integration.Status.Draft)))
            .timesUsed(Optional.of(totalTimesUsed.subtract(parentUses)))
            //We retain the information found on the integration and we override when needed, why?
            //Because this is not called just when we want to create a new revision,
            //but can be used when editing the current one.
            .createdDate(integration.getCreatedDate()).lastUpdated(integration.getLastUpdated()).build();
}

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

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

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