Skip to content

Commit

Permalink
Improved: Replace deprecated BigDecimal.ROUND_HALF by RoundingMode.HA…
Browse files Browse the repository at this point in the history
…LF (OFBIZ-13103)

Address this deprecation

int java.math.BigDecimal.ROUND_HALF_UP
Deprecated. Use RoundingMode.HALF_UP instead.

Rounding mode to round towards "nearest neighbor" unless both neighbors are
equidistant, in which case round up. Behaves as for ROUND_UP if the discarded
fraction is ≥ 0.5; otherwise, behaves as for ROUND_DOWN. Note that this is the
rounding mode that most of us were taught in grade school.
  • Loading branch information
JacquesLeRoux committed May 24, 2024
1 parent bc384fd commit b57e2d1
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
package org.apache.ofbiz.accounting.admin

import java.sql.Timestamp
import java.math.RoundingMode

import org.apache.ofbiz.base.util.UtilDateTime
import org.apache.ofbiz.base.util.UtilProperties
import org.apache.ofbiz.entity.condition.EntityConditionBuilder
import org.apache.ofbiz.entity.GenericValue
import org.apache.ofbiz.entity.condition.EntityConditionBuilder
import org.apache.ofbiz.entity.util.EntityUtil
import org.apache.ofbiz.service.ModelService

