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 formatted for display. Currency can be any string of at most | |
40 * 2048 characters.Currency code more than 6 character is formatted to first | |
41 * 5 characters and ellipsis. | |
42 */ | |
43 public final String mFormattedCurrencyCode; | |
44 | |
45 /** | |
33 * The symbol for the currency specified on the bill. For example, the symbo l for "USD" is "$". | 46 * The symbol for the currency specified on the bill. For example, the symbo l for "USD" is "$". |
34 */ | 47 */ |
35 private final String mCurrencySymbol; | 48 private final String mCurrencySymbol; |
36 | 49 |
37 /** | 50 /** |
38 * The number of digits after the decimal separator for the currency specifi ed on the bill. For | 51 * 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". | 52 * example, 2 for "USD" and 0 for "JPY". |
40 */ | 53 */ |
41 private final int mDefaultFractionDigits; | 54 private final int mDefaultFractionDigits; |
42 | 55 |
(...skipping 16 matching lines...) Expand all Loading... | |
59 * case ASCII letters. For example, "USD". Format is not restricted. Should | 72 * case ASCII letters. For example, "USD". Format is not restricted. Should |
60 * not be null. | 73 * not be null. |
61 * @param userLocale User's current locale. Should not be null. | 74 * @param userLocale User's current locale. Should not be null. |
62 */ | 75 */ |
63 public CurrencyStringFormatter(String currencyCode, Locale userLocale) { | 76 public CurrencyStringFormatter(String currencyCode, Locale userLocale) { |
64 assert currencyCode != null : "currencyCode should not be null"; | 77 assert currencyCode != null : "currencyCode should not be null"; |
65 assert userLocale != null : "userLocale should not be null"; | 78 assert userLocale != null : "userLocale should not be null"; |
66 | 79 |
67 mAmountValuePattern = Pattern.compile(AMOUNT_VALUE_PATTERN); | 80 mAmountValuePattern = Pattern.compile(AMOUNT_VALUE_PATTERN); |
68 | 81 |
82 mFormattedCurrencyCode = currencyCode.length() <= MAX_CURRENCY_CHARS | |
83 ? currencyCode | |
84 : currencyCode.substring(0, MAX_CURRENCY_CHARS - 1) + ELLIPSIS; | |
gone
2016/09/06 17:20:32
Given that you're just tacking on an ellipsis, wil
pals
2016/09/08 04:12:14
I guess RTL rendering is taken care by view. I hav
| |
85 | |
69 String currencySymbol; | 86 String currencySymbol; |
70 int defaultFractionDigits; | 87 int defaultFractionDigits; |
71 try { | 88 try { |
72 Currency currency = Currency.getInstance(currencyCode); | 89 Currency currency = Currency.getInstance(currencyCode); |
73 currencySymbol = currency.getSymbol(); | 90 currencySymbol = currency.getSymbol(); |
74 defaultFractionDigits = currency.getDefaultFractionDigits(); | 91 defaultFractionDigits = currency.getDefaultFractionDigits(); |
75 } catch (IllegalArgumentException e) { | 92 } catch (IllegalArgumentException e) { |
76 // The spec does not limit the currencies to official ISO 4217 curre ncy code list, which | 93 // 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 | 94 // 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. | 95 // 4217 currency code, but is allowed by the spec. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
114 /** | 131 /** |
115 * Returns true if the currency code string is in valid format. | 132 * Returns true if the currency code string is in valid format. |
116 * | 133 * |
117 * @param amountCurrencyCode The currency code to check for validity. | 134 * @param amountCurrencyCode The currency code to check for validity. |
118 * @return Whether the currency code is in valid format. | 135 * @return Whether the currency code is in valid format. |
119 */ | 136 */ |
120 public boolean isValidAmountCurrencyCode(String amountCurrencyCode) { | 137 public boolean isValidAmountCurrencyCode(String amountCurrencyCode) { |
121 return amountCurrencyCode != null && amountCurrencyCode.length() <= MAX_ CURRENCY_CODE_LEN; | 138 return amountCurrencyCode != null && amountCurrencyCode.length() <= MAX_ CURRENCY_CODE_LEN; |
122 } | 139 } |
123 | 140 |
141 /** @return The currency code formatted for display. */ | |
142 public String getFormattedCurrencyCode() { | |
143 return mFormattedCurrencyCode; | |
144 } | |
145 | |
124 /** | 146 /** |
125 * Formats the currency string for display. Does not parse the string into a number, because it | 147 * 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 | 148 * might be too large. The number is formatted for the current locale and fo llows the symbol of |
127 * the currency code. | 149 * the currency code. |
128 * | 150 * |
129 * @param amountValue The number to format. Should be in "^-?[0-9]+(\.[0-9]+ )?$" format. Should | 151 * @param amountValue The number to format. Should be in "^-?[0-9]+(\.[0-9]+ )?$" format. Should |
130 * not be null. | 152 * not be null. |
131 * @return The currency symbol followed by a space and the formatted number. | 153 * @return The currency symbol followed by a space and the formatted number. |
132 */ | 154 */ |
133 public String format(String amountValue) { | 155 public String format(String amountValue) { |
(...skipping 23 matching lines...) Expand all Loading... | |
157 if (null != decimals) result.append(decimals); | 179 if (null != decimals) result.append(decimals); |
158 | 180 |
159 for (int i = numberOfDecimals; i < mDefaultFractionDigits; i++) { | 181 for (int i = numberOfDecimals; i < mDefaultFractionDigits; i++) { |
160 result.append("0"); | 182 result.append("0"); |
161 } | 183 } |
162 } | 184 } |
163 | 185 |
164 return result.toString(); | 186 return result.toString(); |
165 } | 187 } |
166 } | 188 } |
OLD | NEW |