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
1515 */
1616package org .javamoney .moneta .spi ;
1717
18+ import org .javamoney .moneta .internal .JDKObjects ;
19+
1820import javax .money .CurrencyUnit ;
1921import javax .money .MonetaryAmount ;
2022import javax .money .MonetaryContext ;
2123import javax .money .MonetaryException ;
24+
2225import java .math .BigDecimal ;
2326import java .math .MathContext ;
2427import java .math .RoundingMode ;
25- import java .util .Objects ;
2628import 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
3440 * implement {@link MonetaryAmount} directly.
3541 *
3642 * @author Anatole Tresch
43+ * @author Werner Keil
3744 */
3845public 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