Skip to content

Commit 2b484a8

Browse files
committed
Fixed JAVAMONEY-94, Removed magic Strings for formatting.
1 parent 1c5a093 commit 2b484a8

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright (c) 2012, 2014, Credit Suisse (Anatole Tresch), 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.format;
17+
18+
/**
19+
* Class holding constants for passing additional parameters to {@link javax.money.MonetaryAmountFactoryQuery}
20+
* instances for configuring {@link javax.money.format.MonetaryAmountFormat} instances to be accessed.
21+
*/
22+
public final class AmountFormatParams {
23+
24+
/**
25+
* Non instantiatable class.
26+
*/
27+
private AmountFormatParams() {
28+
}
29+
30+
/**
31+
* Allows to pass a pattern as defined by {@link java.text.DecimalFormat}.
32+
*/
33+
public static final String PATTERN = "pattern";
34+
35+
/**
36+
* Allows to define the grouping sizes of the number groups as {@code int[]}, hereby starting from the decimal point.
37+
* So {@code 2, 2, 3} will format the number {@code 1234567890} as {@code 123.456.78.90}, assuming '.' as the only
38+
* grouping char. The grouping chars used can be similarly adapted/combined, see #GROUPING_GROUPING_SEPARATORS.
39+
*/
40+
public static final String GROUPING_SIZES = "groupingSizes";
41+
42+
/**
43+
* Allows to define the grouping characters of a number groups as {@code char[]}, hereby starting from the decimal point.
44+
* So {@code '''',':','.'} will format the number {@code 1234567890} as {@code 1'234:567.890}, assuming 3 as the only
45+
* grouping size. The grouping sizes used can be similarly adapted/combined, see #GROUPING_SIZES.
46+
*/
47+
public static final String GROUPING_GROUPING_SEPARATORS = "groupingSeparators";
48+
}

src/main/java/org/javamoney/moneta/format/internal/AmountNumberToken.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.javamoney.moneta.format.internal;
1717

18+
import org.javamoney.moneta.format.AmountFormatParams;
19+
1820
import java.io.IOException;
1921
import java.math.BigDecimal;
2022
import java.text.DecimalFormat;
@@ -86,8 +88,8 @@ public String getNumberPattern() {
8688
public void print(Appendable appendable, MonetaryAmount amount)
8789
throws IOException {
8890
int digits = amount.getCurrency().getDefaultFractionDigits();
89-
if (amountFormatContext.get("groupingSizes", int[].class) == null ||
90-
amountFormatContext.get("groupingSizes", int[].class).length == 0) {
91+
if (amountFormatContext.get(AmountFormatParams.GROUPING_SIZES, int[].class) == null ||
92+
amountFormatContext.get(AmountFormatParams.GROUPING_SIZES, int[].class).length == 0) {
9193
appendable.append(this.formatFormat.format(amount.getNumber()
9294
.numberValue(BigDecimal.class)));
9395
return;
@@ -101,12 +103,12 @@ public void print(Appendable appendable, MonetaryAmount amount)
101103
appendable.append(preformattedValue);
102104
} else {
103105
if (Objects.isNull(numberGroup)) {
104-
char[] groupChars = amountFormatContext.get("groupingSeparators", char[].class);
106+
char[] groupChars = amountFormatContext.get(AmountFormatParams.GROUPING_GROUPING_SEPARATORS, char[].class);
105107
if (groupChars == null || groupChars.length == 0) {
106108
groupChars = new char[]{this.formatFormat
107109
.getDecimalFormatSymbols().getGroupingSeparator()};
108110
}
109-
int[] groupSizes = amountFormatContext.get("groupingSizes", int[].class);
111+
int[] groupSizes = amountFormatContext.get(AmountFormatParams.GROUPING_SIZES, int[].class);
110112
if (groupSizes == null) {
111113
groupSizes = new int[0];
112114
}

0 commit comments

Comments
 (0)