Example usage for java.math BigInteger negate

List of usage examples for java.math BigInteger negate

Introduction

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

Prototype

public BigInteger negate() 

Source Link

Document

Returns a BigInteger whose value is (-this) .

Usage

From source file:com.healthmarketscience.jackcess.impl.ColumnImpl.java

static BigDecimal toBigDecimal(byte[] bytes, boolean negate, int scale) {
    if ((bytes[0] & 0x80) != 0) {
        // the data is effectively unsigned, but the BigInteger handles it as
        // signed twos complement.  we need to add an extra byte to the input so
        // that it will be treated as unsigned
        bytes = ByteUtil.copyOf(bytes, 0, bytes.length + 1, 1);
    }/*from w ww  .j  a v  a  2 s  . c  o  m*/
    BigInteger intVal = new BigInteger(bytes);
    if (negate) {
        intVal = intVal.negate();
    }
    return new BigDecimal(intVal, scale);
}

From source file:mil.jpeojtrs.sca.util.AnyUtils.java

/**
 * @since 3.0//from w  w w .ja  v a2s  .  c  om
 * @throws NumberFormatException
 */
public static BigInteger bigIntegerDecode(final String nm) {
    int radix = AnyUtils.RADIX_DECIMAL;
    int index = 0;
    boolean negative = false;
    BigInteger result;

    // Handle minus sign, if present
    if (nm.startsWith("-")) {
        negative = true;
        index++;
    }

    // Handle radix specifier, if present
    if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
        index += 2;
        radix = AnyUtils.RADIX_HEX;
    } else if (nm.startsWith("#", index)) {
        index++;
        radix = AnyUtils.RADIX_HEX;
    } else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
        index++;
        radix = AnyUtils.RADIX_OCTAL;
    }

    if (nm.startsWith("-", index)) {
        throw new NumberFormatException("Negative sign in wrong position");
    }
    try {
        result = new BigInteger(nm.substring(index), radix);
        if (negative) {
            result = result.negate();
        }
    } catch (final NumberFormatException e) {
        // If number is Long.MIN_VALUE, we'll end up here. The next line
        // handles this case, and causes any genuine format error to be
        // rethrown.
        final String constant;
        if (negative) {
            constant = "-" + nm.substring(index);
        } else {
            constant = nm.substring(index);
        }
        result = new BigInteger(constant, radix);
    }
    return result;
}

From source file:net.pms.util.Rational.java

/**
 * Returns an instance with the given {@code numerator} and
 * {@code denominator}.//from   w ww  .  jav a  2s .com
 *
 * @param numerator the numerator.
 * @param denominator the denominator.
 * @return An instance that represents the value of {@code numerator}/
 *         {@code denominator}.
 */
@Nullable
public static Rational valueOf(@Nullable BigInteger numerator, @Nullable BigInteger denominator) {
    if (numerator == null || denominator == null) {
        return null;
    }
    if (numerator.signum() == 0 && denominator.signum() == 0) {
        return NaN;
    }
    if (denominator.signum() == 0) {
        return numerator.signum() > 0 ? POSITIVE_INFINITY : NEGATIVE_INFINITY;
    }
    if (numerator.signum() == 0) {
        return ZERO;
    }
    if (numerator.equals(denominator)) {
        return ONE;
    }
    if (denominator.signum() < 0) {
        numerator = numerator.negate();
        denominator = denominator.negate();
    }

    BigInteger reducedNumerator;
    BigInteger reducedDenominator;
    BigInteger greatestCommonDivisor = calculateGreatestCommonDivisor(numerator, denominator);
    if (BigInteger.ONE.equals(greatestCommonDivisor)) {
        reducedNumerator = numerator;
        reducedDenominator = denominator;
    } else {
        reducedNumerator = numerator.divide(greatestCommonDivisor);
        reducedDenominator = denominator.divide(greatestCommonDivisor);
    }
    return new Rational(numerator, denominator, greatestCommonDivisor, reducedNumerator, reducedDenominator);
}

From source file:com.healthmarketscience.jackcess.Column.java

/**
 * Decodes a NUMERIC field.//from w  w  w  .j  a v  a 2 s.c  o m
 */
private BigDecimal readNumericValue(ByteBuffer buffer) {
    boolean negate = (buffer.get() != 0);

    byte[] tmpArr = new byte[16];
    buffer.get(tmpArr);

    if (buffer.order() != ByteOrder.BIG_ENDIAN) {
        fixNumericByteOrder(tmpArr);
    }

    BigInteger intVal = new BigInteger(tmpArr);
    if (negate) {
        intVal = intVal.negate();
    }
    return new BigDecimal(intVal, getScale());
}

