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

Commit 060226e

Browse files
committed
Indian number formatting broken #55
1 parent 6ed30a0 commit 060226e

File tree

5 files changed

+221
-89
lines changed

5 files changed

+221
-89
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Copyright (c) 2020, Werner Keil and others by the @author tag.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
use this file except in compliance with the License. You may obtain a copy of
6+
the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
License for the specific language governing permissions and limitations under
14+
the License.
15+
*/
16+
package org.javamoney.moneta.internal;
17+
18+
public class JDKObjects {
19+
/**
20+
* JDK Drop-in-replacement
21+
* @since 1.4.1
22+
*/
23+
public static boolean nonNull(Object obj) {
24+
return obj != null;
25+
}
26+
27+
/**
28+
* JDK Drop-in-replacement
29+
* @since 1.4.1
30+
*/
31+
public static boolean isNull(Object obj) {
32+
return obj == null;
33+
}
34+
}

src/main/java/org/javamoney/moneta/spi/MoneyUtils.java

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2012, 2014, Credit Suisse (Anatole Tresch), Werner Keil and others by the @author tag.
2+
Copyright (c) 2012, 2020, Werner Keil, Otavio Santana and others by the @author tag.
33
44
Licensed under the Apache License, Version 2.0 (the "License"); you may not
55
use this file except in compliance with the License. You may obtain a copy of
@@ -15,16 +15,22 @@
1515
*/
1616
package org.javamoney.moneta.spi;
1717

18+
import org.javamoney.moneta.internal.JDKObjects;
19+
1820
import javax.money.CurrencyUnit;
1921
import javax.money.MonetaryAmount;
2022
import javax.money.MonetaryContext;
2123
import javax.money.MonetaryException;
24+
2225
import java.math.BigDecimal;
2326
import java.math.MathContext;
2427
import java.math.RoundingMode;
25-
import java.util.Objects;
2628
import java.util.logging.Logger;
2729

30+
import static java.math.RoundingMode.HALF_EVEN;
31+
import static java.util.Objects.requireNonNull;
32+
import static java.util.logging.Level.FINEST;
33+
2834
/**
2935
* Platform RI: This utility class simplifies implementing {@link MonetaryAmount},
3036
* by providing the common functionality. The different explicitly typed methods
@@ -34,13 +40,17 @@
3440
* implement {@link MonetaryAmount} directly.
3541
*
3642
* @author Anatole Tresch
43+
* @author Werner Keil
3744
*/
3845
public final class MoneyUtils {
39-
/**
40-
* The logger used.
41-
*/
46+
4247
private static final Logger LOG = Logger.getLogger(MoneyUtils.class.getName());
4348

49+
public static final String NBSP_STRING = "\u00A0";
50+
public static final String NNBSP_STRING = "\u202F";
51+
public static final char NBSP = NBSP_STRING.charAt(0);
52+
public static final char NNBSP = NNBSP_STRING.charAt(0);
53+
4454
private MoneyUtils() {
4555
}
4656

@@ -94,13 +104,18 @@ public static BigDecimal getBigDecimal(Number num) {
94104
* @return the corresponding {@link BigDecimal}
95105
*/
96106
public static BigDecimal getBigDecimal(Number num, MonetaryContext moneyContext) {
97-
BigDecimal bd = getBigDecimal(num);
98-
if (moneyContext!=null) {
99-
MathContext mc = getMathContext(moneyContext, RoundingMode.HALF_EVEN);
107+
BigDecimal bd = getBigDecimal(num);
108+
if (JDKObjects.nonNull(moneyContext)) {
109+
MathContext mc = getMathContext(moneyContext, HALF_EVEN);
100110
bd = new BigDecimal(bd.toString(), mc);
101-
if (moneyContext.getMaxScale() > 0) {
102-
LOG.fine(String.format("Got Max Scale %s", moneyContext.getMaxScale()));
103-
bd = bd.setScale(moneyContext.getMaxScale(), mc.getRoundingMode());
111+
int maxScale = moneyContext.getMaxScale();
112+
if (maxScale > 0) {
113+
if (bd.scale() > maxScale) {
114+
if (LOG.isLoggable(FINEST)) {
115+
LOG.log(FINEST, "The number scale is " + bd.scale() + " but Max Scale is " + maxScale);
116+
}
117+
bd = bd.setScale(maxScale, mc.getRoundingMode());
118+
}
104119
}
105120
}
106121
return bd;
@@ -116,15 +131,12 @@ public static BigDecimal getBigDecimal(Number num, MonetaryContext moneyContext)
116131
*/
117132
public static MathContext getMathContext(MonetaryContext monetaryContext, RoundingMode defaultMode) {
118133
MathContext ctx = monetaryContext.get(MathContext.class);
119-
if (ctx!=null) {
134+
if (JDKObjects.nonNull(ctx)) {
120135
return ctx;
121136
}
122137
RoundingMode roundingMode = monetaryContext.get(RoundingMode.class);
123138
if (roundingMode == null) {
124-
roundingMode = defaultMode;
125-
}
126-
if (roundingMode == null) {
127-
roundingMode = RoundingMode.HALF_EVEN;
139+
roundingMode = HALF_EVEN;
128140
}
129141
return new MathContext(monetaryContext.getPrecision(), roundingMode);
130142
}
@@ -139,9 +151,9 @@ public static MathContext getMathContext(MonetaryContext monetaryContext, Roundi
139151
* {@link CurrencyUnit#getCurrencyCode()}).
140152
*/
141153
public static void checkAmountParameter(MonetaryAmount amount, CurrencyUnit currencyUnit) {
142-
Objects.requireNonNull(amount, "Amount must not be null.");
154+
requireNonNull(amount, "Amount must not be null.");
143155
final CurrencyUnit amountCurrency = amount.getCurrency();
144-
if (!(currencyUnit.getCurrencyCode().equals(amountCurrency.getCurrencyCode()))) {
156+
if (!currencyUnit.getCurrencyCode().equals(amountCurrency.getCurrencyCode())) {
145157
throw new MonetaryException("Currency mismatch: " + currencyUnit + '/' + amountCurrency);
146158
}
147159
}
@@ -153,7 +165,14 @@ public static void checkAmountParameter(MonetaryAmount amount, CurrencyUnit curr
153165
* @throws IllegalArgumentException If the number is null
154166
*/
155167
public static void checkNumberParameter(Number number) {
156-
Objects.requireNonNull(number, "Number is required.");
168+
requireNonNull(number, "Number is required.");
157169
}
158170

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

0 commit comments

Comments
 (0)