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 // Max currency code length. Maximum length of currency code can be at most 2048. | 24 // Max currency code length. Maximum length of currency code can be at most 2048. |
25 private static final int MAX_CURRENCY_CODE_LEN = 2048; | 25 private static final int MAX_CURRENCY_CODE_LEN = 2048; |
26 | 26 |
27 // Currency code exceeding 6 chars will be ellipsized during formatting for display. | |
28 private static final int MAX_CURRENCY_CHARS = 6; | |
29 | |
30 // Unicode character for ellipsis. | |
31 private static final String ELLIPSIS = "\u2026"; | |
32 | |
27 // Formatting constants. | 33 // Formatting constants. |
28 private static final int DIGIT_GROUPING_SIZE = 3; | 34 private static final int DIGIT_GROUPING_SIZE = 3; |
29 | 35 |
30 private final Pattern mAmountValuePattern; | 36 private final Pattern mAmountValuePattern; |
31 | 37 |
32 /** | 38 /** |
39 * The currency specified in the payment request. | |
40 */ | |
41 private final String mCurrencyCode; | |
42 | |
43 /** | |
33 * The symbol for the currency specified on the bill. For example, the symbo l for "USD" is "$". | 44 * The symbol for the currency specified on the bill. For example, the symbo l for "USD" is "$". |
34 */ | 45 */ |
35 private final String mCurrencySymbol; | 46 private final String mCurrencySymbol; |
36 | 47 |
37 /** | 48 /** |
38 * The number of digits after the decimal separator for the currency specifi ed on the bill. For | 49 * The number of digits after the decimal separator for the currency specifi ed on the bill. For |
39 * example, 2 for "USD" and 0 for "JPY". | 50 * example, 2 for "USD" and 0 for "JPY". |
40 */ | 51 */ |
41 private final int mDefaultFractionDigits; | 52 private final int mDefaultFractionDigits; |
42 | 53 |
(...skipping 15 matching lines...) Expand all Loading... | |
58 * @param currencyCode The currency code. Most commonly, this follows ISO 42 17 format: 3 upper | 69 * @param currencyCode The currency code. Most commonly, this follows ISO 42 17 format: 3 upper |
59 * case ASCII letters. For example, "USD". Format is not restricted. Should | 70 * case ASCII letters. For example, "USD". Format is not restricted. Should |
60 * not be null. | 71 * not be null. |
61 * @param userLocale User's current locale. Should not be null. | 72 * @param userLocale User's current locale. Should not be null. |
62 */ | 73 */ |
63 public CurrencyStringFormatter(String currencyCode, Locale userLocale) { | 74 public CurrencyStringFormatter(String currencyCode, Locale userLocale) { |
64 assert currencyCode != null : "currencyCode should not be null"; | 75 assert currencyCode != null : "currencyCode should not be null"; |
65 assert userLocale != null : "userLocale should not be null"; | 76 assert userLocale != null : "userLocale should not be null"; |
66 | 77 |
67 mAmountValuePattern = Pattern.compile(AMOUNT_VALUE_PATTERN); | 78 mAmountValuePattern = Pattern.compile(AMOUNT_VALUE_PATTERN); |
79 mCurrencyCode = currencyCode; | |
68 | 80 |
69 String currencySymbol; | 81 String currencySymbol; |
70 int defaultFractionDigits; | 82 int defaultFractionDigits; |
71 try { | 83 try { |
72 Currency currency = Currency.getInstance(currencyCode); | 84 Currency currency = Currency.getInstance(currencyCode); |
73 currencySymbol = currency.getSymbol(); | 85 currencySymbol = currency.getSymbol(); |
74 defaultFractionDigits = currency.getDefaultFractionDigits(); | 86 defaultFractionDigits = currency.getDefaultFractionDigits(); |
75 } catch (IllegalArgumentException e) { | 87 } catch (IllegalArgumentException e) { |
76 // The spec does not limit the currencies to official ISO 4217 curre ncy code list, which | 88 // The spec does not limit the currencies to official ISO 4217 curre ncy code list, which |
77 // is used by java.util.Currency. For example, "BTX" (bitcoin) is no t an official ISO | 89 // is used by java.util.Currency. For example, "BTX" (bitcoin) is no t an official ISO |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
153 int numberOfDecimals = decimals == null ? 0 : decimals.length(); | 165 int numberOfDecimals = decimals == null ? 0 : decimals.length(); |
154 | 166 |
155 if (numberOfDecimals > 0 || mDefaultFractionDigits > 0) { | 167 if (numberOfDecimals > 0 || mDefaultFractionDigits > 0) { |
156 result.append(mMonetaryDecimalSeparator); | 168 result.append(mMonetaryDecimalSeparator); |
157 if (null != decimals) result.append(decimals); | 169 if (null != decimals) result.append(decimals); |
158 | 170 |
159 for (int i = numberOfDecimals; i < mDefaultFractionDigits; i++) { | 171 for (int i = numberOfDecimals; i < mDefaultFractionDigits; i++) { |
160 result.append("0"); | 172 result.append("0"); |
161 } | 173 } |
162 } | 174 } |
163 | 175 |
please use gerrit instead
2016/09/01 14:55:55
/** @return The currency code formatted for displa
pals
2016/09/02 11:51:15
Done.
| |
176 String currency = mCurrencyCode; | |
177 if (mCurrencyCode.length() > MAX_CURRENCY_CHARS) { | |
178 currency = mCurrencyCode.substring(0, MAX_CURRENCY_CHARS - 1) + ELLI PSIS; | |
179 } | |
180 | |
181 if (currency.length() > 0) { | |
182 result.insert(0, " "); | |
183 result.insert(0, currency); | |
184 } | |
185 | |
164 return result.toString(); | 186 return result.toString(); |
165 } | 187 } |
166 } | 188 } |
OLD | NEW |