Example usage for java.math BigDecimal compareTo

List of usage examples for java.math BigDecimal compareTo

Introduction

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

Prototype

@Override
public int compareTo(BigDecimal val) 

Source Link

Document

Compares this BigDecimal with the specified BigDecimal .

Usage

From source file:edu.usu.sdl.openstorefront.validation.MaxValueRule.java

@Override
protected boolean validate(Field field, Object dataObject) {
    boolean valid = true;

    Max max = field.getAnnotation(Max.class);
    if (max != null) {
        try {//from w w  w.  ja v  a  2s . co  m
            String value = BeanUtils.getProperty(dataObject, field.getName());
            if (value != null) {
                try {
                    BigDecimal numberValue = new BigDecimal(value);
                    if (numberValue.compareTo(BigDecimal.valueOf(max.value())) == 1) {
                        valid = false;
                    }
                } catch (NumberFormatException e) {
                    throw new OpenStorefrontRuntimeException("This annotation is for numbers only",
                            "Programming error");
                }
            }
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
            throw new OpenStorefrontRuntimeException("Unexpected error occur trying to get value from object.",
                    ex);
        }
    }
    return valid;
}

From source file:edu.usu.sdl.openstorefront.validation.MinValueRule.java

@Override
protected boolean validate(Field field, Object dataObject) {
    boolean valid = true;

    Min min = field.getAnnotation(Min.class);
    if (min != null) {
        try {/*from   ww  w  .ja v a 2 s . c  o m*/
            String value = BeanUtils.getProperty(dataObject, field.getName());
            if (value != null) {
                try {
                    BigDecimal numberValue = new BigDecimal(value);
                    if (numberValue.compareTo(BigDecimal.valueOf(min.value())) == -1) {
                        valid = false;
                    }
                } catch (NumberFormatException e) {
                    throw new OpenStorefrontRuntimeException("This annotation is for numbers only",
                            "Programming error");
                }
            }
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
            throw new OpenStorefrontRuntimeException("Unexpected error occur trying to get value from object.",
                    ex);
        }
    }
    return valid;
}

From source file:net.groupbuy.service.impl.MemberRankServiceImpl.java

@Transactional(readOnly = true)
public boolean amountUnique(BigDecimal previousAmount, BigDecimal currentAmount) {
    if (previousAmount != null && previousAmount.compareTo(currentAmount) == 0) {
        return true;
    } else {//from w w  w.ja  v a2  s .co m
        if (memberRankDao.amountExists(currentAmount)) {
            return false;
        } else {
            return true;
        }
    }
}

From source file:com.dp2345.service.impl.ShopRankServiceImpl.java

@Transactional(readOnly = true)
public boolean amountUnique(BigDecimal previousAmount, BigDecimal currentAmount) {
    if (previousAmount != null && previousAmount.compareTo(currentAmount) == 0) {
        return true;
    } else {//from  www  .j a va2s  . c  o m
        if (shopRankDao.amountExists(currentAmount)) {
            return false;
        } else {
            return true;
        }
    }
}

From source file:org.springmodules.validation.util.condition.range.NumberAwareComparableComparator.java

/**
 * Compares the two given numbers. These numbers are compared regardless of
 * their type.//from   w ww . jav  a 2 s.c o  m
 * 
 * @param n1
 *            The first number.
 * @param n2
 *            The second number.
 * @return possitive number if <code>n1 &gt; n2</code>, negative number if
 *         <code>n1 &lt; n2</code>, or 0 (Zero) if <code>n1 == n2</code>.
 */
protected int compareNumbers(Number n1, Number n2) {
    BigDecimal bd1 = new BigDecimal(n1.toString());
    BigDecimal bd2 = new BigDecimal(n2.toString());
    return bd1.compareTo(bd2);
}

From source file:es.logongas.encuestas.modelo.resultados.InferenciaEstadistica.java

public InferenciaEstadistica(EstadisticaDescriptiva estadisticaDescriptiva, BigDecimal nivelConfianza,
        int numDecimals) {
    this.numDecimals = numDecimals;
    if (nivelConfianza.compareTo(BigDecimal.ZERO) <= 0) {
        throw new IllegalArgumentException("El nivelConfianza debe ser mayor que 0");
    }//from   w  ww  .  ja v  a2s.  co m
    if (nivelConfianza.compareTo(BigDecimal.ONE) >= 0) {
        throw new IllegalArgumentException("El nivelConfianza debe ser menor que 1");
    }

    TDistribution tDistribution = new TDistribution(estadisticaDescriptiva.getNumMuestras() - 1);
    double t = tDistribution.inverseCumulativeProbability(nivelConfianza.doubleValue());
    BigDecimal delta = new BigDecimal(t * (estadisticaDescriptiva.getDesviacionEstandar().doubleValue()
            / Math.sqrt(estadisticaDescriptiva.getNumMuestras())));

    BigDecimal min = estadisticaDescriptiva.getMedia().subtract(delta).setScale(this.numDecimals,
            RoundingMode.HALF_UP);
    BigDecimal max = estadisticaDescriptiva.getMedia().add(delta).setScale(this.numDecimals,
            RoundingMode.HALF_UP);
    intervaloConfianzaMedia = new IntervaloConfianza(min, max, nivelConfianza);

}

