11/*
2- Copyright (c) 2012, 2020, Werner Keil, Otavio Santana and others by the @author tag.
2+ Copyright (c) 2012, 2014, Credit Suisse (Anatole Tresch), Werner Keil 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-
2018import javax .money .CurrencyUnit ;
2119import javax .money .MonetaryAmount ;
2220import javax .money .MonetaryContext ;
2321import javax .money .MonetaryException ;
24-
2522import java .math .BigDecimal ;
2623import java .math .MathContext ;
2724import java .math .RoundingMode ;
25+ import java .util .Objects ;
2826import java .util .logging .Logger ;
2927
30- import static java .math .RoundingMode .HALF_EVEN ;
31- import static java .util .Objects .requireNonNull ;
32- import static java .util .logging .Level .FINEST ;
33-
3428/**
3529 * Platform RI: This utility class simplifies implementing {@link MonetaryAmount},
3630 * by providing the common functionality. The different explicitly typed methods
4034 * implement {@link MonetaryAmount} directly.
4135 *
4236 * @author Anatole Tresch
43- * @author Werner Keil
4437 */
4538public final class MoneyUtils {
46-
39+ /**
40+ * The logger used.
41+ */
4742 private static final Logger LOG = Logger .getLogger (MoneyUtils .class .getName ());
4843
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-
5444 private MoneyUtils () {
5545 }
5646
@@ -104,18 +94,13 @@ public static BigDecimal getBigDecimal(Number num) {
10494 * @return the corresponding {@link BigDecimal}
10595 */
10696 public static BigDecimal getBigDecimal (Number num , MonetaryContext moneyContext ) {
107- BigDecimal bd = getBigDecimal (num );
108- if (JDKObjects . nonNull ( moneyContext ) ) {
109- MathContext mc = getMathContext (moneyContext , HALF_EVEN );
97+ BigDecimal bd = getBigDecimal (num );
98+ if (moneyContext != null ) {
99+ MathContext mc = getMathContext (moneyContext , RoundingMode . HALF_EVEN );
110100 bd = new BigDecimal (bd .toString (), mc );
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- }
101+ if (moneyContext .getMaxScale () > 0 ) {
102+ LOG .fine (String .format ("Got Max Scale %s" , moneyContext .getMaxScale ()));
103+ bd = bd .setScale (moneyContext .getMaxScale (), mc .getRoundingMode ());
119104 }
120105 }
121106 return bd ;
@@ -131,12 +116,15 @@ public static BigDecimal getBigDecimal(Number num, MonetaryContext moneyContext)
131116 */
132117 public static MathContext getMathContext (MonetaryContext monetaryContext , RoundingMode defaultMode ) {
133118 MathContext ctx = monetaryContext .get (MathContext .class );
134- if (JDKObjects . nonNull ( ctx ) ) {
119+ if (ctx != null ) {
135120 return ctx ;
136121 }
137122 RoundingMode roundingMode = monetaryContext .get (RoundingMode .class );
138123 if (roundingMode == null ) {
139- roundingMode = HALF_EVEN ;
124+ roundingMode = defaultMode ;
125+ }
126+ if (roundingMode == null ) {
127+ roundingMode = RoundingMode .HALF_EVEN ;
140128 }
141129 return new MathContext (monetaryContext .getPrecision (), roundingMode );
142130 }
@@ -151,9 +139,9 @@ public static MathContext getMathContext(MonetaryContext monetaryContext, Roundi
151139 * {@link CurrencyUnit#getCurrencyCode()}).
152140 */
153141 public static void checkAmountParameter (MonetaryAmount amount , CurrencyUnit currencyUnit ) {
154- requireNonNull (amount , "Amount must not be null." );
142+ Objects . requireNonNull (amount , "Amount must not be null." );
155143 final CurrencyUnit amountCurrency = amount .getCurrency ();
156- if (!currencyUnit .getCurrencyCode ().equals (amountCurrency .getCurrencyCode ())) {
144+ if (!( currencyUnit .getCurrencyCode ().equals (amountCurrency .getCurrencyCode () ))) {
157145 throw new MonetaryException ("Currency mismatch: " + currencyUnit + '/' + amountCurrency );
158146 }
159147 }
@@ -165,14 +153,7 @@ public static void checkAmountParameter(MonetaryAmount amount, CurrencyUnit curr
165153 * @throws IllegalArgumentException If the number is null
166154 */
167155 public static void checkNumberParameter (Number number ) {
168- requireNonNull (number , "Number is required." );
156+ Objects . requireNonNull (number , "Number is required." );
169157 }
170158
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- }
178159}
0 commit comments