OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.payments; | 5 package org.chromium.chrome.browser.payments; |
6 | 6 |
7 import java.text.DecimalFormatSymbols; | 7 import java.text.DecimalFormatSymbols; |
8 import java.util.Currency; | 8 import java.util.Currency; |
9 import java.util.Locale; | 9 import java.util.Locale; |
10 import java.util.regex.Matcher; | 10 import java.util.regex.Matcher; |
11 import java.util.regex.Pattern; | 11 import java.util.regex.Pattern; |
12 | 12 |
13 /** | 13 /** |
14 * Formatter for currency strings that can be too large to parse into numbers. | 14 * Formatter for currency strings that can be too large to parse into numbers. |
15 * https://w3c.github.io/browser-payment-api/specs/paymentrequest.html#currencya
mount | 15 * https://w3c.github.io/browser-payment-api/specs/paymentrequest.html#currencya
mount |
16 */ | 16 */ |
17 public class CurrencyStringFormatter { | 17 public class CurrencyStringFormatter { |
18 // Amount value pattern and capture group numbers. | 18 // Amount value pattern and capture group numbers. |
19 private static final String AMOUNT_VALUE_PATTERN = "^(-?)([0-9]+)(\\.([0-9]+
))?$"; | 19 private static final String AMOUNT_VALUE_PATTERN = "^(-?)([0-9]+)(\\.([0-9]+
))?$"; |
20 private static final int OPTIONAL_NEGATIVE_GROUP = 1; | 20 private static final int OPTIONAL_NEGATIVE_GROUP = 1; |
21 private static final int DIGITS_BETWEEN_NEGATIVE_AND_PERIOD_GROUP = 2; | 21 private static final int DIGITS_BETWEEN_NEGATIVE_AND_PERIOD_GROUP = 2; |
22 private static final int DIGITS_AFTER_PERIOD_GROUP = 4; | 22 private static final int DIGITS_AFTER_PERIOD_GROUP = 4; |
23 | 23 |
24 // Amount currency code pattern. | 24 // Amount currency code pattern. |
25 private static final String AMOUNT_CURRENCY_CODE_PATTERN = "^[A-Z]{3}$"; | 25 private static final int MAX_CURRENCY_CODE_LEN = 2048; |
26 | 26 |
27 // Formatting constants. | 27 // Formatting constants. |
28 private static final int DIGIT_GROUPING_SIZE = 3; | 28 private static final int DIGIT_GROUPING_SIZE = 3; |
29 | 29 |
30 private final Pattern mAmountValuePattern; | 30 private final Pattern mAmountValuePattern; |
31 private final Pattern mAmountCurrencyCodePattern; | |
32 | 31 |
33 /** | 32 /** |
34 * The symbol for the currency specified on the bill. For example, the symbo
l for "USD" is "$". | 33 * The symbol for the currency specified on the bill. For example, the symbo
l for "USD" is "$". |
35 */ | 34 */ |
36 private final String mCurrencySymbol; | 35 private final String mCurrencySymbol; |
37 | 36 |
38 /** | 37 /** |
39 * The number grouping separator for the current locale. For example, "," in
US. 3-digit groups | 38 * The number grouping separator for the current locale. For example, "," in
US. 3-digit groups |
40 * are assumed. | 39 * are assumed. |
41 */ | 40 */ |
(...skipping 10 matching lines...) Expand all Loading... |
52 * @param currencyCode The currency code. Most commonly, this follows ISO 42
17 format: 3 upper | 51 * @param currencyCode The currency code. Most commonly, this follows ISO 42
17 format: 3 upper |
53 * case ASCII letters. For example, "USD". Format is not
restricted. Should | 52 * case ASCII letters. For example, "USD". Format is not
restricted. Should |
54 * not be null. | 53 * not be null. |
55 * @param userLocale User's current locale. Should not be null. | 54 * @param userLocale User's current locale. Should not be null. |
56 */ | 55 */ |
57 public CurrencyStringFormatter(String currencyCode, Locale userLocale) { | 56 public CurrencyStringFormatter(String currencyCode, Locale userLocale) { |
58 assert currencyCode != null : "currencyCode should not be null"; | 57 assert currencyCode != null : "currencyCode should not be null"; |
59 assert userLocale != null : "userLocale should not be null"; | 58 assert userLocale != null : "userLocale should not be null"; |
60 | 59 |
61 mAmountValuePattern = Pattern.compile(AMOUNT_VALUE_PATTERN); | 60 mAmountValuePattern = Pattern.compile(AMOUNT_VALUE_PATTERN); |
62 mAmountCurrencyCodePattern = Pattern.compile(AMOUNT_CURRENCY_CODE_PATTER
N); | |
63 | 61 |
64 String currencySymbol; | 62 String currencySymbol; |
65 try { | 63 try { |
66 currencySymbol = Currency.getInstance(currencyCode).getSymbol(); | 64 currencySymbol = Currency.getInstance(currencyCode).getSymbol(); |
67 } catch (IllegalArgumentException e) { | 65 } catch (IllegalArgumentException e) { |
68 // The spec does not limit the currencies to official ISO 4217 curre
ncy code list, which | 66 // The spec does not limit the currencies to official ISO 4217 curre
ncy code list, which |
69 // is used by java.util.Currency. For example, "BTX" (bitcoin) is no
t an official ISO | 67 // is used by java.util.Currency. For example, "BTX" (bitcoin) is no
t an official ISO |
70 // 4217 currency code, but is allowed by the spec. | 68 // 4217 currency code, but is allowed by the spec. |
71 currencySymbol = currencyCode; | 69 currencySymbol = currencyCode; |
72 } | 70 } |
(...skipping 20 matching lines...) Expand all Loading... |
93 return amountValue != null && mAmountValuePattern.matcher(amountValue).m
atches(); | 91 return amountValue != null && mAmountValuePattern.matcher(amountValue).m
atches(); |
94 } | 92 } |
95 | 93 |
96 /** | 94 /** |
97 * Returns true if the currency code string is in valid format. | 95 * Returns true if the currency code string is in valid format. |
98 * | 96 * |
99 * @param amountCurrencyCode The currency code to check for validity. | 97 * @param amountCurrencyCode The currency code to check for validity. |
100 * @return Whether the currency code is in valid format. | 98 * @return Whether the currency code is in valid format. |
101 */ | 99 */ |
102 public boolean isValidAmountCurrencyCode(String amountCurrencyCode) { | 100 public boolean isValidAmountCurrencyCode(String amountCurrencyCode) { |
103 return amountCurrencyCode != null | 101 return amountCurrencyCode != null && amountCurrencyCode.length() <= MAX_
CURRENCY_CODE_LEN; |
104 && mAmountCurrencyCodePattern.matcher(amountCurrencyCode).matche
s(); | |
105 } | 102 } |
106 | 103 |
107 /** | 104 /** |
108 * Formats the currency string for display. Does not parse the string into a
number, because it | 105 * Formats the currency string for display. Does not parse the string into a
number, because it |
109 * might be too large. The number is formatted for the current locale and fo
llows the symbol of | 106 * might be too large. The number is formatted for the current locale and fo
llows the symbol of |
110 * the currency code. | 107 * the currency code. |
111 * | 108 * |
112 * @param amountValue The number to format. Should be in "^-?[0-9]+(\.[0-9]+
)?$" format. Should | 109 * @param amountValue The number to format. Should be in "^-?[0-9]+(\.[0-9]+
)?$" format. Should |
113 * not be null. | 110 * not be null. |
114 * @return The currency symbol followed by a space and the formatted number. | 111 * @return The currency symbol followed by a space and the formatted number. |
(...skipping 26 matching lines...) Expand all Loading... |
141 result.append(decimals); | 138 result.append(decimals); |
142 } | 139 } |
143 | 140 |
144 for (int i = numberOfDecimals; i < 2; i++) { | 141 for (int i = numberOfDecimals; i < 2; i++) { |
145 result.append("0"); | 142 result.append("0"); |
146 } | 143 } |
147 | 144 |
148 return result.toString(); | 145 return result.toString(); |
149 } | 146 } |
150 } | 147 } |
OLD | NEW |