quantityformatter.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. ******************************************************************************
  5. * Copyright (C) 2014-2016, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. ******************************************************************************
  8. * quantityformatter.h
  9. */
  10. #ifndef __QUANTITY_FORMATTER_H__
  11. #define __QUANTITY_FORMATTER_H__
  12. #include "unicode/utypes.h"
  13. #include "unicode/uobject.h"
  14. #if !UCONFIG_NO_FORMATTING
  15. #include "standardplural.h"
  16. U_NAMESPACE_BEGIN
  17. class SimpleFormatter;
  18. class UnicodeString;
  19. class PluralRules;
  20. class NumberFormat;
  21. class Formattable;
  22. class FieldPosition;
  23. class FormattedStringBuilder;
  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. */
  37. class U_I18N_API QuantityFormatter : public UMemory {
  38. public:
  39. /**
  40. * Default constructor.
  41. */
  42. QuantityFormatter();
  43. /**
  44. * Copy constructor.
  45. */
  46. QuantityFormatter(const QuantityFormatter& other);
  47. /**
  48. * Assignment operator
  49. */
  50. QuantityFormatter &operator=(const QuantityFormatter& other);
  51. /**
  52. * Destructor.
  53. */
  54. ~QuantityFormatter();
  55. /**
  56. * Removes all variants from this object including the "other" variant.
  57. */
  58. void reset();
  59. /**
  60. * Adds a plural variant if there is none yet for the plural form.
  61. *
  62. * @param variant "zero", "one", "two", "few", "many", "other"
  63. * @param rawPattern the pattern for the variant e.g "{0} meters"
  64. * @param status any error returned here.
  65. * @return TRUE on success; FALSE if status was set to a non zero error.
  66. */
  67. UBool addIfAbsent(const char *variant, const UnicodeString &rawPattern, UErrorCode &status);
  68. /**
  69. * returns TRUE if this object has at least the "other" variant.
  70. */
  71. UBool isValid() const;
  72. /**
  73. * Gets the pattern formatter that would be used for a particular variant.
  74. * If isValid() returns TRUE, this method is guaranteed to return a
  75. * non-NULL value.
  76. */
  77. const SimpleFormatter *getByVariant(const char *variant) const;
  78. /**
  79. * Formats a number with this object appending the result to appendTo.
  80. * At least the "other" variant must be added to this object for this
  81. * method to work.
  82. *
  83. * @param number the single number.
  84. * @param fmt formats the number
  85. * @param rules computes the plural variant to use.
  86. * @param appendTo result appended here.
  87. * @param status any error returned here.
  88. * @return appendTo
  89. */
  90. UnicodeString &format(
  91. const Formattable &number,
  92. const NumberFormat &fmt,
  93. const PluralRules &rules,
  94. UnicodeString &appendTo,
  95. FieldPosition &pos,
  96. UErrorCode &status) const;
  97. /**
  98. * Selects the standard plural form for the number/formatter/rules.
  99. * TODO(13591): Remove this method.
  100. */
  101. static StandardPlural::Form selectPlural(
  102. const Formattable &number,
  103. const NumberFormat &fmt,
  104. const PluralRules &rules,
  105. UnicodeString &formattedNumber,
  106. FieldPosition &pos,
  107. UErrorCode &status);
  108. /**
  109. * Formats a quantity and selects its plural form. The output is appended
  110. * to a FormattedStringBuilder in order to retain field information.
  111. *
  112. * @param quantity The number to format.
  113. * @param fmt The formatter to use to format the number.
  114. * @param rules The rules to use to select the plural form of the
  115. * formatted number.
  116. * @param output Where to append the result of the format operation.
  117. * @param pluralForm Output variable populated with the plural form of the
  118. * formatted number.
  119. * @param status Set if an error occurs.
  120. */
  121. static void formatAndSelect(
  122. double quantity,
  123. const NumberFormat& fmt,
  124. const PluralRules& rules,
  125. FormattedStringBuilder& output,
  126. StandardPlural::Form& pluralForm,
  127. UErrorCode& status);
  128. /**
  129. * Formats the pattern with the value and adjusts the FieldPosition.
  130. * TODO: Remove?
  131. */
  132. static UnicodeString &format(
  133. const SimpleFormatter &pattern,
  134. const UnicodeString &value,
  135. UnicodeString &appendTo,
  136. FieldPosition &pos,
  137. UErrorCode &status);
  138. private:
  139. SimpleFormatter *formatters[StandardPlural::COUNT];
  140. };
  141. U_NAMESPACE_END
  142. #endif
  143. #endif