1/*
2 *******************************************************************************
3 * Copyright (C) 2008-2014, Google, International Business Machines Corporation
4 * and others. All Rights Reserved.
5 *******************************************************************************
6 */
7
8#ifndef __TMUTFMT_H__
9#define __TMUTFMT_H__
10
11#include "unicode/utypes.h"
12
13/**
14 * \file
15 * \brief C++ API: Format and parse duration in single time unit
16 */
17
18
19#if !UCONFIG_NO_FORMATTING
20
21#include "unicode/unistr.h"
22#include "unicode/tmunit.h"
23#include "unicode/tmutamt.h"
24#include "unicode/measfmt.h"
25#include "unicode/numfmt.h"
26#include "unicode/plurrule.h"
27
28#ifndef U_HIDE_DEPRECATED_API
29
30/**
31 * Constants for various styles.
32 * There are 2 styles: full name and abbreviated name.
33 * For example, for English, the full name for hour duration is "3 hours",
34 * and the abbreviated name is "3 hrs".
35 * @deprecated ICU 53 Use MeasureFormat and UMeasureFormatWidth instead.
36 */
37enum UTimeUnitFormatStyle {
38    /** @deprecated ICU 53 */
39    UTMUTFMT_FULL_STYLE,
40    /** @deprecated ICU 53 */
41    UTMUTFMT_ABBREVIATED_STYLE,
42    /** @deprecated ICU 53 */
43    UTMUTFMT_FORMAT_STYLE_COUNT
44};
45typedef enum UTimeUnitFormatStyle UTimeUnitFormatStyle; /**< @deprecated ICU 53 */
46
47#endif  /* U_HIDE_DEPRECATED_API */
48
49
50U_NAMESPACE_BEGIN
51
52class Hashtable;
53class UVector;
54
55#ifndef U_HIDE_DEPRECATED_API
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 */
87class U_I18N_API TimeUnitFormat: public MeasureFormat {
88public:
89
90    /**
91     * Create TimeUnitFormat with default locale, and full name style.
92     * Use setLocale and/or setFormat to modify.
93     * @stable ICU 4.2
94     */
95    TimeUnitFormat(UErrorCode& status);
96
97    /**
98     * Create TimeUnitFormat given locale, and full name style.
99     * @stable ICU 4.2
100     */
101    TimeUnitFormat(const Locale& locale, UErrorCode& status);
102
103    /**
104     * Create TimeUnitFormat given locale and style.
105     * @stable ICU 4.8
106     */
107    TimeUnitFormat(const Locale& locale, UTimeUnitFormatStyle style, UErrorCode& status);
108
109    /**
110     * Copy constructor.
111     * @stable ICU 4.2
112     */
113    TimeUnitFormat(const TimeUnitFormat&);
114
115    /**
116     * deconstructor
117     * @stable ICU 4.2
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     * @stable ICU 4.2
126     */
127    virtual Format* clone(void) const;
128
129    /**
130     * Assignment operator
131     * @stable ICU 4.2
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     * @stable ICU 4.2
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     * @stable ICU 4.2
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     * @stable ICU 4.2
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     * @stable ICU 4.2
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     * @stable ICU 4.2
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     * @stable ICU 4.2
193     */
194    virtual UClassID getDynamicClassID(void) const;
195
196private:
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};
235
236inline UBool
237TimeUnitFormat::operator!=(const Format& other) const  {
238    return !operator==(other);
239}
240
241#endif /* U_HIDE_DEPRECATED_API */
242
243U_NAMESPACE_END
244
245#endif /* #if !UCONFIG_NO_FORMATTING */
246
247#endif // __TMUTFMT_H__
248//eof
249