From source file:net.pms.util.Rational.java

/**
 * Returns a {@link Rational} whose value is {@code (this - value)}.
 *
 * @param value the value to be subtracted from this {@link Rational}.
 * @return The subtraction result./*from w  ww . java  2 s .  com*/
 */
@Nullable
public Rational subtract(@Nullable BigInteger value) {
    if (value == null) {
        return null;
    }

    return add(value.negate());
}

From source file:net.pms.util.Rational.java

/**
 * Returns a {@link Rational} whose value is {@code (this / value)}.
 *
 * @param value the value by which this {@link Rational} is to be divided.
 * @return The division result./*from w  w w.  j  a va2  s  .c  o m*/
 */
@Nullable
public Rational divide(@Nullable BigInteger value) {
    if (value == null) {
        return null;
    }
    if (isNaN()) {
        return NaN;
    }

    if (value.signum() == 0) {
        if (signum() == 0) {
            return NaN;
        }
        return signum() > 0 ? POSITIVE_INFINITY : NEGATIVE_INFINITY;
    }

    if (signum() == 0 || isInfinite() || BigInteger.ONE.equals(value.abs())) {
        return value.signum() < 0 ? negate() : this;
    }

    // Keep the sign in the numerator and the denominator positive
    if (value.signum() < 0) {
        return valueOf(reducedNumerator.negate(), reducedDenominator.multiply(value.negate()));
    }
    return valueOf(reducedNumerator, reducedDenominator.multiply(value));
}

From source file:org.ossie.properties.AnyUtils.java

/**
 * @since 3.0//from   ww w  .jav  a2  s. c  o  m
 */
public static BigInteger bigIntegerDecode(final String nm) throws NumberFormatException {
    int radix = 10;
    int index = 0;
    boolean negative = false;
    BigInteger result;

    // Handle minus sign, if present
    if (nm.startsWith("-")) {
        negative = true;
        index++;
    }

    // Handle radix specifier, if present
    if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
        index += 2;
        radix = 16;
    } else if (nm.startsWith("#", index)) {
        index++;
        radix = 16;
    } else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
        index++;
        radix = 8;
    }

    if (nm.startsWith("-", index)) {
        throw new NumberFormatException("Negative sign in wrong position");
    }
    try {
        result = new BigInteger(nm.substring(index), radix);
        result = (negative) ? result.negate() : result;
    } catch (final NumberFormatException e) {
        // If number is Long.MIN_VALUE, we'll end up here. The next line
        // handles this case, and causes any genuine format error to be
        // rethrown.
        final String constant = (negative) ? "-" + nm.substring(index) : nm.substring(index);
        result = new BigInteger(constant, radix);
    }
    return result;
}

From source file:org.ow2.aspirerfid.demos.warehouse.management.beg.CaptureReport.java

