2 ******************************************************************************
3 * Copyright (C) 2014-2016, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ******************************************************************************
9 #ifndef __QUANTITY_FORMATTER_H__
10 #define __QUANTITY_FORMATTER_H__
12 #include "unicode/utypes.h"
13 #include "unicode/uobject.h"
15 #if !UCONFIG_NO_FORMATTING
17 #include "standardplural.h"
21 class SimpleFormatter
;
29 * A plural aware formatter that is good for expressing a single quantity and
32 * First use the add() methods to add a pattern for each plural variant.
33 * There must be a pattern for the "other" variant.
34 * Then use the format() method.
36 * Concurrent calls only to const methods on a QuantityFormatter object are
37 * safe, but concurrent const and non-const method calls on a QuantityFormatter
38 * object are not safe and require synchronization.
41 class U_I18N_API QuantityFormatter
: public UMemory
{
44 * Default constructor.
51 QuantityFormatter(const QuantityFormatter
& other
);
56 QuantityFormatter
&operator=(const QuantityFormatter
& other
);
64 * Removes all variants from this object including the "other" variant.
69 * Adds a plural variant if there is none yet for the plural form.
71 * @param variant "zero", "one", "two", "few", "many", "other"
72 * @param rawPattern the pattern for the variant e.g "{0} meters"
73 * @param status any error returned here.
74 * @return TRUE on success; FALSE if status was set to a non zero error.
76 UBool
addIfAbsent(const char *variant
, const UnicodeString
&rawPattern
, UErrorCode
&status
);
79 * returns TRUE if this object has at least the "other" variant.
81 UBool
isValid() const;
84 * Gets the pattern formatter that would be used for a particular variant.
85 * If isValid() returns TRUE, this method is guaranteed to return a
88 const SimpleFormatter
*getByVariant(const char *variant
) const;
91 * Formats a number with this object appending the result to appendTo.
92 * At least the "other" variant must be added to this object for this
95 * @param number the single number.
96 * @param fmt formats the number
97 * @param rules computes the plural variant to use.
98 * @param appendTo result appended here.
99 * @param status any error returned here.
102 UnicodeString
&format(
103 const Formattable
&number
,
104 const NumberFormat
&fmt
,
105 const PluralRules
&rules
,
106 UnicodeString
&appendTo
,
108 UErrorCode
&status
) const;
111 * Selects the standard plural form for the number/formatter/rules.
113 static StandardPlural::Form
selectPlural(
114 const Formattable
&number
,
115 const NumberFormat
&fmt
,
116 const PluralRules
&rules
,
117 UnicodeString
&formattedNumber
,
122 * Formats the pattern with the value and adjusts the FieldPosition.
124 static UnicodeString
&format(
125 const SimpleFormatter
&pattern
,
126 const UnicodeString
&value
,
127 UnicodeString
&appendTo
,
132 SimpleFormatter
*formatters
[StandardPlural::COUNT
];