summaryrefslogtreecommitdiff
path: root/deps/icu-small/source/i18n/pluralaffix.h
blob: 94366ce4cf81ec4a353592a45b9f4f3f5fa4c47c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
*******************************************************************************
* Copyright (C) 2015, International Business Machines
* Corporation and others.  All Rights Reserved.
*******************************************************************************
* pluralaffix.h
*
* created on: 2015jan06
* created by: Travis Keep
*/

#ifndef __PLURALAFFIX_H__
#define __PLURALAFFIX_H__

#include "unicode/utypes.h"

#if !UCONFIG_NO_FORMATTING

#include "unicode/unum.h"
#include "unicode/uobject.h"

#include "digitaffix.h"
#include "pluralmap.h"

U_NAMESPACE_BEGIN

class FieldPositionHandler;

// Export an explicit template instantiation.
//
//    MSVC requires this, even though it should not be necessary.
//    No direct access leaks out of the i18n library.
//
//    Macintosh produces duplicate definition linker errors with the explicit template
//    instantiation.
//
#if !U_PLATFORM_IS_DARWIN_BASED
template class U_I18N_API PluralMap<DigitAffix>;
#endif


/**
 * A plural aware prefix or suffix of a formatted number.
 *
 * PluralAffix is essentially a map of DigitAffix objects keyed by plural
 * category. The 'other' category is the default and always has some
 * value. The rest of the categories are optional. Querying for a category that
 * is not set always returns the DigitAffix stored in the 'other' category.
 *
 * To use one of these objects, build it up first using append() and
 * setVariant() methods. Once built, leave unchanged and let multiple threads
 * safely access.
 *
 * The following code is sample code for building up:
 *   one: US Dollar -
 *   other: US Dollars -
 *
 * and storing it in "negativeCurrencyPrefix"
 *
 * UErrorCode status = U_ZERO_ERROR;
 *
 * PluralAffix negativeCurrencyPrefix;
 *
 * PluralAffix currencyName;
 * currencyName.setVariant("one", "US Dollar", status);
 * currencyName.setVariant("other", "US Dollars", status);
 *
 * negativeCurrencyPrefix.append(currencyName, UNUM_CURRENCY_FIELD, status);
 * negativeCurrencyPrefix.append(" ");
 * negativeCurrencyPrefix.append("-", UNUM_SIGN_FIELD, status);
 */
class U_I18N_API PluralAffix : public UMemory {
public:

    /**
     * Create empty PluralAffix.
     */
    PluralAffix() : affixes() { }

    /**
     * Create a PluralAffix where the 'other' variant is otherVariant.
     */
    PluralAffix(const DigitAffix &otherVariant) : affixes(otherVariant) { }

    /**
     * Sets a particular variant for a plural category while overwriting
     * anything that may have been previously stored for that plural
     * category. The set value has no field annotations.
     * @param category "one", "two", "few", ...
     * @param variant the variant to store under the particular category
     * @param status Any error returned here.
     */
    UBool setVariant(
            const char *category,
            const UnicodeString &variant,
            UErrorCode &status);
    /**
     * Make the 'other' variant be the empty string with no field annotations
     * and remove the variants for the rest of the plural categories.
     */
    void remove();

    /**
     * Append value to all set plural categories. If fieldId present, value
     * is that field type.
     */
    void appendUChar(UChar value, int32_t fieldId=UNUM_FIELD_COUNT);

    /**
     * Append value to all set plural categories. If fieldId present, value
     * is that field type.
     */
    void append(const UnicodeString &value, int32_t fieldId=UNUM_FIELD_COUNT);

    /**
     * Append value to all set plural categories. If fieldId present, value
     * is that field type.
     */
    void append(const UChar *value, int32_t charCount, int32_t fieldId=UNUM_FIELD_COUNT);

    /**
     * Append the value for each plural category in rhs to the corresponding
     * plural category in this instance. Each value appended from rhs is
     * of type fieldId.
     */
    UBool append(
            const PluralAffix &rhs,
            int32_t fieldId,
            UErrorCode &status);
    /**
     * Get the DigitAffix for a paricular category such as "zero", "one", ...
     * If the particular category is not set, returns the 'other' category
     * which is always set.
     */
    const DigitAffix &getByCategory(const char *category) const;

    /**
     * Get the DigitAffix for a paricular category such as "zero", "one", ...
     * If the particular category is not set, returns the 'other' category
     * which is always set.
     */
    const DigitAffix &getByCategory(const UnicodeString &category) const;

    /**
     * Get the DigitAffix for the other category which is always set.
     */
    const DigitAffix &getOtherVariant() const {
        return affixes.getOther();
    }

    /**
     * Returns TRUE if this instance has variants stored besides the "other"
     * variant.
     */
    UBool hasMultipleVariants() const;

    /**
     * Returns TRUE if this instance equals rhs.
     */
    UBool equals(const PluralAffix &rhs) const {
        return affixes.equals(rhs.affixes, &eq);
    }

private:
    PluralMap<DigitAffix> affixes;

    static UBool eq(const DigitAffix &x, const DigitAffix &y) {
        return x.equals(y);
    }
};


U_NAMESPACE_END
#endif /* #if !UCONFIG_NO_FORMATTING */
#endif  // __PLURALAFFIX_H__