1 /*
2 ******************************************************************************
3 * Copyright (C) 2014, International Business Machines
4 * Corporation and others.  All Rights Reserved.
5 ******************************************************************************
6 * quantityformatter.h
7 */
8 
9 #ifndef __QUANTITY_FORMATTER_H__
10 #define __QUANTITY_FORMATTER_H__
11 
12 #include "unicode/utypes.h"
13 #include "unicode/uobject.h"
14 
15 #if !UCONFIG_NO_FORMATTING
16 
17 U_NAMESPACE_BEGIN
18 
19 class SimplePatternFormatter;
20 class UnicodeString;
21 class PluralRules;
22 class NumberFormat;
23 class Formattable;
24 class FieldPosition;
25 
26 /**
27  * A plural aware formatter that is good for expressing a single quantity and
28  * a unit.
29  * <p>
30  * First use the add() methods to add a pattern for each plural variant.
31  * There must be a pattern for the "other" variant.
32  * Then use the format() method.
33  * <p>
34  * Concurrent calls only to const methods on a QuantityFormatter object are
35  * safe, but concurrent const and non-const method calls on a QuantityFormatter
36  * object are not safe and require synchronization.
37  *
38  */
39 class U_I18N_API QuantityFormatter : public UMemory {
40 public:
41     /**
42      * Default constructor.
43      */
44     QuantityFormatter();
45 
46     /**
47      * Copy constructor.
48      */
49     QuantityFormatter(const QuantityFormatter& other);
50 
51     /**
52      * Assignment operator
53      */
54     QuantityFormatter &operator=(const QuantityFormatter& other);
55 
56     /**
57      * Destructor.
58      */
59     ~QuantityFormatter();
60 
61     /**
62      * Removes all variants from this object including the "other" variant.
63      */
64     void reset();
65 
66     /**
67       * Adds a plural variant.
68       *
69       * @param variant "zero", "one", "two", "few", "many", "other"
70       * @param rawPattern the pattern for the variant e.g "{0} meters"
71       * @param status any error returned here.
72       * @return TRUE on success; FALSE if status was set to a non zero error.
73       */
74     UBool add(
75             const char *variant,
76             const UnicodeString &rawPattern,
77             UErrorCode &status);
78 
79     /**
80      * returns TRUE if this object has at least the "other" variant.
81      */
82     UBool isValid() const;
83 
84     /**
85      * Gets the pattern formatter that would be used for a particular variant.
86      * If isValid() returns TRUE, this method is guaranteed to return a
87      * non-NULL value.
88      */
89     const SimplePatternFormatter *getByVariant(const char *variant) const;
90 
91     /**
92      * Formats a quantity with this object appending the result to appendTo.
93      * At least the "other" variant must be added to this object for this
94      * method to work.
95      *
96      * @param quantity the single quantity.
97      * @param fmt formats the quantity
98      * @param rules computes the plural variant to use.
99      * @param appendTo result appended here.
100      * @param status any error returned here.
101      * @return appendTo
102      */
103     UnicodeString &format(
104             const Formattable &quantity,
105             const NumberFormat &fmt,
106             const PluralRules &rules,
107             UnicodeString &appendTo,
108             FieldPosition &pos,
109             UErrorCode &status) const;
110 
111 private:
112     SimplePatternFormatter *formatters[6];
113 };
114 
115 U_NAMESPACE_END
116 
117 #endif
118 
119 #endif
120