]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/quantityformatter.h
ICU-64232.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / quantityformatter.h
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
57a6839d
A
3/*
4******************************************************************************
2ca993e8 5* Copyright (C) 2014-2016, International Business Machines
57a6839d
A
6* Corporation and others. All Rights Reserved.
7******************************************************************************
8* quantityformatter.h
9*/
10
11#ifndef __QUANTITY_FORMATTER_H__
12#define __QUANTITY_FORMATTER_H__
13
14#include "unicode/utypes.h"
15#include "unicode/uobject.h"
16
b331163b
A
17#if !UCONFIG_NO_FORMATTING
18
2ca993e8
A
19#include "standardplural.h"
20
57a6839d
A
21U_NAMESPACE_BEGIN
22
2ca993e8 23class SimpleFormatter;
57a6839d
A
24class UnicodeString;
25class PluralRules;
26class NumberFormat;
27class Formattable;
28class FieldPosition;
29
3d1f044b
A
30namespace number {
31namespace impl {
32class NumberStringBuilder;
33}
34}
35
57a6839d
A
36/**
37 * A plural aware formatter that is good for expressing a single quantity and
38 * a unit.
39 * <p>
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.
43 * <p>
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.
47 *
48 */
49class U_I18N_API QuantityFormatter : public UMemory {
57a6839d
A
50public:
51 /**
52 * Default constructor.
53 */
54 QuantityFormatter();
55
56 /**
57 * Copy constructor.
58 */
59 QuantityFormatter(const QuantityFormatter& other);
60
61 /**
62 * Assignment operator
63 */
64 QuantityFormatter &operator=(const QuantityFormatter& other);
65
66 /**
67 * Destructor.
68 */
69 ~QuantityFormatter();
70
71 /**
72 * Removes all variants from this object including the "other" variant.
73 */
74 void reset();
75
76 /**
2ca993e8
A
77 * Adds a plural variant if there is none yet for the plural form.
78 *
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.
83 */
84 UBool addIfAbsent(const char *variant, const UnicodeString &rawPattern, UErrorCode &status);
57a6839d
A
85
86 /**
87 * returns TRUE if this object has at least the "other" variant.
88 */
89 UBool isValid() const;
90
b331163b
A
91 /**
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
94 * non-NULL value.
95 */
2ca993e8 96 const SimpleFormatter *getByVariant(const char *variant) const;
b331163b 97
57a6839d 98 /**
2ca993e8 99 * Formats a number with this object appending the result to appendTo.
57a6839d
A
100 * At least the "other" variant must be added to this object for this
101 * method to work.
102 *
2ca993e8
A
103 * @param number the single number.
104 * @param fmt formats the number
57a6839d
A
105 * @param rules computes the plural variant to use.
106 * @param appendTo result appended here.
107 * @param status any error returned here.
108 * @return appendTo
109 */
110 UnicodeString &format(
2ca993e8 111 const Formattable &number,
57a6839d
A
112 const NumberFormat &fmt,
113 const PluralRules &rules,
114 UnicodeString &appendTo,
115 FieldPosition &pos,
116 UErrorCode &status) const;
117
2ca993e8
A
118 /**
119 * Selects the standard plural form for the number/formatter/rules.
3d1f044b 120 * TODO(13591): Remove this method.
2ca993e8
A
121 */
122 static StandardPlural::Form selectPlural(
123 const Formattable &number,
124 const NumberFormat &fmt,
125 const PluralRules &rules,
126 UnicodeString &formattedNumber,
127 FieldPosition &pos,
128 UErrorCode &status);
129
3d1f044b
A
130 /**
131 * Formats a quantity and selects its plural form. The output is appended
132 * to a NumberStringBuilder in order to retain field information.
133 *
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
137 * formatted number.
138 * @param output Where to append the result of the format operation.
139 * @param pluralForm Output variable populated with the plural form of the
140 * formatted number.
141 * @param status Set if an error occurs.
142 */
143 static void formatAndSelect(
144 double quantity,
145 const NumberFormat& fmt,
146 const PluralRules& rules,
147 number::impl::NumberStringBuilder& output,
148 StandardPlural::Form& pluralForm,
149 UErrorCode& status);
150
2ca993e8
A
151 /**
152 * Formats the pattern with the value and adjusts the FieldPosition.
3d1f044b 153 * TODO: Remove?
2ca993e8
A
154 */
155 static UnicodeString &format(
156 const SimpleFormatter &pattern,
157 const UnicodeString &value,
158 UnicodeString &appendTo,
159 FieldPosition &pos,
160 UErrorCode &status);
161
57a6839d 162private:
2ca993e8 163 SimpleFormatter *formatters[StandardPlural::COUNT];
57a6839d
A
164};
165
166U_NAMESPACE_END
167
168#endif
b331163b
A
169
170#endif