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 15U_NAMESPACE_BEGIN 16 17class SimplePatternFormatter; 18class UnicodeString; 19class PluralRules; 20class NumberFormat; 21class Formattable; 22class FieldPosition; 23 24/** 25 * A plural aware formatter that is good for expressing a single quantity and 26 * a unit. 27 * <p> 28 * First use the add() methods to add a pattern for each plural variant. 29 * There must be a pattern for the "other" variant. 30 * Then use the format() method. 31 * <p> 32 * Concurrent calls only to const methods on a QuantityFormatter object are 33 * safe, but concurrent const and non-const method calls on a QuantityFormatter 34 * object are not safe and require synchronization. 35 * 36 */ 37class U_I18N_API QuantityFormatter : public UMemory { 38// TODO(Travis Keep): Add test for copy constructor, assignment, and reset. 39public: 40 /** 41 * Default constructor. 42 */ 43 QuantityFormatter(); 44 45 /** 46 * Copy constructor. 47 */ 48 QuantityFormatter(const QuantityFormatter& other); 49 50 /** 51 * Assignment operator 52 */ 53 QuantityFormatter &operator=(const QuantityFormatter& other); 54 55 /** 56 * Destructor. 57 */ 58 ~QuantityFormatter(); 59 60 /** 61 * Removes all variants from this object including the "other" variant. 62 */ 63 void reset(); 64 65 /** 66 * Adds a plural variant. 67 * 68 * @param variant "zero", "one", "two", "few", "many", "other" 69 * @param rawPattern the pattern for the variant e.g "{0} meters" 70 * @param status any error returned here. 71 * @return TRUE on success; FALSE otherwise. 72 */ 73 UBool add( 74 const char *variant, 75 const UnicodeString &rawPattern, 76 UErrorCode &status); 77 78 /** 79 * returns TRUE if this object has at least the "other" variant. 80 */ 81 UBool isValid() const; 82 83 /** 84 * Formats a quantity with this object appending the result to appendTo. 85 * At least the "other" variant must be added to this object for this 86 * method to work. 87 * 88 * @param quantity the single quantity. 89 * @param fmt formats the quantity 90 * @param rules computes the plural variant to use. 91 * @param appendTo result appended here. 92 * @param status any error returned here. 93 * @return appendTo 94 */ 95 UnicodeString &format( 96 const Formattable &quantity, 97 const NumberFormat &fmt, 98 const PluralRules &rules, 99 UnicodeString &appendTo, 100 FieldPosition &pos, 101 UErrorCode &status) const; 102 103private: 104 SimplePatternFormatter *formatters[6]; 105}; 106 107U_NAMESPACE_END 108 109#endif 110