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 |
please use gerrit instead
2016/09/02 17:30:55
This object is created based on a single currencyC
pals
2016/09/06 06:08:59
Done.
| |
32 /** | 38 /** |
33 * The symbol for the currency specified on the bill. For example, the symbo l for "USD" is "$". | 39 * The symbol for the currency specified on the bill. For example, the symbo l for "USD" is "$". |
34 */ | 40 */ |
35 private final String mCurrencySymbol; | 41 private final String mCurrencySymbol; |
36 | 42 |
37 /** | 43 /** |
38 * The number of digits after the decimal separator for the currency specifi ed on the bill. For | 44 * 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". | 45 * example, 2 for "USD" and 0 for "JPY". |
40 */ | 46 */ |
41 private final int mDefaultFractionDigits; | 47 private final int mDefaultFractionDigits; |
(...skipping 16 matching lines...) Expand all Loading... | |
58 * @param currencyCode The currency code. Most commonly, this follows ISO 42 17 format: 3 upper | 64 * @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 | 65 * case ASCII letters. For example, "USD". Format is not restricted. Should |
60 * not be null. | 66 * not be null. |
61 * @param userLocale User's current locale. Should not be null. | 67 * @param userLocale User's current locale. Should not be null. |
62 */ | 68 */ |
63 public CurrencyStringFormatter(String currencyCode, Locale userLocale) { | 69 public CurrencyStringFormatter(String currencyCode, Locale userLocale) { |
64 assert currencyCode != null : "currencyCode should not be null"; | 70 assert currencyCode != null : "currencyCode should not be null"; |
65 assert userLocale != null : "userLocale should not be null"; | 71 assert userLocale != null : "userLocale should not be null"; |
66 | 72 |
67 mAmountValuePattern = Pattern.compile(AMOUNT_VALUE_PATTERN); | 73 mAmountValuePattern = Pattern.compile(AMOUNT_VALUE_PATTERN); |
68 | 74 |
please use gerrit instead
2016/09/02 17:30:55
mFormattedCurrencyCode = currencyCode.length() <=
pals
2016/09/06 06:09:00
Done.
| |
69 String currencySymbol; | 75 String currencySymbol; |
70 int defaultFractionDigits; | 76 int defaultFractionDigits; |
71 try { | 77 try { |
72 Currency currency = Currency.getInstance(currencyCode); | 78 Currency currency = Currency.getInstance(currencyCode); |
73 currencySymbol = currency.getSymbol(); | 79 currencySymbol = currency.getSymbol(); |
74 defaultFractionDigits = currency.getDefaultFractionDigits(); | 80 defaultFractionDigits = currency.getDefaultFractionDigits(); |
75 } catch (IllegalArgumentException e) { | 81 } catch (IllegalArgumentException e) { |
76 // The spec does not limit the currencies to official ISO 4217 curre ncy code list, which | 82 // 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 | 83 // is used by java.util.Currency. For example, "BTX" (bitcoin) is no t an official ISO |
78 // 4217 currency code, but is allowed by the spec. | 84 // 4217 currency code, but is allowed by the spec. |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
115 * Returns true if the currency code string is in valid format. | 121 * Returns true if the currency code string is in valid format. |
116 * | 122 * |
117 * @param amountCurrencyCode The currency code to check for validity. | 123 * @param amountCurrencyCode The currency code to check for validity. |
118 * @return Whether the currency code is in valid format. | 124 * @return Whether the currency code is in valid format. |
119 */ | 125 */ |
120 public boolean isValidAmountCurrencyCode(String amountCurrencyCode) { | 126 public boolean isValidAmountCurrencyCode(String amountCurrencyCode) { |
121 return amountCurrencyCode != null && amountCurrencyCode.length() <= MAX_ CURRENCY_CODE_LEN; | 127 return amountCurrencyCode != null && amountCurrencyCode.length() <= MAX_ CURRENCY_CODE_LEN; |
122 } | 128 } |
123 | 129 |
124 /** | 130 /** |
131 * Formats the currency for display. Currency can be any string of at most 2 048 characters. | |
132 * Currency code more than 6 character is formatted to first 5 characters an d ellipsis. | |
133 * | |
134 * @param currency The currency code to format. Should not be null. | |
135 * @return The formatted currency code. | |
136 */ | |
137 public String formatCurrency(String currency) { | |
please use gerrit instead
2016/09/02 17:30:55
/** @return The currency code formatted for displa
pals
2016/09/06 06:09:00
Done.
| |
138 assert currency != null : "currency should not be null"; | |
139 | |
140 if (currency.length() <= MAX_CURRENCY_CHARS) { | |
141 return currency; | |
142 } | |
143 | |
144 return currency.substring(0, MAX_CURRENCY_CHARS - 1) + ELLIPSIS; | |
145 } | |
146 | |
147 /** | |
125 * Formats the currency string for display. Does not parse the string into a number, because it | 148 * Formats the currency string for display. Does not parse the string into a number, because it |
126 * might be too large. The number is formatted for the current locale and fo llows the symbol of | 149 * might be too large. The number is formatted for the current locale and fo llows the symbol of |
127 * the currency code. | 150 * the currency code. |
128 * | 151 * |
129 * @param amountValue The number to format. Should be in "^-?[0-9]+(\.[0-9]+ )?$" format. Should | 152 * @param amountValue The number to format. Should be in "^-?[0-9]+(\.[0-9]+ )?$" format. Should |
130 * not be null. | 153 * not be null. |
131 * @return The currency symbol followed by a space and the formatted number. | 154 * @return The currency symbol followed by a space and the formatted number. |
132 */ | 155 */ |
133 public String format(String amountValue) { | 156 public String format(String amountValue) { |
134 assert amountValue != null : "amountValue should not be null"; | 157 assert amountValue != null : "amountValue should not be null"; |
(...skipping 22 matching lines...) Expand all Loading... | |
157 if (null != decimals) result.append(decimals); | 180 if (null != decimals) result.append(decimals); |
158 | 181 |
159 for (int i = numberOfDecimals; i < mDefaultFractionDigits; i++) { | 182 for (int i = numberOfDecimals; i < mDefaultFractionDigits; i++) { |
160 result.append("0"); | 183 result.append("0"); |
161 } | 184 } |
162 } | 185 } |
163 | 186 |
164 return result.toString(); | 187 return result.toString(); |
165 } | 188 } |
166 } | 189 } |
OLD | NEW |