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