Example usage for java.math BigInteger abs

List of usage examples for java.math BigInteger abs

Introduction

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

Prototype

public BigInteger abs() 

Source Link

Document

Returns a BigInteger whose value is the absolute value of this BigInteger.

Usage

From source file:org.apache.cassandra.utils.FBUtilities.java

public static BigInteger hashToBigInteger(ByteBuffer data) {
    byte[] result = hash(data);
    BigInteger hash = new BigInteger(result);
    return hash.abs();
}

From source file:org.apache.usergrid.persistence.cassandra.SimpleIndexBucketLocatorImpl.java

/** Get the next token in the ring for this big int. */
private String getClosestToken(UUID entityId) {
    BigInteger location = new BigInteger(md5(bytes(entityId)));
    location = location.abs();

    int index = Collections.binarySearch(buckets, location);

    if (index < 0) {
        index = (index + 1) * -1;//from   w  w w  .j  a va  2s. co  m
    }

    // mod if we need to wrap
    index = index % size;

    return bucketsString.get(index);
}

From source file:org.sparkbit.jsonrpc.SparkBitJSONRPCServiceImpl.java

private ArrayList<JSONRPCTransactionAmount> getAssetTransactionAmounts(Wallet wallet, Transaction tx,
        boolean excludeBTCFee, boolean absoluteBTCFee) {
    if (wallet == null || tx == null)
        return null;

    Map<Integer, BigInteger> receiveMap = wallet.CS.getAssetsSentToMe(tx);
    Map<Integer, BigInteger> sendMap = wallet.CS.getAssetsSentFromMe(tx);

    //   System.out.println(">>>> tx = " + tx.getHashAsString());
    //   System.out.println(">>>>     receive map = " +  receiveMap);
    //   System.out.println(">>>>     send map = " +  sendMap);

    //Map<String, String> nameAmountMap = new TreeMap<>();
    ArrayList<JSONRPCTransactionAmount> resultList = new ArrayList<>();

    boolean isSentByMe = tx.sent(wallet);
    Map<Integer, BigInteger> loopMap = (isSentByMe) ? sendMap : receiveMap;

    //   Integer assetID = null;
    BigInteger netAmount = null;//w  w w .  j av  a2 s .  c  o m

    //   for (Map.Entry<Integer, BigInteger> entry : loopMap.entrySet()) {
    for (Integer assetID : loopMap.keySet()) {
        //       assetID = entry.getKey();
        if (assetID == null || assetID == 0)
            continue; // skip bitcoin

        BigInteger receivedAmount = receiveMap.get(assetID); // should be number of raw units
        BigInteger sentAmount = sendMap.get(assetID);
        boolean isReceivedAmountMissing = (receivedAmount == null);
        boolean isSentAmountMissing = (sentAmount == null);

        netAmount = BigInteger.ZERO;
        if (!isReceivedAmountMissing)
            netAmount = netAmount.add(receivedAmount);
        if (!isSentAmountMissing)
            netAmount = netAmount.subtract(sentAmount);

        if (isSentByMe && !isSentAmountMissing && sentAmount.equals(BigInteger.ZERO)) {
            // Catch a case where for a send transaction, the send amount for an asset is 0,
            // but the receive cmount is not 0.  Also the asset was not valid.
            continue;
        }

        CSAsset asset = wallet.CS.getAsset(assetID);
        if (asset == null) {
            // something went wrong, we have asset id but no asset, probably deleted.
            // For now, we carry on, and we display what we know.
        }

        if (netAmount.equals(BigInteger.ZERO) && isSentByMe) {
            // If net asset is 0 and this is our send transaction,
            // we don't need to show anything, as this probably due to implicit transfer.
            // So continue the loop.
            continue;
        }

        if (netAmount.equals(BigInteger.ZERO) && !isSentByMe) {
            // Receiving an asset, where the value is 0 because its not confirmed yet,
            // or not known because asset files not uploaded so we dont know display format.
            // Anyway, we don't do anything here as we do want to display this incoming
            // transaction the best we can.
        }

        //       System.out.println(">>>>     isSentAmountMissing = " + isSentAmountMissing);
        //       System.out.println(">>>>     asset reference = " + asset.getAssetReference());
        //       System.out.println(">>>>     asset name = " + asset.getName());

        String name = null;
        CoinSparkGenesis genesis = null;
        boolean isUnknown = false;
        if (asset != null) {
            genesis = asset.getGenesis();
            name = asset.getNameShort(); // could return null?
        }
        if (name == null) {
            isUnknown = true;
            if (genesis != null) {
                name = "Asset from " + genesis.getDomainName();
            } else {
                // No genesis block found yet
                name = "Other Asset";
            }
        }

        String s1 = null;
        if (asset == null || isUnknown == true || (netAmount.equals(BigInteger.ZERO) && !isSentByMe)) {
            // We don't have formatting details since asset is unknown or deleted
            // If there is a quantity, we don't display it since we don't have display format info
            // Of if incoming asset transfer, unconfirmed, it will be zero, so show ... instead
            s1 = "...";
        } else {
            BigDecimal displayUnits = CSMiscUtils.getDisplayUnitsForRawUnits(asset, netAmount);
            s1 = CSMiscUtils.getFormattedDisplayString(asset, displayUnits);
        }

        //
        // Create JSONRPCTransactionAmount and add it to list
        // 
        String fullName = "";
        String assetRef = "";
        if (asset != null) {
            fullName = asset.getName();
            if (fullName == null)
                fullName = name; // use short name
            assetRef = CSMiscUtils.getHumanReadableAssetRef(asset);
        }
        BigDecimal displayQty = CSMiscUtils.getDisplayUnitsForRawUnits(asset, netAmount);
        JSONRPCTransactionAmount amount = new JSONRPCTransactionAmount();
        amount.setAsset_ref(assetRef);
        amount.setDisplay(s1);
        amount.setName(fullName);
        amount.setName_short(name);
        amount.setQty(displayQty.doubleValue());
        amount.setRaw(netAmount.longValue());
        resultList.add(amount);
    }

    BigInteger satoshiAmount = receiveMap.get(0);
    satoshiAmount = satoshiAmount.subtract(sendMap.get(0));

    // We will show the fee separately so no need to include here.
    if (excludeBTCFee && isSentByMe) {
        BigInteger feeSatoshis = tx.calculateFee(wallet); // returns positive
        if (absoluteBTCFee) {
            satoshiAmount = satoshiAmount.abs().subtract(feeSatoshis);
        } else {
            satoshiAmount = satoshiAmount.add(feeSatoshis);
        }
    }

    String btcAmount = Utils.bitcoinValueToFriendlyString(satoshiAmount) + " BTC";
    BigDecimal satoshiAmountBTC = new BigDecimal(satoshiAmount).divide(new BigDecimal(Utils.COIN));
    JSONRPCTransactionAmount amount = new JSONRPCTransactionAmount();
    amount.setAsset_ref("bitcoin");
    amount.setDisplay(btcAmount);
    amount.setName("Bitcoin");
    amount.setName_short("Bitcoin");
    amount.setQty(satoshiAmountBTC.doubleValue());
    amount.setRaw(satoshiAmount.longValue());
    resultList.add(amount);

    return resultList;
}

