2017-01-20 00:20:31 +00:00
|
|
|
// © 2016 and later: Unicode, Inc. and others.
|
2016-06-15 18:58:17 +00:00
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
2014-01-29 05:08:55 +00:00
|
|
|
/*
|
|
|
|
******************************************************************************
|
2016-05-31 21:45:07 +00:00
|
|
|
* Copyright (C) 2014-2016, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
2014-01-29 05:08:55 +00:00
|
|
|
******************************************************************************
|
|
|
|
* quantityformatter.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __QUANTITY_FORMATTER_H__
|
|
|
|
#define __QUANTITY_FORMATTER_H__
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
#include "unicode/uobject.h"
|
|
|
|
|
2014-09-13 00:53:06 +00:00
|
|
|
#if !UCONFIG_NO_FORMATTING
|
|
|
|
|
2015-12-14 21:57:41 +00:00
|
|
|
#include "standardplural.h"
|
2015-12-02 22:54:54 +00:00
|
|
|
|
2014-01-29 05:08:55 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2016-01-23 05:20:23 +00:00
|
|
|
class SimpleFormatter;
|
2014-01-29 05:08:55 +00:00
|
|
|
class UnicodeString;
|
|
|
|
class PluralRules;
|
|
|
|
class NumberFormat;
|
|
|
|
class Formattable;
|
2014-02-04 00:29:17 +00:00
|
|
|
class FieldPosition;
|
2019-04-05 23:43:19 +00:00
|
|
|
class FormattedStringBuilder;
|
2019-02-09 06:08:16 +00:00
|
|
|
|
2014-01-29 05:08:55 +00:00
|
|
|
/**
|
2014-01-29 22:57:06 +00:00
|
|
|
* A plural aware formatter that is good for expressing a single quantity and
|
2014-01-29 05:08:55 +00:00
|
|
|
* a unit.
|
|
|
|
* <p>
|
|
|
|
* First use the add() methods to add a pattern for each plural variant.
|
|
|
|
* There must be a pattern for the "other" variant.
|
|
|
|
* Then use the format() method.
|
|
|
|
* <p>
|
|
|
|
* Concurrent calls only to const methods on a QuantityFormatter object are
|
|
|
|
* safe, but concurrent const and non-const method calls on a QuantityFormatter
|
|
|
|
* object are not safe and require synchronization.
|
|
|
|
*
|
|
|
|
*/
|
2014-01-29 05:52:11 +00:00
|
|
|
class U_I18N_API QuantityFormatter : public UMemory {
|
2014-01-29 05:08:55 +00:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Default constructor.
|
|
|
|
*/
|
|
|
|
QuantityFormatter();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy constructor.
|
|
|
|
*/
|
|
|
|
QuantityFormatter(const QuantityFormatter& other);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assignment operator
|
|
|
|
*/
|
|
|
|
QuantityFormatter &operator=(const QuantityFormatter& other);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*/
|
|
|
|
~QuantityFormatter();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes all variants from this object including the "other" variant.
|
|
|
|
*/
|
|
|
|
void reset();
|
|
|
|
|
|
|
|
/**
|
2015-12-02 22:54:54 +00:00
|
|
|
* Adds a plural variant if there is none yet for the plural form.
|
|
|
|
*
|
|
|
|
* @param variant "zero", "one", "two", "few", "many", "other"
|
|
|
|
* @param rawPattern the pattern for the variant e.g "{0} meters"
|
|
|
|
* @param status any error returned here.
|
|
|
|
* @return TRUE on success; FALSE if status was set to a non zero error.
|
|
|
|
*/
|
2015-12-16 04:41:33 +00:00
|
|
|
UBool addIfAbsent(const char *variant, const UnicodeString &rawPattern, UErrorCode &status);
|
2014-01-29 05:08:55 +00:00
|
|
|
|
2014-02-04 00:29:17 +00:00
|
|
|
/**
|
|
|
|
* returns TRUE if this object has at least the "other" variant.
|
|
|
|
*/
|
|
|
|
UBool isValid() const;
|
|
|
|
|
2014-09-10 20:06:52 +00:00
|
|
|
/**
|
|
|
|
* Gets the pattern formatter that would be used for a particular variant.
|
|
|
|
* If isValid() returns TRUE, this method is guaranteed to return a
|
|
|
|
* non-NULL value.
|
|
|
|
*/
|
2016-01-23 05:20:23 +00:00
|
|
|
const SimpleFormatter *getByVariant(const char *variant) const;
|
2014-09-10 20:06:52 +00:00
|
|
|
|
2014-01-29 05:08:55 +00:00
|
|
|
/**
|
2015-12-16 04:41:33 +00:00
|
|
|
* Formats a number with this object appending the result to appendTo.
|
2014-01-29 05:08:55 +00:00
|
|
|
* At least the "other" variant must be added to this object for this
|
|
|
|
* method to work.
|
|
|
|
*
|
2015-12-16 04:41:33 +00:00
|
|
|
* @param number the single number.
|
|
|
|
* @param fmt formats the number
|
2014-01-29 05:08:55 +00:00
|
|
|
* @param rules computes the plural variant to use.
|
|
|
|
* @param appendTo result appended here.
|
|
|
|
* @param status any error returned here.
|
|
|
|
* @return appendTo
|
|
|
|
*/
|
|
|
|
UnicodeString &format(
|
2015-12-16 04:41:33 +00:00
|
|
|
const Formattable &number,
|
2014-01-29 05:08:55 +00:00
|
|
|
const NumberFormat &fmt,
|
|
|
|
const PluralRules &rules,
|
|
|
|
UnicodeString &appendTo,
|
2014-02-04 00:29:17 +00:00
|
|
|
FieldPosition &pos,
|
2014-01-29 05:08:55 +00:00
|
|
|
UErrorCode &status) const;
|
|
|
|
|
2015-12-16 04:41:33 +00:00
|
|
|
/**
|
|
|
|
* Selects the standard plural form for the number/formatter/rules.
|
2019-02-09 06:08:16 +00:00
|
|
|
* TODO(13591): Remove this method.
|
2015-12-16 04:41:33 +00:00
|
|
|
*/
|
|
|
|
static StandardPlural::Form selectPlural(
|
|
|
|
const Formattable &number,
|
|
|
|
const NumberFormat &fmt,
|
|
|
|
const PluralRules &rules,
|
|
|
|
UnicodeString &formattedNumber,
|
|
|
|
FieldPosition &pos,
|
2015-12-02 22:54:54 +00:00
|
|
|
UErrorCode &status);
|
|
|
|
|
2019-02-09 06:08:16 +00:00
|
|
|
/**
|
|
|
|
* Formats a quantity and selects its plural form. The output is appended
|
2019-04-05 23:43:19 +00:00
|
|
|
* to a FormattedStringBuilder in order to retain field information.
|
2019-02-09 06:08:16 +00:00
|
|
|
*
|
|
|
|
* @param quantity The number to format.
|
|
|
|
* @param fmt The formatter to use to format the number.
|
|
|
|
* @param rules The rules to use to select the plural form of the
|
|
|
|
* formatted number.
|
|
|
|
* @param output Where to append the result of the format operation.
|
|
|
|
* @param pluralForm Output variable populated with the plural form of the
|
|
|
|
* formatted number.
|
|
|
|
* @param status Set if an error occurs.
|
|
|
|
*/
|
|
|
|
static void formatAndSelect(
|
|
|
|
double quantity,
|
|
|
|
const NumberFormat& fmt,
|
|
|
|
const PluralRules& rules,
|
2019-04-05 23:43:19 +00:00
|
|
|
FormattedStringBuilder& output,
|
2019-02-09 06:08:16 +00:00
|
|
|
StandardPlural::Form& pluralForm,
|
|
|
|
UErrorCode& status);
|
|
|
|
|
2015-12-16 04:41:33 +00:00
|
|
|
/**
|
|
|
|
* Formats the pattern with the value and adjusts the FieldPosition.
|
2019-02-16 01:32:50 +00:00
|
|
|
* TODO: Remove?
|
2015-12-16 04:41:33 +00:00
|
|
|
*/
|
|
|
|
static UnicodeString &format(
|
2016-01-23 05:20:23 +00:00
|
|
|
const SimpleFormatter &pattern,
|
2015-12-16 04:41:33 +00:00
|
|
|
const UnicodeString &value,
|
|
|
|
UnicodeString &appendTo,
|
|
|
|
FieldPosition &pos,
|
|
|
|
UErrorCode &status);
|
|
|
|
|
|
|
|
private:
|
2016-01-23 05:20:23 +00:00
|
|
|
SimpleFormatter *formatters[StandardPlural::COUNT];
|
2014-01-29 05:08:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif
|
2014-09-13 00:53:06 +00:00
|
|
|
|
|
|
|
#endif
|