private void handleReports(ECReports reports) throws IOException, JAXBException {
    log.debug("**********************Handling incomming reports****************************");

    // get the current time and set the eventTime
    XMLGregorianCalendar now = null;
    try {//from w w  w.  j ava2s  .  co m
        DatatypeFactory dataFactory = DatatypeFactory.newInstance();
        now = dataFactory.newXMLGregorianCalendar(new GregorianCalendar());

        log.debug("Event Time:" + now.getHour() + ":" + now.getMinute() + ":" + ":" + now.getSecond() + "\n");

    } catch (DatatypeConfigurationException e) {
        e.printStackTrace();
    }

    List<ECReport> theReports = reports.getReports().getReport();
    // collect all the tags
    List<EPC> epcs = new LinkedList<EPC>();
    if (theReports != null) {
        for (ECReport report : theReports) {
            // log.debug("Report Count: "+report.getGroup().size());
            log.debug("***************Report Name:" + report.getReportName() + "**************");
            if (report.getGroup() != null) {
                for (ECReportGroup group : report.getGroup()) {

                    if (WarehouseManagement.getEntryDateTextField().equals("")) {
                        WarehouseManagement.setEntryDateTextField(
                                now.getDay() + "/" + now.getMonth() + "/" + now.getYear());
                    }
                    if (WarehouseManagement.getEntryHourTextField().equals("")) {
                        WarehouseManagement.setEntryHourTextField(
                                now.getHour() + ":" + now.getMinute() + ":" + now.getSecond());
                    }

                    WarehouseManagement.setZoneIDTextField(zoneID);
                    WarehouseManagement.setWarehouseIDTextField(warehouseID);

                    log.debug("Group Count: " + group.getGroupCount().getCount());
                    log.debug("Group Name: " + group.getGroupName());
                    if (group.getGroupList() != null) {
                        deliveredItem = null;

                        // warehousemen
                        if (group.getGroupName().equals(warehousemenGroupName)) {
                            for (ECReportGroupListMember member : group.getGroupList().getMember()) {
                                if (member.getEpc() != null) {
                                    WarehouseManagement
                                            .setUserIDTextField(member.getTag().getValue().split(":")[4]);
                                }
                            }
                        }

                        // Invoice
                        if (group.getGroupName().equals(invoiceGroupName)) {
                            for (ECReportGroupListMember member : group.getGroupList().getMember()) {
                                if (member.getEpc() != null) {
                                    WarehouseManagement
                                            .setInvoiceIDTextField(member.getTag().getValue().split(":")[4]);
                                    WarehouseManagement.setOfferingDateTextField("22/05/08");
                                    WarehouseManagement.setOfferingHourTextField("10:53:22");
                                }
                            }
                        }
                        //                     // Small Packets
                        //                     if (group.getGroupName().equals("urn:epc:pat:gid-96:145.56.*")) {
                        //                        for (ECReportGroupListMember member : group.getGroupList().getMember()) {
                        //                           if (member.getEpc() != null) {
                        //                              // WarehouseManagement.setInvoiceIDTextField(member.getTag().getValue().split(":")[4]);
                        //                           }
                        //                        }
                        //                     }
                        //                     // Medium Packets
                        //                     if (group.getGroupName().equals("urn:epc:pat:gid-96:145.87.*")) {
                        //                        for (ECReportGroupListMember member : group.getGroupList().getMember()) {
                        //                           if (member.getEpc() != null) {
                        //                              // WarehouseManagement.setInvoiceIDTextField(member.getTag().getValue().split(":")[4]);
                        //                           }
                        //                        }
                        //                     }

                        for (int i = 0; i < nOFmerchandise; i++) {

                            if (group.getGroupName().equals(packetsGroupName[i])) {
                                BigInteger quantity = new BigInteger(packetsQuantity[i]);
                                BigInteger expectedQuantity = new BigInteger(packetsExpectedQuantity[i]);
                                BigInteger quantityDelivered = new BigInteger(
                                        (group.getGroupCount().getCount()) + "");
                                BigInteger quantityRemain = quantity.add(quantityDelivered.negate());

                                for (ECReportGroupListMember member : group.getGroupList().getMember()) {
                                    if (member.getEpc() != null) {
                                        deliveredItem = new DeliveredItem();
                                        deliveredItem.setCompany(packetsCompany[i]);
                                        deliveredItem.setDeliveryDate(
                                                now.getDay() + "/" + now.getMonth() + "/" + now.getYear());
                                        deliveredItem.setDescription(packetsDescription[i]);
                                        deliveredItem.setExpectedQuantity(expectedQuantity);
                                        deliveredItem.setMeasurementID(packetsMeasurementID[i]);
                                        deliveredItem.setQuantity(quantity);
                                        deliveredItem.setQuantityDelivered(quantityDelivered);
                                        deliveredItem.setQuantityRemain(quantityRemain);
                                        deliveredItem.setItemCode(member.getTag().getValue().split(":")[4]);
                                        WarehouseManagement.updateDeliveryTableModel(deliveredItem);
                                        deliveredItem = null;
                                    }
                                }
                            }
                        }

                        // Print All
                        for (ECReportGroupListMember member : group.getGroupList().getMember()) {
                            if (member.getEpc() != null) {
                                log.debug("***Recieved Group Values***");
                                log.debug("RawDecimal Value: " + member.getRawDecimal().getValue());
                                {
                                    if (!(member.getEpc() == null))
                                        epcs.add(member.getEpc());
                                    log.debug("Epc Value: " + member.getEpc().getValue());
                                    if ((member.getEpc() == null))
                                        log.debug("Epc Value: null");
                                }
                                log.debug("RawHex Value: " + member.getRawHex().getValue());
                                log.debug("Tag Value: " + member.getTag().getValue());
                                // log.debug("Group
                                // Value:"+member.getExtension().getFieldList().toString());

                            }
                        }

                    }
                }
            }
        }
    }
    if (epcs.size() == 0) {
        log.debug("no epc received - generating no event");
        return;
    }
}

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 . ja v a2 s  .  c om
        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;
}