1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ******************************************************************************
5 * Copyright (C) 2014-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
11 #ifndef __QUANTITY_FORMATTER_H__
12 #define __QUANTITY_FORMATTER_H__
14 #include "unicode/utypes.h"
15 #include "unicode/uobject.h"
17 #if !UCONFIG_NO_FORMATTING
19 #include "standardplural.h"
23 class SimpleFormatter
;
32 class NumberStringBuilder
;
37 * A plural aware formatter that is good for expressing a single quantity and
40 * First use the add() methods to add a pattern for each plural variant.
41 * There must be a pattern for the "other" variant.
42 * Then use the format() method.
44 * Concurrent calls only to const methods on a QuantityFormatter object are
45 * safe, but concurrent const and non-const method calls on a QuantityFormatter
46 * object are not safe and require synchronization.
49 class U_I18N_API QuantityFormatter
: public UMemory
{
52 * Default constructor.
59 QuantityFormatter(const QuantityFormatter
& other
);
64 QuantityFormatter
&operator=(const QuantityFormatter
& other
);
72 * Removes all variants from this object including the "other" variant.
77 * Adds a plural variant if there is none yet for the plural form.
79 * @param variant "zero", "one", "two", "few", "many", "other"
80 * @param rawPattern the pattern for the variant e.g "{0} meters"
81 * @param status any error returned here.
82 * @return TRUE on success; FALSE if status was set to a non zero error.
84 UBool
addIfAbsent(const char *variant
, const UnicodeString
&rawPattern
, UErrorCode
&status
);
87 * returns TRUE if this object has at least the "other" variant.
89 UBool
isValid() const;
92 * Gets the pattern formatter that would be used for a particular variant.
93 * If isValid() returns TRUE, this method is guaranteed to return a
96 const SimpleFormatter
*getByVariant(const char *variant
) const;
99 * Formats a number with this object appending the result to appendTo.
100 * At least the "other" variant must be added to this object for this
103 * @param number the single number.
104 * @param fmt formats the number
105 * @param rules computes the plural variant to use.
106 * @param appendTo result appended here.
107 * @param status any error returned here.
110 UnicodeString
&format(
111 const Formattable
&number
,
112 const NumberFormat
&fmt
,
113 const PluralRules
&rules
,
114 UnicodeString
&appendTo
,
116 UErrorCode
&status
) const;
119 * Selects the standard plural form for the number/formatter/rules.
120 * TODO(13591): Remove this method.
122 static StandardPlural::Form
selectPlural(
123 const Formattable
&number
,
124 const NumberFormat
&fmt
,
125 const PluralRules
&rules
,
126 UnicodeString
&formattedNumber
,
131 * Formats a quantity and selects its plural form. The output is appended
132 * to a NumberStringBuilder in order to retain field information.
134 * @param quantity The number to format.
135 * @param fmt The formatter to use to format the number.
136 * @param rules The rules to use to select the plural form of the
138 * @param output Where to append the result of the format operation.
139 * @param pluralForm Output variable populated with the plural form of the
141 * @param status Set if an error occurs.
143 static void formatAndSelect(
145 const NumberFormat
& fmt
,
146 const PluralRules
& rules
,
147 number::impl::NumberStringBuilder
& output
,
148 StandardPlural::Form
& pluralForm
,
152 * Formats the pattern with the value and adjusts the FieldPosition.
155 static UnicodeString
&format(
156 const SimpleFormatter
&pattern
,
157 const UnicodeString
&value
,
158 UnicodeString
&appendTo
,
163 SimpleFormatter
*formatters
[StandardPlural::COUNT
];