Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/intl/icu/source/i18n/unicode/tmutfmt.h
Line
Count
Source (jump to first uncovered line)
1
// © 2016 and later: Unicode, Inc. and others.
2
// License & terms of use: http://www.unicode.org/copyright.html
3
/*
4
 *******************************************************************************
5
 * Copyright (C) 2008-2014, Google, International Business Machines Corporation
6
 * and others. All Rights Reserved.
7
 *******************************************************************************
8
 */
9
10
#ifndef __TMUTFMT_H__
11
#define __TMUTFMT_H__
12
13
#include "unicode/utypes.h"
14
15
/**
16
 * \file
17
 * \brief C++ API: Format and parse duration in single time unit
18
 */
19
20
21
#if !UCONFIG_NO_FORMATTING
22
#ifndef U_HIDE_DEPRECATED_API
23
24
#include "unicode/unistr.h"
25
#include "unicode/tmunit.h"
26
#include "unicode/tmutamt.h"
27
#include "unicode/measfmt.h"
28
#include "unicode/numfmt.h"
29
#include "unicode/plurrule.h"
30
31
32
/**
33
 * Constants for various styles.
34
 * There are 2 styles: full name and abbreviated name.
35
 * For example, for English, the full name for hour duration is "3 hours",
36
 * and the abbreviated name is "3 hrs".
37
 * @deprecated ICU 53 Use MeasureFormat and UMeasureFormatWidth instead.
38
 */
39
enum UTimeUnitFormatStyle {
40
    /** @deprecated ICU 53 */
41
    UTMUTFMT_FULL_STYLE,
42
    /** @deprecated ICU 53 */
43
    UTMUTFMT_ABBREVIATED_STYLE,
44
    /** @deprecated ICU 53 */
45
    UTMUTFMT_FORMAT_STYLE_COUNT
46
};
47
typedef enum UTimeUnitFormatStyle UTimeUnitFormatStyle; /**< @deprecated ICU 53 */
48
49
50
U_NAMESPACE_BEGIN
51
52
class Hashtable;
53
class UVector;
54
55
struct TimeUnitFormatReadSink;
56
57
/**
58
 * Format or parse a TimeUnitAmount, using plural rules for the units where available.
59
 *
60
 * <P>
61
 * Code Sample:
62
 * <pre>
63
 *   // create time unit amount instance - a combination of Number and time unit
64
 *   UErrorCode status = U_ZERO_ERROR;
65
 *   TimeUnitAmount* source = new TimeUnitAmount(2, TimeUnit::UTIMEUNIT_YEAR, status);
66
 *   // create time unit format instance
67
 *   TimeUnitFormat* format = new TimeUnitFormat(Locale("en"), status);
68
 *   // format a time unit amount
69
 *   UnicodeString formatted;
70
 *   Formattable formattable;
71
 *   if (U_SUCCESS(status)) {
72
 *       formattable.adoptObject(source);
73
 *       formatted = ((Format*)format)->format(formattable, formatted, status);
74
 *       Formattable result;
75
 *       ((Format*)format)->parseObject(formatted, result, status);
76
 *       if (U_SUCCESS(status)) {
77
 *           assert (result == formattable);
78
 *       }
79
 *   }
80
 * </pre>
81
 *
82
 * <P>
83
 * @see TimeUnitAmount
84
 * @see TimeUnitFormat
85
 * @deprecated ICU 53 Use the MeasureFormat class instead.
86
 */