From source file:org.trnltk.numeral.DigitsToTextConverter.java

public String convert(String digits) {
    if (StringUtils.isBlank(digits))
        return null;

    digits = digits.replaceAll(GROUPING_SEPARATOR_REGEX, StringUtils.EMPTY);

    if (!TURKISH_NUMBER_PATTERN.matcher(digits).matches())
        throw new IllegalArgumentException("'" + digits
                + "' is not a valid Turkish number. Allowed pattern is : " + TURKISH_NUMBER_PATTERN.pattern());

    String strIntegerPart, strFractionPart;

    if (digits.contains(FRACTION_SEPARATOR)) {
        strIntegerPart = digits.substring(0, digits.indexOf(FRACTION_SEPARATOR));
        strFractionPart = digits.substring(digits.indexOf(FRACTION_SEPARATOR) + 1);
    } else {/*from   www .j a v  a 2  s  .c  o m*/
        strIntegerPart = digits;
        strFractionPart = null;
    }

    boolean isPositive = true;
    if (strIntegerPart.startsWith(POSITIVE_SIGN)) {
        isPositive = true;
        strIntegerPart = strIntegerPart.substring(1);
    }
    if (strIntegerPart.startsWith(NEGATIVE_SIGN)) {
        isPositive = false;
        strIntegerPart = strIntegerPart.substring(1);
    }

    BigInteger integerPart = new BigInteger(strIntegerPart);
    BigInteger fractionPart = StringUtils.isNotBlank(strFractionPart) ? new BigInteger(strFractionPart)
            : BigInteger.ZERO;

    if (!isPositive)
        integerPart = integerPart.negate();

    String wordIntegerPart = this.convertNaturalNumberToWords(integerPart.abs());
    String wordFractionPart = this.convertNaturalNumberToWords(fractionPart);

    wordIntegerPart = this.addTextForLeadingZeros(strIntegerPart, wordIntegerPart);
    wordFractionPart = StringUtils.isNotBlank(strFractionPart)
            ? this.addTextForLeadingZeros(strFractionPart, wordFractionPart)
            : wordFractionPart;

    if (integerPart.compareTo(ZERO) < 0)
        wordIntegerPart = MINUS_NAME + " " + wordIntegerPart;

    if (digits.contains(FRACTION_SEPARATOR))
        return wordIntegerPart + " " + COMMA_NAME + " " + wordFractionPart;
    else
        return wordIntegerPart;
}

From source file:org.usergrid.persistence.cassandra.SimpleIndexBucketLocatorImpl.java

/**
 * Get the next token in the ring for this big int.
 * /*from w  w  w  .  j  a  v a  2  s. c o m*/
 * @param entityId
 * @return
 */
private String getClosestToken(UUID entityId) {
    BigInteger location = new BigInteger(md5(bytes(entityId)));
    location = location.abs();

    int index = Collections.binarySearch(buckets, location);

    if (index < 0) {
        index = (index + 1) * -1;
    }

    // mod if we need to wrap
    index = index % size;

    return bucketsString.get(index);

}

From source file:piuk.blockchain.android.util.WalletUtils.java

public static String formatValue(final BigInteger value, final String plusSign, final String minusSign) {
    final boolean negative = value.compareTo(BigInteger.ZERO) < 0;
    final BigInteger absValue = value.abs();

    final String sign = negative ? minusSign : plusSign;

    final int coins = absValue.divide(Utils.COIN).intValue();
    final int cents = absValue.remainder(Utils.COIN).intValue();

    if (cents % 1000000 == 0)
        return String.format("%s%d.%02d", sign, coins, cents / 1000000);
    else if (cents % 10000 == 0)
        return String.format("%s%d.%04d", sign, coins, cents / 10000);
    else/*from  w w  w  .j  av a 2 s  .co  m*/
        return String.format("%s%d.%08d", sign, coins, cents);
}