1 /* 2 ****************************************************************************** 3 * Copyright (C) 2014-2015, 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 #include "standardplural.h" 18 19 U_NAMESPACE_BEGIN 20 21 class SimplePatternFormatter; 22 class UnicodeString; 23 class PluralRules; 24 class NumberFormat; 25 class Formattable; 26 class FieldPosition; 27 28 /** 29 * A plural aware formatter that is good for expressing a single quantity and 30 * a unit. 31 * <p> 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. 35 * <p> 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. 39 * 40 */ 41 class U_I18N_API QuantityFormatter : public UMemory { 42 public: 43 /** 44 * Default constructor. 45 */ 46 QuantityFormatter(); 47 48 /** 49 * Copy constructor. 50 */ 51 QuantityFormatter(const QuantityFormatter& other); 52 53 /** 54 * Assignment operator 55 */ 56 QuantityFormatter &operator=(const QuantityFormatter& other); 57 58 /** 59 * Destructor. 60 */ 61 ~QuantityFormatter(); 62 63 /** 64 * Removes all variants from this object including the "other" variant. 65 */ 66 void reset(); 67 68 /** 69 * Adds a plural variant if there is none yet for the plural form. 70 * 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. 75 */ 76 UBool addIfAbsent(const char *variant, const UnicodeString &rawPattern, UErrorCode &status); 77 78 /** 79 * returns TRUE if this object has at least the "other" variant. 80 */ 81 UBool isValid() const; 82 83 /** 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 86 * non-NULL value. 87 */ 88 const SimplePatternFormatter *getByVariant(const char *variant) const; 89 90 /** 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 93 * method to work. 94 * 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. 100 * @return appendTo 101 */ 102 UnicodeString &format( 103 const Formattable &number, 104 const NumberFormat &fmt, 105 const PluralRules &rules, 106 UnicodeString &appendTo, 107 FieldPosition &pos, 108 UErrorCode &status) const; 109 110 /** 111 * Selects the standard plural form for the number/formatter/rules. 112 */ 113 static StandardPlural::Form selectPlural( 114 const Formattable &number, 115 const NumberFormat &fmt, 116 const PluralRules &rules, 117 UnicodeString &formattedNumber, 118 FieldPosition &pos, 119 UErrorCode &status); 120 121 /** 122 * Formats the pattern with the value and adjusts the FieldPosition. 123 */ 124 static UnicodeString &format( 125 const SimplePatternFormatter &pattern, 126 const UnicodeString &value, 127 UnicodeString &appendTo, 128 FieldPosition &pos, 129 UErrorCode &status); 130 131 private: 132 SimplePatternFormatter *formatters[StandardPlural::COUNT]; 133 }; 134 135 U_NAMESPACE_END 136 137 #endif 138 139 #endif 140