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
;
31 * A plural aware formatter that is good for expressing a single quantity and
34 * First use the add() methods to add a pattern for each plural variant.
35 * There must be a pattern for the "other" variant.
36 * Then use the format() method.
38 * Concurrent calls only to const methods on a QuantityFormatter object are
39 * safe, but concurrent const and non-const method calls on a QuantityFormatter
40 * object are not safe and require synchronization.
43 class U_I18N_API QuantityFormatter
: public UMemory
{
46 * Default constructor.
53 QuantityFormatter(const QuantityFormatter
& other
);
58 QuantityFormatter
&operator=(const QuantityFormatter
& other
);
66 * Removes all variants from this object including the "other" variant.
71 * Adds a plural variant if there is none yet for the plural form.
73 * @param variant "zero", "one", "two", "few", "many", "other"
74 * @param rawPattern the pattern for the variant e.g "{0} meters"
75 * @param status any error returned here.
76 * @return TRUE on success; FALSE if status was set to a non zero error.
78 UBool
addIfAbsent(const char *variant
, const UnicodeString
&rawPattern
, UErrorCode
&status
);
81 * returns TRUE if this object has at least the "other" variant.
83 UBool
isValid() const;
86 * Gets the pattern formatter that would be used for a particular variant.
87 * If isValid() returns TRUE, this method is guaranteed to return a
90 const SimpleFormatter
*getByVariant(const char *variant
) const;
93 * Formats a number with this object appending the result to appendTo.
94 * At least the "other" variant must be added to this object for this
97 * @param number the single number.
98 * @param fmt formats the number
99 * @param rules computes the plural variant to use.
100 * @param appendTo result appended here.
101 * @param status any error returned here.
104 UnicodeString
&format(
105 const Formattable
&number
,
106 const NumberFormat
&fmt
,
107 const PluralRules
&rules
,
108 UnicodeString
&appendTo
,
110 UErrorCode
&status
) const;
113 * Selects the standard plural form for the number/formatter/rules.
115 static StandardPlural::Form
selectPlural(
116 const Formattable
&number
,
117 const NumberFormat
&fmt
,
118 const PluralRules
&rules
,
119 UnicodeString
&formattedNumber
,
124 * Formats the pattern with the value and adjusts the FieldPosition.
126 static UnicodeString
&format(
127 const SimpleFormatter
&pattern
,
128 const UnicodeString
&value
,
129 UnicodeString
&appendTo
,
134 SimpleFormatter
*formatters
[StandardPlural::COUNT
];