Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | ******************************************************************************** |
michael@0 | 3 | * Copyright (C) 2012-2013, International Business Machines |
michael@0 | 4 | * Corporation and others. All Rights Reserved. |
michael@0 | 5 | ******************************************************************************** |
michael@0 | 6 | * |
michael@0 | 7 | * File COMPACTDECIMALFORMAT.H |
michael@0 | 8 | ******************************************************************************** |
michael@0 | 9 | */ |
michael@0 | 10 | |
michael@0 | 11 | #ifndef __COMPACT_DECIMAL_FORMAT_H__ |
michael@0 | 12 | #define __COMPACT_DECIMAL_FORMAT_H__ |
michael@0 | 13 | |
michael@0 | 14 | #include "unicode/utypes.h" |
michael@0 | 15 | /** |
michael@0 | 16 | * \file |
michael@0 | 17 | * \brief C++ API: Formats decimal numbers in compact form. |
michael@0 | 18 | */ |
michael@0 | 19 | |
michael@0 | 20 | #if !UCONFIG_NO_FORMATTING |
michael@0 | 21 | #ifndef U_HIDE_DRAFT_API |
michael@0 | 22 | |
michael@0 | 23 | #include "unicode/decimfmt.h" |
michael@0 | 24 | |
michael@0 | 25 | struct UHashtable; |
michael@0 | 26 | |
michael@0 | 27 | U_NAMESPACE_BEGIN |
michael@0 | 28 | |
michael@0 | 29 | class PluralRules; |
michael@0 | 30 | |
michael@0 | 31 | /** |
michael@0 | 32 | * The CompactDecimalFormat produces abbreviated numbers, suitable for display in |
michael@0 | 33 | * environments will limited real estate. For example, 'Hits: 1.2B' instead of |
michael@0 | 34 | * 'Hits: 1,200,000,000'. The format will be appropriate for the given language, |
michael@0 | 35 | * such as "1,2 Mrd." for German. |
michael@0 | 36 | * <p> |
michael@0 | 37 | * For numbers under 1000 trillion (under 10^15, such as 123,456,789,012,345), |
michael@0 | 38 | * the result will be short for supported languages. However, the result may |
michael@0 | 39 | * sometimes exceed 7 characters, such as when there are combining marks or thin |
michael@0 | 40 | * characters. In such cases, the visual width in fonts should still be short. |
michael@0 | 41 | * <p> |
michael@0 | 42 | * By default, there are 3 significant digits. After creation, if more than |
michael@0 | 43 | * three significant digits are set (with setMaximumSignificantDigits), or if a |
michael@0 | 44 | * fixed number of digits are set (with setMaximumIntegerDigits or |
michael@0 | 45 | * setMaximumFractionDigits), then result may be wider. |
michael@0 | 46 | * <p> |
michael@0 | 47 | * At this time, parsing is not supported, and will produce a U_UNSUPPORTED_ERROR. |
michael@0 | 48 | * Resetting the pattern prefixes or suffixes is not supported; the method calls |
michael@0 | 49 | * are ignored. |
michael@0 | 50 | * <p> |
michael@0 | 51 | * @draft ICU 51 |
michael@0 | 52 | */ |
michael@0 | 53 | class U_I18N_API CompactDecimalFormat : public DecimalFormat { |
michael@0 | 54 | public: |
michael@0 | 55 | |
michael@0 | 56 | /** |
michael@0 | 57 | * Returns a compact decimal instance for specified locale. |
michael@0 | 58 | * @param inLocale the given locale. |
michael@0 | 59 | * @param style whether to use short or long style. |
michael@0 | 60 | * @param status error code returned here. |
michael@0 | 61 | * @draft ICU 51 |
michael@0 | 62 | */ |
michael@0 | 63 | static CompactDecimalFormat* U_EXPORT2 createInstance( |
michael@0 | 64 | const Locale& inLocale, UNumberCompactStyle style, UErrorCode& status); |
michael@0 | 65 | |
michael@0 | 66 | /** |
michael@0 | 67 | * Copy constructor. |
michael@0 | 68 | * |
michael@0 | 69 | * @param source the DecimalFormat object to be copied from. |
michael@0 | 70 | * @draft ICU 51 |
michael@0 | 71 | */ |
michael@0 | 72 | CompactDecimalFormat(const CompactDecimalFormat& source); |
michael@0 | 73 | |
michael@0 | 74 | /** |
michael@0 | 75 | * Destructor. |
michael@0 | 76 | * @draft ICU 51 |
michael@0 | 77 | */ |
michael@0 | 78 | virtual ~CompactDecimalFormat(); |
michael@0 | 79 | |
michael@0 | 80 | /** |
michael@0 | 81 | * Assignment operator. |
michael@0 | 82 | * |
michael@0 | 83 | * @param rhs the DecimalFormat object to be copied. |
michael@0 | 84 | * @draft ICU 51 |
michael@0 | 85 | */ |
michael@0 | 86 | CompactDecimalFormat& operator=(const CompactDecimalFormat& rhs); |
michael@0 | 87 | |
michael@0 | 88 | /** |
michael@0 | 89 | * Clone this Format object polymorphically. The caller owns the |
michael@0 | 90 | * result and should delete it when done. |
michael@0 | 91 | * |
michael@0 | 92 | * @return a polymorphic copy of this CompactDecimalFormat. |
michael@0 | 93 | * @draft ICU 51 |
michael@0 | 94 | */ |
michael@0 | 95 | virtual Format* clone() const; |
michael@0 | 96 | |
michael@0 | 97 | /** |
michael@0 | 98 | * Return TRUE if the given Format objects are semantically equal. |
michael@0 | 99 | * Objects of different subclasses are considered unequal. |
michael@0 | 100 | * |
michael@0 | 101 | * @param other the object to be compared with. |
michael@0 | 102 | * @return TRUE if the given Format objects are semantically equal. |
michael@0 | 103 | * @draft ICU 51 |
michael@0 | 104 | */ |
michael@0 | 105 | virtual UBool operator==(const Format& other) const; |
michael@0 | 106 | |
michael@0 | 107 | |
michael@0 | 108 | using DecimalFormat::format; |
michael@0 | 109 | |
michael@0 | 110 | /** |
michael@0 | 111 | * Format a double or long number using base-10 representation. |
michael@0 | 112 | * |
michael@0 | 113 | * @param number The value to be formatted. |
michael@0 | 114 | * @param appendTo Output parameter to receive result. |
michael@0 | 115 | * Result is appended to existing contents. |
michael@0 | 116 | * @param pos On input: an alignment field, if desired. |
michael@0 | 117 | * On output: the offsets of the alignment field. |
michael@0 | 118 | * @return Reference to 'appendTo' parameter. |
michael@0 | 119 | * @draft ICU 51 |
michael@0 | 120 | */ |
michael@0 | 121 | virtual UnicodeString& format(double number, |
michael@0 | 122 | UnicodeString& appendTo, |
michael@0 | 123 | FieldPosition& pos) const; |
michael@0 | 124 | |
michael@0 | 125 | /** |
michael@0 | 126 | * Format a double or long number using base-10 representation. |
michael@0 | 127 | * Currently sets status to U_UNSUPPORTED_ERROR. |
michael@0 | 128 | * |
michael@0 | 129 | * @param number The value to be formatted. |
michael@0 | 130 | * @param appendTo Output parameter to receive result. |
michael@0 | 131 | * Result is appended to existing contents. |
michael@0 | 132 | * @param posIter On return, can be used to iterate over positions |
michael@0 | 133 | * of fields generated by this format call. |
michael@0 | 134 | * Can be NULL. |
michael@0 | 135 | * @param status Output param filled with success/failure status. |
michael@0 | 136 | * @return Reference to 'appendTo' parameter. |
michael@0 | 137 | * @internal |
michael@0 | 138 | */ |
michael@0 | 139 | virtual UnicodeString& format(double number, |
michael@0 | 140 | UnicodeString& appendTo, |
michael@0 | 141 | FieldPositionIterator* posIter, |
michael@0 | 142 | UErrorCode& status) const; |
michael@0 | 143 | |
michael@0 | 144 | /** |
michael@0 | 145 | * Format an int64 number using base-10 representation. |
michael@0 | 146 | * |
michael@0 | 147 | * @param number The value to be formatted. |
michael@0 | 148 | * @param appendTo Output parameter to receive result. |
michael@0 | 149 | * Result is appended to existing contents. |
michael@0 | 150 | * @param pos On input: an alignment field, if desired. |
michael@0 | 151 | * On output: the offsets of the alignment field. |
michael@0 | 152 | * @return Reference to 'appendTo' parameter. |
michael@0 | 153 | * @draft ICU 51 |
michael@0 | 154 | */ |
michael@0 | 155 | virtual UnicodeString& format(int64_t number, |
michael@0 | 156 | UnicodeString& appendTo, |
michael@0 | 157 | FieldPosition& pos) const; |
michael@0 | 158 | |
michael@0 | 159 | /** |
michael@0 | 160 | * Format an int64 number using base-10 representation. |
michael@0 | 161 | * Currently sets status to U_UNSUPPORTED_ERROR |
michael@0 | 162 | * |
michael@0 | 163 | * @param number The value to be formatted. |
michael@0 | 164 | * @param appendTo Output parameter to receive result. |
michael@0 | 165 | * Result is appended to existing contents. |
michael@0 | 166 | * @param posIter On return, can be used to iterate over positions |
michael@0 | 167 | * of fields generated by this format call. |
michael@0 | 168 | * Can be NULL. |
michael@0 | 169 | * @param status Output param filled with success/failure status. |
michael@0 | 170 | * @return Reference to 'appendTo' parameter. |
michael@0 | 171 | * @internal |
michael@0 | 172 | */ |
michael@0 | 173 | virtual UnicodeString& format(int64_t number, |
michael@0 | 174 | UnicodeString& appendTo, |
michael@0 | 175 | FieldPositionIterator* posIter, |
michael@0 | 176 | UErrorCode& status) const; |
michael@0 | 177 | |
michael@0 | 178 | /** |
michael@0 | 179 | * Format a decimal number. Currently sets status to U_UNSUPPORTED_ERROR |
michael@0 | 180 | * The syntax of the unformatted number is a "numeric string" |
michael@0 | 181 | * as defined in the Decimal Arithmetic Specification, available at |
michael@0 | 182 | * http://speleotrove.com/decimal |
michael@0 | 183 | * |
michael@0 | 184 | * @param number The unformatted number, as a string. |
michael@0 | 185 | * @param appendTo Output parameter to receive result. |
michael@0 | 186 | * Result is appended to existing contents. |
michael@0 | 187 | * @param posIter On return, can be used to iterate over positions |
michael@0 | 188 | * of fields generated by this format call. |
michael@0 | 189 | * Can be NULL. |
michael@0 | 190 | * @param status Output param filled with success/failure status. |
michael@0 | 191 | * @return Reference to 'appendTo' parameter. |
michael@0 | 192 | * @internal |
michael@0 | 193 | */ |
michael@0 | 194 | virtual UnicodeString& format(const StringPiece &number, |
michael@0 | 195 | UnicodeString& appendTo, |
michael@0 | 196 | FieldPositionIterator* posIter, |
michael@0 | 197 | UErrorCode& status) const; |
michael@0 | 198 | |
michael@0 | 199 | /** |
michael@0 | 200 | * Format a decimal number. Currently sets status to U_UNSUPPORTED_ERROR |
michael@0 | 201 | * The number is a DigitList wrapper onto a floating point decimal number. |
michael@0 | 202 | * The default implementation in NumberFormat converts the decimal number |
michael@0 | 203 | * to a double and formats that. |
michael@0 | 204 | * |
michael@0 | 205 | * @param number The number, a DigitList format Decimal Floating Point. |
michael@0 | 206 | * @param appendTo Output parameter to receive result. |
michael@0 | 207 | * Result is appended to existing contents. |
michael@0 | 208 | * @param posIter On return, can be used to iterate over positions |
michael@0 | 209 | * of fields generated by this format call. |
michael@0 | 210 | * @param status Output param filled with success/failure status. |
michael@0 | 211 | * @return Reference to 'appendTo' parameter. |
michael@0 | 212 | * @internal |
michael@0 | 213 | */ |
michael@0 | 214 | virtual UnicodeString& format(const DigitList &number, |
michael@0 | 215 | UnicodeString& appendTo, |
michael@0 | 216 | FieldPositionIterator* posIter, |
michael@0 | 217 | UErrorCode& status) const; |
michael@0 | 218 | |
michael@0 | 219 | /** |
michael@0 | 220 | * Format a decimal number. Currently sets status to U_UNSUPPORTED_ERROR. |
michael@0 | 221 | * The number is a DigitList wrapper onto a floating point decimal number. |
michael@0 | 222 | * The default implementation in NumberFormat converts the decimal number |
michael@0 | 223 | * to a double and formats that. |
michael@0 | 224 | * |
michael@0 | 225 | * @param number The number, a DigitList format Decimal Floating Point. |
michael@0 | 226 | * @param appendTo Output parameter to receive result. |
michael@0 | 227 | * Result is appended to existing contents. |
michael@0 | 228 | * @param pos On input: an alignment field, if desired. |
michael@0 | 229 | * On output: the offsets of the alignment field. |
michael@0 | 230 | * @param status Output param filled with success/failure status. |
michael@0 | 231 | * @return Reference to 'appendTo' parameter. |
michael@0 | 232 | * @internal |
michael@0 | 233 | */ |
michael@0 | 234 | virtual UnicodeString& format(const DigitList &number, |
michael@0 | 235 | UnicodeString& appendTo, |
michael@0 | 236 | FieldPosition& pos, |
michael@0 | 237 | UErrorCode& status) const; |
michael@0 | 238 | |
michael@0 | 239 | /** |
michael@0 | 240 | * CompactDecimalFormat does not support parsing. This implementation |
michael@0 | 241 | * does nothing. |
michael@0 | 242 | * @param text Unused. |
michael@0 | 243 | * @param result Does not change. |
michael@0 | 244 | * @param parsePosition Does not change. |
michael@0 | 245 | * @see Formattable |
michael@0 | 246 | * @draft ICU 51 |
michael@0 | 247 | */ |
michael@0 | 248 | virtual void parse(const UnicodeString& text, |
michael@0 | 249 | Formattable& result, |
michael@0 | 250 | ParsePosition& parsePosition) const; |
michael@0 | 251 | |
michael@0 | 252 | /** |
michael@0 | 253 | * CompactDecimalFormat does not support parsing. This implementation |
michael@0 | 254 | * sets status to U_UNSUPPORTED_ERROR |
michael@0 | 255 | * |
michael@0 | 256 | * @param text Unused. |
michael@0 | 257 | * @param result Does not change. |
michael@0 | 258 | * @param status Always set to U_UNSUPPORTED_ERROR. |
michael@0 | 259 | * @draft ICU 51 |
michael@0 | 260 | */ |
michael@0 | 261 | virtual void parse(const UnicodeString& text, |
michael@0 | 262 | Formattable& result, |
michael@0 | 263 | UErrorCode& status) const; |
michael@0 | 264 | |
michael@0 | 265 | /* Cannot use #ifndef U_HIDE_INTERNAL_API for the following draft method since it is virtual */ |
michael@0 | 266 | /** |
michael@0 | 267 | * Parses text from the given string as a currency amount. Unlike |
michael@0 | 268 | * the parse() method, this method will attempt to parse a generic |
michael@0 | 269 | * currency name, searching for a match of this object's locale's |
michael@0 | 270 | * currency display names, or for a 3-letter ISO currency code. |
michael@0 | 271 | * This method will fail if this format is not a currency format, |
michael@0 | 272 | * that is, if it does not contain the currency pattern symbol |
michael@0 | 273 | * (U+00A4) in its prefix or suffix. This implementation always returns |
michael@0 | 274 | * NULL. |
michael@0 | 275 | * |
michael@0 | 276 | * @param text the string to parse |
michael@0 | 277 | * @param pos input-output position; on input, the position within text |
michael@0 | 278 | * to match; must have 0 <= pos.getIndex() < text.length(); |
michael@0 | 279 | * on output, the position after the last matched character. |
michael@0 | 280 | * If the parse fails, the position in unchanged upon output. |
michael@0 | 281 | * @return if parse succeeds, a pointer to a newly-created CurrencyAmount |
michael@0 | 282 | * object (owned by the caller) containing information about |
michael@0 | 283 | * the parsed currency; if parse fails, this is NULL. |
michael@0 | 284 | * @internal |
michael@0 | 285 | */ |
michael@0 | 286 | virtual CurrencyAmount* parseCurrency(const UnicodeString& text, |
michael@0 | 287 | ParsePosition& pos) const; |
michael@0 | 288 | |
michael@0 | 289 | /** |
michael@0 | 290 | * Return the class ID for this class. This is useful only for |
michael@0 | 291 | * comparing to a return value from getDynamicClassID(). For example: |
michael@0 | 292 | * <pre> |
michael@0 | 293 | * . Base* polymorphic_pointer = createPolymorphicObject(); |
michael@0 | 294 | * . if (polymorphic_pointer->getDynamicClassID() == |
michael@0 | 295 | * . Derived::getStaticClassID()) ... |
michael@0 | 296 | * </pre> |
michael@0 | 297 | * @return The class ID for all objects of this class. |
michael@0 | 298 | * @draft ICU 51 |
michael@0 | 299 | */ |
michael@0 | 300 | static UClassID U_EXPORT2 getStaticClassID(); |
michael@0 | 301 | |
michael@0 | 302 | /** |
michael@0 | 303 | * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. |
michael@0 | 304 | * This method is to implement a simple version of RTTI, since not all |
michael@0 | 305 | * C++ compilers support genuine RTTI. Polymorphic operator==() and |
michael@0 | 306 | * clone() methods call this method. |
michael@0 | 307 | * |
michael@0 | 308 | * @return The class ID for this object. All objects of a |
michael@0 | 309 | * given class have the same class ID. Objects of |
michael@0 | 310 | * other classes have different class IDs. |
michael@0 | 311 | * @draft ICU 51 |
michael@0 | 312 | */ |
michael@0 | 313 | virtual UClassID getDynamicClassID() const; |
michael@0 | 314 | |
michael@0 | 315 | private: |
michael@0 | 316 | |
michael@0 | 317 | const UHashtable* _unitsByVariant; |
michael@0 | 318 | const double* _divisors; |
michael@0 | 319 | PluralRules* _pluralRules; |
michael@0 | 320 | |
michael@0 | 321 | // Default constructor not implemented. |
michael@0 | 322 | CompactDecimalFormat(const DecimalFormat &, const UHashtable* unitsByVariant, const double* divisors, PluralRules* pluralRules); |
michael@0 | 323 | |
michael@0 | 324 | UBool eqHelper(const CompactDecimalFormat& that) const; |
michael@0 | 325 | }; |
michael@0 | 326 | |
michael@0 | 327 | U_NAMESPACE_END |
michael@0 | 328 | |
michael@0 | 329 | #endif /* U_HIDE_DRAFT_API */ |
michael@0 | 330 | #endif /* #if !UCONFIG_NO_FORMATTING */ |
michael@0 | 331 | |
michael@0 | 332 | #endif // __COMPACT_DECIMAL_FORMAT_H__ |
michael@0 | 333 | //eof |