87
class U_I18N_API TimeUnitFormat: public MeasureFormat {
88
public:
89
90
    /**
91
     * Create TimeUnitFormat with default locale, and full name style.
92
     * Use setLocale and/or setFormat to modify.
93
     * @deprecated ICU 53
94
     */
95
    TimeUnitFormat(UErrorCode& status);
96
97
    /**
98
     * Create TimeUnitFormat given locale, and full name style.
99
     * @deprecated ICU 53
100
     */
101
    TimeUnitFormat(const Locale& locale, UErrorCode& status);
102
103
    /**
104
     * Create TimeUnitFormat given locale and style.
105
     * @deprecated ICU 53
106
     */
107
    TimeUnitFormat(const Locale& locale, UTimeUnitFormatStyle style, UErrorCode& status);
108
109
    /**
110
     * Copy constructor.
111
     * @deprecated ICU 53
112
     */
113
    TimeUnitFormat(const TimeUnitFormat&);
114
115
    /**
116
     * deconstructor
117
     * @deprecated ICU 53
118
     */
119
    virtual ~TimeUnitFormat();
120
121
    /**
122
     * Clone this Format object polymorphically. The caller owns the result and
123
     * should delete it when done.
124
     * @return    A copy of the object.
125
     * @deprecated ICU 53
126
     */
127
    virtual Format* clone(void) const;
128
129
    /**
130
     * Assignment operator
131
     * @deprecated ICU 53
132
     */
133
    TimeUnitFormat& operator=(const TimeUnitFormat& other);
134
135
    /**
136
     * Return true if the given Format objects are not semantically equal.
137
     * Objects of different subclasses are considered unequal.
138
     * @param other the object to be compared with.
139
     * @return      true if the given Format objects are not semantically equal.
140
     * @deprecated ICU 53
141
     */
142
    UBool operator!=(const Format& other) const;
143
144
    /**
145
     * Set the locale used for formatting or parsing.
146
     * @param locale  the locale to be set
147
     * @param status  output param set to success/failure code on exit
148
     * @deprecated ICU 53
149
     */
150
    void setLocale(const Locale& locale, UErrorCode& status);
151
152
153
    /**
154
     * Set the number format used for formatting or parsing.
155
     * @param format  the number formatter to be set
156
     * @param status  output param set to success/failure code on exit
157
     * @deprecated ICU 53
158
     */
159
    void setNumberFormat(const NumberFormat& format, UErrorCode& status);
160
161
    /**
162
     * Parse a TimeUnitAmount.
163
     * @see Format#parseObject(const UnicodeString&, Formattable&, ParsePosition&) const;
164
     * @deprecated ICU 53
165
     */
166
    virtual void parseObject(const UnicodeString& source,
167
                             Formattable& result,
168
                             ParsePosition& pos) const;
169
170
    /**
171
     * Return the class ID for this class. This is useful only for comparing to
172
     * a return value from getDynamicClassID(). For example:
173
     * <pre>
174
     * .   Base* polymorphic_pointer = createPolymorphicObject();
175
     * .   if (polymorphic_pointer->getDynamicClassID() ==
176
     * .       erived::getStaticClassID()) ...
177
     * </pre>
178
     * @return          The class ID for all objects of this class.
179
     * @deprecated ICU 53
180
     */
181
    static UClassID U_EXPORT2 getStaticClassID(void);
182
183
    /**
184
     * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
185
     * method is to implement a simple version of RTTI, since not all C++
186
     * compilers support genuine RTTI. Polymorphic operator==() and clone()
187
     * methods call this method.
188
     *
189
     * @return          The class ID for this object. All objects of a
190
     *                  given class have the same class ID.  Objects of
191
     *                  other classes have different class IDs.
192
     * @deprecated ICU 53
193
     */
194
    virtual UClassID getDynamicClassID(void) const;
195
196
private:
197
    Hashtable*    fTimeUnitToCountToPatterns[TimeUnit::UTIMEUNIT_FIELD_COUNT];
198
    UTimeUnitFormatStyle fStyle;
199
200
    void create(UTimeUnitFormatStyle style, UErrorCode& status);
201
202
    // it might actually be simpler to make them Decimal Formats later.
203
    // initialize all private data members
204
    void setup(UErrorCode& status);
205
206
    // initialize data member without fill in data for fTimeUnitToCountToPattern
207
    void initDataMembers(UErrorCode& status);
208
209
    // initialize fTimeUnitToCountToPatterns from current locale's resource.
210
    void readFromCurrentLocale(UTimeUnitFormatStyle style, const char* key, const UVector& pluralCounts,
211
                               UErrorCode& status);
212
213
    // check completeness of fTimeUnitToCountToPatterns against all time units,
214
    // and all plural rules, fill in fallback as necessary.
215
    void checkConsistency(UTimeUnitFormatStyle style, const char* key, UErrorCode& status);
216
217
    // fill in fTimeUnitToCountToPatterns from locale fall-back chain
218
    void searchInLocaleChain(UTimeUnitFormatStyle style, const char* key, const char* localeName,
219
                             TimeUnit::UTimeUnitFields field, const UnicodeString&,
220
                             const char*, Hashtable*, UErrorCode&);
221
222
    // initialize hash table
223
    Hashtable* initHash(UErrorCode& status);
224
225
    // delete hash table
226
    void deleteHash(Hashtable* htable);
227
228
    // copy hash table
229
    void copyHash(const Hashtable* source, Hashtable* target, UErrorCode& status);
230
    // get time unit name, such as "year", from time unit field enum, such as
231
    // UTIMEUNIT_YEAR.
232
    static const char* getTimeUnitName(TimeUnit::UTimeUnitFields field, UErrorCode& status);
233
234
    friend struct TimeUnitFormatReadSink;
235
};
236
237
inline UBool
238
0
TimeUnitFormat::operator!=(const Format& other) const  {
239
0
    return !operator==(other);
240
0
}
241
242
U_NAMESPACE_END
243
244
#endif /* U_HIDE_DEPRECATED_API */
245
#endif /* #if !UCONFIG_NO_FORMATTING */
246
247
#endif // __TMUTFMT_H__
248
//eof