Skip to content
This repository has been archived by the owner on Jan 18, 2022. It is now read-only.

Commit

Permalink
Indian number formatting broken #55
Browse files Browse the repository at this point in the history
  • Loading branch information
keilw committed Jul 21, 2020
1 parent 6ed30a0 commit 060226e
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 89 deletions.
34 changes: 34 additions & 0 deletions src/main/java/org/javamoney/moneta/internal/JDKObjects.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Copyright (c) 2020, Werner Keil and others by the @author tag.
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.
*/
package org.javamoney.moneta.internal;

public class JDKObjects {
/**
* JDK Drop-in-replacement
* @since 1.4.1
*/
public static boolean nonNull(Object obj) {
return obj != null;
}

/**
* JDK Drop-in-replacement
* @since 1.4.1
*/
public static boolean isNull(Object obj) {
return obj == null;
}
}
57 changes: 38 additions & 19 deletions src/main/java/org/javamoney/moneta/spi/MoneyUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2012, 2014, Credit Suisse (Anatole Tresch), Werner Keil and others by the @author tag.
Copyright (c) 2012, 2020, Werner Keil, Otavio Santana and others by the @author tag.
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
Expand All @@ -15,16 +15,22 @@
*/
package org.javamoney.moneta.spi;

import org.javamoney.moneta.internal.JDKObjects;

import javax.money.CurrencyUnit;
import javax.money.MonetaryAmount;
import javax.money.MonetaryContext;
import javax.money.MonetaryException;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.Objects;
import java.util.logging.Logger;

import static java.math.RoundingMode.HALF_EVEN;
import static java.util.Objects.requireNonNull;
import static java.util.logging.Level.FINEST;

/**
* Platform RI: This utility class simplifies implementing {@link MonetaryAmount},
* by providing the common functionality. The different explicitly typed methods
Expand All @@ -34,13 +40,17 @@
* implement {@link MonetaryAmount} directly.
*
* @author Anatole Tresch
* @author Werner Keil
*/
public final class MoneyUtils {
/**
* The logger used.
*/

private static final Logger LOG = Logger.getLogger(MoneyUtils.class.getName());

public static final String NBSP_STRING = "\u00A0";
public static final String NNBSP_STRING = "\u202F";
public static final char NBSP = NBSP_STRING.charAt(0);
public static final char NNBSP = NNBSP_STRING.charAt(0);

private MoneyUtils() {
}

Expand Down Expand Up @@ -94,13 +104,18 @@ public static BigDecimal getBigDecimal(Number num) {
* @return the corresponding {@link BigDecimal}
*/
public static BigDecimal getBigDecimal(Number num, MonetaryContext moneyContext) {
BigDecimal bd = getBigDecimal(num);
if (moneyContext!=null) {
MathContext mc = getMathContext(moneyContext, RoundingMode.HALF_EVEN);
BigDecimal bd = getBigDecimal(num);
if (JDKObjects.nonNull(moneyContext)) {
MathContext mc = getMathContext(moneyContext, HALF_EVEN);
bd = new BigDecimal(bd.toString(), mc);
if (moneyContext.getMaxScale() > 0) {
LOG.fine(String.format("Got Max Scale %s", moneyContext.getMaxScale()));
bd = bd.setScale(moneyContext.getMaxScale(), mc.getRoundingMode());
int maxScale = moneyContext.getMaxScale();
if (maxScale > 0) {
if (bd.scale() > maxScale) {
if (LOG.isLoggable(FINEST)) {
LOG.log(FINEST, "The number scale is " + bd.scale() + " but Max Scale is " + maxScale);
}
bd = bd.setScale(maxScale, mc.getRoundingMode());
}
}
}
return bd;
Expand All @@ -116,15 +131,12 @@ public static BigDecimal getBigDecimal(Number num, MonetaryContext moneyContext)
*/
public static MathContext getMathContext(MonetaryContext monetaryContext, RoundingMode defaultMode) {
MathContext ctx = monetaryContext.get(MathContext.class);
if (ctx!=null) {
if (JDKObjects.nonNull(ctx)) {
return ctx;
}
RoundingMode roundingMode = monetaryContext.get(RoundingMode.class);
if (roundingMode == null) {
roundingMode = defaultMode;
}
if (roundingMode == null) {
roundingMode = RoundingMode.HALF_EVEN;
roundingMode = HALF_EVEN;
}
return new MathContext(monetaryContext.getPrecision(), roundingMode);
}
Expand All @@ -139,9 +151,9 @@ public static MathContext getMathContext(MonetaryContext monetaryContext, Roundi
* {@link CurrencyUnit#getCurrencyCode()}).
*/
public static void checkAmountParameter(MonetaryAmount amount, CurrencyUnit currencyUnit) {
Objects.requireNonNull(amount, "Amount must not be null.");
requireNonNull(amount, "Amount must not be null.");
final CurrencyUnit amountCurrency = amount.getCurrency();
if (!(currencyUnit.getCurrencyCode().equals(amountCurrency.getCurrencyCode()))) {
if (!currencyUnit.getCurrencyCode().equals(amountCurrency.getCurrencyCode())) {
throw new MonetaryException("Currency mismatch: " + currencyUnit + '/' + amountCurrency);
}
}
Expand All @@ -153,7 +165,14 @@ public static void checkAmountParameter(MonetaryAmount amount, CurrencyUnit curr
* @throws IllegalArgumentException If the number is null
*/
public static void checkNumberParameter(Number number) {
Objects.requireNonNull(number, "Number is required.");
requireNonNull(number, "Number is required.");
}

/**
* Replaces the non-breaking space character U+00A0 and Narrow non-breaking space U+202F from the string with usual space.
* https://en.wikipedia.org/wiki/Non-breaking_space}
*/
public static String replaceNbspWithSpace(String s) {
return s.replace(NBSP, ' ').replace(NNBSP, ' ');
}
}
Loading

0 comments on commit 060226e

Please sign in to comment.