From source file:net.groupbuy.plugin.oss.OssController.java

/**
 * /*ww w .  j av  a2s.  c  o m*/
 */
@RequestMapping(value = "/install", method = RequestMethod.POST)
public @ResponseBody Message install() {
    String specificationVersion = System.getProperty("java.specification.version");
    if (StringUtils.isNotEmpty(specificationVersion)) {
        BigDecimal version = new BigDecimal(specificationVersion);
        if (version.compareTo(new BigDecimal("1.6")) < 0) {
            return Message.error("admin.plugin.oss.unsupportedJavaVersion");
        }
    }
    if (!ossPlugin.getIsInstalled()) {
        PluginConfig pluginConfig = new PluginConfig();
        pluginConfig.setPluginId(ossPlugin.getId());
        pluginConfig.setIsEnabled(false);
        pluginConfigService.save(pluginConfig);
    }
    return SUCCESS_MESSAGE;
}

From source file:com.aoindustries.website.clientarea.accounting.MakePaymentStoredCardForm.java

@Override
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
    ActionErrors errors = new ActionErrors();
    if (GenericValidator.isBlankOrNull(paymentAmount)) {
        errors.add("paymentAmount", new ActionMessage("makePaymentStoredCardForm.paymentAmount.required"));
    } else {//from   ww  w .ja va 2  s  .c o m
        try {
            BigDecimal pa = new BigDecimal(this.paymentAmount);
            if (pa.compareTo(BigDecimal.ZERO) <= 0) {
                errors.add("paymentAmount",
                        new ActionMessage("makePaymentStoredCardForm.paymentAmount.mustBeGeaterThanZero"));
            } else if (pa.scale() > 2) {
                // Must not have more than 2 decimal places
                errors.add("paymentAmount",
                        new ActionMessage("makePaymentStoredCardForm.paymentAmount.invalid"));
            }
        } catch (NumberFormatException err) {
            errors.add("paymentAmount", new ActionMessage("makePaymentStoredCardForm.paymentAmount.invalid"));
        }
    }
    return errors;
}

From source file:ddf.catalog.validation.impl.validator.RangeValidator.java

private boolean checkRange(final BigDecimal value) {
    return min.compareTo(value) <= 0 && value.compareTo(max) <= 0;
}

From source file:org.apache.ofbiz.accounting.thirdparty.paypal.PayPalServices.java

private static void addCartDetails(NVPEncoder encoder, ShoppingCart cart) throws GenericEntityException {
    encoder.add("CURRENCYCODE", cart.getCurrency());
    int line = 0;
    for (ShoppingCartItem item : cart.items()) {
        encoder.add("L_NUMBER" + line, item.getProductId());
        encoder.add("L_NAME" + line, item.getName());
        encoder.add("L_AMT" + line, item.getBasePrice().setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());
        encoder.add("L_QTY" + line, item.getQuantity().toBigInteger().toString());
        line++;/*from   w  w w .  jav  a 2  s.c  o  m*/
        BigDecimal otherAdjustments = item.getOtherAdjustments();
        if (otherAdjustments.compareTo(BigDecimal.ZERO) != 0) {
            encoder.add("L_NUMBER" + line, item.getProductId());
            encoder.add("L_NAME" + line, item.getName() + " Adjustments");
            encoder.add("L_AMT" + line, otherAdjustments.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());
            encoder.add("L_QTY" + line, "1");
            line++;
        }
    }
    BigDecimal otherAdjustments = cart.getOrderOtherAdjustmentTotal();
    if (otherAdjustments.compareTo(BigDecimal.ZERO) != 0) {
        encoder.add("L_NUMBER" + line, "N/A");
        encoder.add("L_NAME" + line, "Order Adjustments");
        encoder.add("L_AMT" + line, otherAdjustments.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());
        encoder.add("L_QTY" + line, "1");
        line++;
    }
    encoder.add("ITEMAMT", cart.getSubTotal().add(otherAdjustments).setScale(2).toPlainString());
    encoder.add("SHIPPINGAMT", "0.00");
    encoder.add("TAXAMT", "0.00");
    encoder.add("AMT", cart.getSubTotal().add(otherAdjustments).setScale(2).toPlainString());
    //NOTE: The docs say this is optional but then won't work without it
    encoder.add("MAXAMT", cart.getSubTotal().add(otherAdjustments).setScale(2).toPlainString());
}