1 /* 2 ******************************************************************************* 3 * Copyright (C) 2008, Google, International Business Machines Corporation and * 4 * 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 /** 29 * @internal ICU 4.2 30 */ 31 32 union UHashTok; 33 34 U_NAMESPACE_BEGIN 35 36 U_CDECL_BEGIN 37 38 /** 39 * @internal ICU 4.2 40 */ 41 static UBool U_CALLCONV hashTableValueComparator(UHashTok val1, UHashTok val2) ; 42 43 U_CDECL_END 44 45 46 class Hashtable; 47 48 49 /** 50 * Format or parse a TimeUnitAmount, using plural rules for the units where available. 51 * 52 * <P> 53 * Code Sample: 54 * <pre> 55 * // create time unit amount instance - a combination of Number and time unit 56 * UErrorCode status = U_ZERO_ERROR; 57 * TimeUnitAmount* source = new TimeUnitAmount(2, TimeUnit::UTIMEUNIT_YEAR, status); 58 * // create time unit format instance 59 * TimeUnitFormat* format = new TimeUnitFormat(Locale("en"), status); 60 * // format a time unit amount 61 * UnicodeString formatted; 62 * Formattable formattable; 63 * if (U_SUCCESS(status)) { 64 * formattable.adoptObject(source); 65 * formatted = ((Format*)format)->format(formattable, formatted, status); 66 * Formattable result; 67 * ((Format*)format)->parseObject(formatted, result, status); 68 * if (U_SUCCESS(status)) { 69 * assert (result == formattable); 70 * } 71 * } 72 * </pre> 73 * 74 * <P> 75 * @see TimeUnitAmount 76 * @see TimeUnitFormat 77 * @draft ICU 4.2 78 */ 79 class U_I18N_API TimeUnitFormat: public MeasureFormat { 80 public: 81 82 /** 83 * Constants for various styles. 84 * There are 2 styles: full name and abbreviated name. 85 * For example, for English, the full name for hour duration is "3 hours", 86 * and the abbreviated name is "3 hrs". 87 * @draft ICU 4.2 88 */ 89 enum EStyle { 90 kFull = 0, 91 kAbbreviate = 1, 92 kTotal = kAbbreviate + 1 93 }; 94 95 /** 96 * Create TimeUnitFormat with default locale, and full name style. 97 * Use setLocale and/or setFormat to modify. 98 * @draft ICU 4.2 99 */ 100 TimeUnitFormat(UErrorCode& status); 101 102 /** 103 * Create TimeUnitFormat given locale, and full name style. 104 * @draft ICU 4.2 105 */ 106 TimeUnitFormat(const Locale& locale, UErrorCode& status); 107 108 /** 109 * Create TimeUnitFormat given locale and style. 110 * @draft ICU 4.2 111 */ 112 TimeUnitFormat(const Locale& locale, EStyle style, UErrorCode& status); 113 114 /** 115 * Copy constructor. 116 * @draft ICU 4.2 117 */ 118 TimeUnitFormat(const TimeUnitFormat&); 119 120 /** 121 * deconstructor 122 * @draft ICU 4.2 123 */ 124 virtual ~TimeUnitFormat(); 125 126 /** 127 * Clone this Format object polymorphically. The caller owns the result and 128 * should delete it when done. 129 * @return A copy of the object. 130 * @draft ICU 4.2 131 */ 132 virtual Format* clone(void) const; 133 134 /** 135 * Assignment operator 136 * @draft ICU 4.2 137 */ 138 TimeUnitFormat& operator=(const TimeUnitFormat& other); 139 140 141 /** 142 * Return true if the given Format objects are semantically equal. Objects 143 * of different subclasses are considered unequal. 144 * @param other the object to be compared with. 145 * @return true if the given Format objects are semantically equal. 146 * @draft ICU 4.2 147 */ 148 virtual UBool operator==(const Format& other) const; 149 150 /** 151 * Return true if the given Format objects are not semantically equal. 152 * Objects of different subclasses are considered unequal. 153 * @param other the object to be compared with. 154 * @return true if the given Format objects are not semantically equal. 155 * @draft ICU 4.2 156 */ 157 UBool operator!=(const Format& other) const; 158 159 /** 160 * Set the locale used for formatting or parsing. 161 * @param locale the locale to be set 162 * @param status output param set to success/failure code on exit 163 * @draft ICU 4.2 164 */ 165 void setLocale(const Locale& locale, UErrorCode& status); 166 167 168 /** 169 * Set the number format used for formatting or parsing. 170 * @param format the number formatter to be set 171 * @param status output param set to success/failure code on exit 172 * @draft ICU 4.2 173 */ 174 void setNumberFormat(const NumberFormat& format, UErrorCode& status); 175 176 /** 177 * Format a TimeUnitAmount. 178 * If the formattable object is not a time unit amount object, 179 * or the number in time unit amount is not a double type or long type 180 * numeric, it returns a failing status: U_ILLEGAL_ARGUMENT_ERROR. 181 * @see Format#format(const Formattable&, UnicodeString&, FieldPosition&, UErrorCode&) const 182 * @draft ICU 4.2 183 */ 184 virtual UnicodeString& format(const Formattable& obj, 185 UnicodeString& toAppendTo, 186 FieldPosition& pos, 187 UErrorCode& status) const; 188 189 /** 190 * Parse a TimeUnitAmount. 191 * @see Format#parseObject(const UnicodeString&, Formattable&, ParsePosition&) const; 192 * @draft ICU 4.2 193 */ 194 virtual void parseObject(const UnicodeString& source, 195 Formattable& result, 196 ParsePosition& pos) const; 197 198 /** 199 * Return the class ID for this class. This is useful only for comparing to 200 * a return value from getDynamicClassID(). For example: 201 * <pre> 202 * . Base* polymorphic_pointer = createPolymorphicObject(); 203 * . if (polymorphic_pointer->getDynamicClassID() == 204 * . erived::getStaticClassID()) ... 205 * </pre> 206 * @return The class ID for all objects of this class. 207 * @draft ICU 4.2 208 */ 209 static UClassID U_EXPORT2 getStaticClassID(void); 210 211 /** 212 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This 213 * method is to implement a simple version of RTTI, since not all C++ 214 * compilers support genuine RTTI. Polymorphic operator==() and clone() 215 * methods call this method. 216 * 217 * @return The class ID for this object. All objects of a 218 * given class have the same class ID. Objects of 219 * other classes have different class IDs. 220 * @draft ICU 4.2 221 */ 222 virtual UClassID getDynamicClassID(void) const; 223 224 private: 225 NumberFormat* fNumberFormat; 226 Locale fLocale; 227 Hashtable* fTimeUnitToCountToPatterns[TimeUnit::UTIMEUNIT_FIELD_COUNT]; 228 PluralRules* fPluralRules; 229 EStyle fStyle; 230 231 friend UBool U_CALLCONV hashTableValueComparator(UHashTok val1, UHashTok val2); 232 233 void create(const Locale& locale, EStyle style, UErrorCode& status); 234 235 // it might actually be simpler to make them Decimal Formats later. 236 // initialize all private data members 237 void setup(UErrorCode& status); 238 239 // initialize data member without fill in data for fTimeUnitToCountToPattern 240 void initDataMembers(UErrorCode& status); 241 242 // initialize fTimeUnitToCountToPatterns from current locale's resource. 243 void readFromCurrentLocale(EStyle style, const char* key, UErrorCode& status); 244 245 // check completeness of fTimeUnitToCountToPatterns against all time units, 246 // and all plural rules, fill in fallback as necessary. 247 void checkConsistency(EStyle style, const char* key, UErrorCode& status); 248 249 // fill in fTimeUnitToCountToPatterns from locale fall-back chain 250 void searchInLocaleChain(EStyle style, const char* key, 251 TimeUnit::UTimeUnitFields field, const char*, 252 const char*, Hashtable*, UErrorCode&); 253 254 // initialize hash table 255 Hashtable* initHash(UErrorCode& status); 256 257 // delete hash table 258 void deleteHash(Hashtable* htable); 259 260 // copy hash table 261 void copyHash(const Hashtable* source, Hashtable* target, UErrorCode& status); 262 // get time unit name, such as "year", from time unit field enum, such as 263 // UTIMEUNIT_YEAR. 264 static const char* getTimeUnitName(TimeUnit::UTimeUnitFields field, UErrorCode& status); 265 }; 266 267 268 269 inline UBool 270 TimeUnitFormat::operator!=(const Format& other) const { 271 return !operator==(other); 272 } 273 274 275 276 U_NAMESPACE_END 277 278 #endif /* #if !UCONFIG_NO_FORMATTING */ 279 280 #endif // __TMUTFMT_H__ 281 //eof 282