Expand Down Expand Up @@ -156,7 +158,7 @@ Map getFXConversion() {

BigDecimal conversionRate
int decimalScale = 2
int roundingMode = BigDecimal.ROUND_HALF_UP
int roundingMode = RoundingMode.ROUND_HALF_UP
if (rates) {
conversionFactor = EntityUtil.getFirst(rates).getBigDecimal('conversionFactor')
BigDecimal originalValue = BigDecimal.ONE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.apache.ofbiz.party.party

import java.math.RoundingMode

import org.apache.ofbiz.accounting.invoice.InvoiceWorker
import org.apache.ofbiz.accounting.payment.PaymentWorker
import org.apache.ofbiz.entity.condition.EntityCondition
Expand Down Expand Up @@ -61,12 +63,12 @@ while (invIterator.next()) {
Boolean isSalesInvoice = EntityTypeUtil.hasParentType(delegator, 'InvoiceType', 'invoiceTypeId', (String) invoice.getString('invoiceTypeId'),
'parentTypeId', 'SALES_INVOICE')
if (isPurchaseInvoice) {
totalInvPuApplied += InvoiceWorker.getInvoiceApplied(invoice, actualCurrency).setScale(2, BigDecimal.ROUND_HALF_UP)
totalInvPuNotApplied += InvoiceWorker.getInvoiceNotApplied(invoice, actualCurrency).setScale(2, BigDecimal.ROUND_HALF_UP)
totalInvPuApplied += InvoiceWorker.getInvoiceApplied(invoice, actualCurrency).setScale(2, RoundingMode.ROUND_HALF_UP)
totalInvPuNotApplied += InvoiceWorker.getInvoiceNotApplied(invoice, actualCurrency).setScale(2, RoundingMode.ROUND_HALF_UP)
}
else if (isSalesInvoice) {
totalInvSaApplied += InvoiceWorker.getInvoiceApplied(invoice, actualCurrency).setScale(2, BigDecimal.ROUND_HALF_UP)
totalInvSaNotApplied += InvoiceWorker.getInvoiceNotApplied(invoice, actualCurrency).setScale(2, BigDecimal.ROUND_HALF_UP)
totalInvSaApplied += InvoiceWorker.getInvoiceApplied(invoice, actualCurrency).setScale(2, RoundingMode.ROUND_HALF_UP)
totalInvSaNotApplied += InvoiceWorker.getInvoiceNotApplied(invoice, actualCurrency).setScale(2, RoundingMode.ROUND_HALF_UP)
}
else {
logError('InvoiceType: ' + invoice.invoiceTypeId + ' without a valid parentTypeId: ' + invoice.parentTypeId
Expand Down Expand Up @@ -101,12 +103,12 @@ payIterator = from('PaymentAndType').where(payExprs).cursorScrollInsensitive().d
while (payIterator.next()) {
payment = payIterator.next()
if (payment.parentTypeId == 'DISBURSEMENT' || payment.parentTypeId == 'TAX_PAYMENT') {
totalPayOutApplied += PaymentWorker.getPaymentApplied(payment, actualCurrency).setScale(2, BigDecimal.ROUND_HALF_UP)
totalPayOutNotApplied += PaymentWorker.getPaymentNotApplied(payment, actualCurrency).setScale(2, BigDecimal.ROUND_HALF_UP)
totalPayOutApplied += PaymentWorker.getPaymentApplied(payment, actualCurrency).setScale(2, RoundingMode.ROUND_HALF_UP)
totalPayOutNotApplied += PaymentWorker.getPaymentNotApplied(payment, actualCurrency).setScale(2, RoundingMode.ROUND_HALF_UP)
}
else if (payment.parentTypeId == 'RECEIPT') {
totalPayInApplied += PaymentWorker.getPaymentApplied(payment, actualCurrency).setScale(2, BigDecimal.ROUND_HALF_UP)
totalPayInNotApplied += PaymentWorker.getPaymentNotApplied(payment, actualCurrency).setScale(2, BigDecimal.ROUND_HALF_UP)
totalPayInApplied += PaymentWorker.getPaymentApplied(payment, actualCurrency).setScale(2, RoundingMode.ROUND_HALF_UP)
totalPayInNotApplied += PaymentWorker.getPaymentNotApplied(payment, actualCurrency).setScale(2, RoundingMode.ROUND_HALF_UP)
}
else {
logError('PaymentTypeId: ' + payment.paymentTypeId + ' without a valid parentTypeId: ' + payment.parentTypeId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.apache.ofbiz.party.party

import java.math.RoundingMode

import org.apache.ofbiz.accounting.invoice.InvoiceWorker
import org.apache.ofbiz.entity.condition.EntityCondition
import org.apache.ofbiz.entity.condition.EntityOperator
Expand Down Expand Up @@ -48,7 +50,7 @@ invIterator = from('InvoiceAndType').where(invExprs).cursorScrollInsensitive().d
invoiceList = []
while (invIterator.next()) {
invoice = invIterator.next()
unAppliedAmount = InvoiceWorker.getInvoiceNotApplied(invoice, actualCurrency).setScale(2, BigDecimal.ROUND_HALF_UP)
unAppliedAmount = InvoiceWorker.getInvoiceNotApplied(invoice, actualCurrency).setScale(2, RoundingMode.ROUND_HALF_UP)
if (unAppliedAmount.signum() == 1) {
if (actualCurrency == true) {
invoiceCurrencyUomId = invoice.currencyUomId
Expand All @@ -59,7 +61,7 @@ while (invIterator.next()) {
invoiceDate: invoice.invoiceDate,
unAppliedAmount: unAppliedAmount,
invoiceCurrencyUomId: invoiceCurrencyUomId,
amount: InvoiceWorker.getInvoiceTotal(invoice, actualCurrency).setScale(2, BigDecimal.ROUND_HALF_UP),
amount: InvoiceWorker.getInvoiceTotal(invoice, actualCurrency).setScale(2, RoundingMode.ROUND_HALF_UP),
invoiceTypeId: invoice.invoiceTypeId,
invoiceParentTypeId: invoice.parentTypeId])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.apache.ofbiz.party.party

import java.math.RoundingMode

import org.apache.ofbiz.accounting.payment.PaymentWorker
import org.apache.ofbiz.entity.util.EntityFindOptions
import org.apache.ofbiz.entity.condition.EntityCondition
Expand Down Expand Up @@ -50,7 +52,7 @@ payIterator = from('PaymentAndType').where(payExprs).cursorScrollInsensitive().d

while (payIterator.next()) {
payment = payIterator.next()
unAppliedAmount = PaymentWorker.getPaymentNotApplied(payment, actualCurrency).setScale(2, BigDecimal.ROUND_HALF_UP)
unAppliedAmount = PaymentWorker.getPaymentNotApplied(payment, actualCurrency).setScale(2, RoundingMode.ROUND_HALF_UP)
if (unAppliedAmount.signum() == 1) {
if (actualCurrency == true && payment.actualCurrencyAmount && payment.actualCurrencyUomId) {
amount = payment.actualCurrencyAmount
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.apache.ofbiz.product.catalog.product

import java.math.RoundingMode

import org.apache.ofbiz.base.util.UtilDateTime
import org.apache.ofbiz.entity.condition.EntityCondition
import org.apache.ofbiz.entity.condition.EntityOperator
Expand Down Expand Up @@ -92,7 +94,7 @@ while (itr <= 5) {
}
if (!orderItemDetail.isEmpty()) {
if (orderItemDetail.amount) {
orderItemDetail.amount = orderItemDetail.amount.setScale(2, BigDecimal.ROUND_HALF_UP)
orderItemDetail.amount = orderItemDetail.amount.setScale(2, RoundingMode.ROUND_HALF_UP)
}
topSellingProducts.add(orderItemDetail)
bestSellingProducts.remove(orderItemDetail)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public static int getBigDecimalScale(String property) {
* Method to get BigDecimal rounding mode from a property
* @param file - Name of the property file
* @param property - Name of the config property from ARITH_PROP_FILE (e.g., "invoice.rounding")
* @return int - Rounding mode to pass to BigDecimal's methods. Defaults to BigDecimal.ROUND_HALF_UP
* @return int - Rounding mode to pass to BigDecimal's methods. Defaults to RoundingMode.ROUND_HALF_UP
* @deprecated Use {@link #getRoundingMode(String, String)} instead
*/
@Deprecated
Expand All @@ -228,7 +228,7 @@ public static int getBigDecimalRoundingMode(String file, String property) {
/**
* Method to get BigDecimal rounding mode from a property. Use the default ARITH_PROP_FILE properties file
* @param property - Name of the config property from ARITH_PROP_FILE (e.g., "invoice.rounding")
* @return int - Rounding mode to pass to BigDecimal's methods. Defaults to BigDecimal.ROUND_HALF_UP
* @return int - Rounding mode to pass to BigDecimal's methods. Defaults to RoundingMode.ROUND_HALF_UP
* @deprecated Use {@link #getRoundingMode(String)} instead
*/
@Deprecated
Expand Down

0 comments on commit b57e2d1

Please sign in to comment.