]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/unicode/measfmt.h
ICU-57131.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / unicode / measfmt.h
CommitLineData
374ca955
A
1/*
2**********************************************************************
2ca993e8 3* Copyright (c) 2004-2016, International Business Machines
374ca955
A
4* Corporation and others. All Rights Reserved.
5**********************************************************************
6* Author: Alan Liu
7* Created: April 20, 2004
8* Since: ICU 3.0
9**********************************************************************
10*/
11#ifndef MEASUREFORMAT_H
12#define MEASUREFORMAT_H
13
14#include "unicode/utypes.h"
15
16#if !UCONFIG_NO_FORMATTING
17
18#include "unicode/format.h"
57a6839d 19#include "unicode/udat.h"
2ca993e8
A
20// Apple specific:
21#include "unicode/uameasureformat.h"
374ca955 22
73c04bcf
A
23/**
24 * \file
25 * \brief C++ API: Formatter for measure objects.
26 */
27
57a6839d
A
28/**
29 * Constants for various widths.
b331163b 30 * There are 4 widths: Wide, Short, Narrow, Numeric.
57a6839d 31 * For example, for English, when formatting "3 hours"
b331163b
A
32 * Wide is "3 hours"; short is "3 hrs"; narrow is "3h";
33 * formatting "3 hours 17 minutes" as numeric give "3:17"
34 * @stable ICU 53
57a6839d
A
35 */
36enum UMeasureFormatWidth {
37
38 // Wide, short, and narrow must be first and in this order.
39 /**
40 * Spell out measure units.
b331163b 41 * @stable ICU 53
57a6839d
A
42 */
43 UMEASFMT_WIDTH_WIDE,
44
45 /**
46 * Abbreviate measure units.
b331163b 47 * @stable ICU 53
57a6839d
A
48 */
49 UMEASFMT_WIDTH_SHORT,
50
51 /**
52 * Use symbols for measure units when possible.
b331163b 53 * @stable ICU 53
57a6839d
A
54 */
55 UMEASFMT_WIDTH_NARROW,
56
57 /**
58 * Completely omit measure units when possible. For example, format
59 * '5 hours, 37 minutes' as '5:37'
b331163b 60 * @stable ICU 53
57a6839d
A
61 */
62 UMEASFMT_WIDTH_NUMERIC,
63
64 /**
65 * Count of values in this enum.
b331163b 66 * @stable ICU 53
57a6839d 67 */
2ca993e8
A
68 UMEASFMT_WIDTH_COUNT = 4,
69
70 /**
71 * Apple-specific
72 * Shorter, between SHORT and NARROW (SHORT without space in unit pattern)
73 * @draft ICU 57
74 */
75 UMEASFMT_WIDTH_SHORTER = 8
76
57a6839d 77};
b331163b 78/** @stable ICU 53 */
57a6839d 79typedef enum UMeasureFormatWidth UMeasureFormatWidth;
57a6839d 80
374ca955
A
81U_NAMESPACE_BEGIN
82
b331163b
A
83class Measure;
84class MeasureUnit;
57a6839d
A
85class NumberFormat;
86class PluralRules;
87class MeasureFormatCacheData;
88class SharedNumberFormat;
89class SharedPluralRules;
90class QuantityFormatter;
2ca993e8 91class SimpleFormatter;
57a6839d
A
92class ListFormatter;
93class DateFormat;
2ca993e8 94class FieldPositionHandler;
57a6839d 95
374ca955 96/**
73c04bcf 97 *
57a6839d 98 * A formatter for measure objects.
374ca955
A
99 *
100 * @see Format
101 * @author Alan Liu
73c04bcf 102 * @stable ICU 3.0
374ca955
A
103 */
104class U_I18N_API MeasureFormat : public Format {
374ca955 105 public:
57a6839d
A
106 using Format::parseObject;
107 using Format::format;
108
57a6839d
A
109 /**
110 * Constructor.
b331163b 111 * @stable ICU 53
57a6839d
A
112 */
113 MeasureFormat(
114 const Locale &locale, UMeasureFormatWidth width, UErrorCode &status);
115
116 /**
117 * Constructor.
b331163b 118 * @stable ICU 53
57a6839d
A
119 */
120 MeasureFormat(
121 const Locale &locale,
122 UMeasureFormatWidth width,
123 NumberFormat *nfToAdopt,
124 UErrorCode &status);
57a6839d
A
125
126 /**
127 * Copy constructor.
b331163b 128 * @stable ICU 3.0
57a6839d
A
129 */
130 MeasureFormat(const MeasureFormat &other);
131
132 /**
133 * Assignment operator.
b331163b 134 * @stable ICU 3.0
57a6839d
A
135 */
136 MeasureFormat &operator=(const MeasureFormat &rhs);
137
4388f060
A
138 /**
139 * Destructor.
140 * @stable ICU 3.0
141 */
142 virtual ~MeasureFormat();
374ca955 143
57a6839d
A
144 /**
145 * Return true if given Format objects are semantically equal.
b331163b 146 * @stable ICU 53
57a6839d
A
147 */
148 virtual UBool operator==(const Format &other) const;
149
150 /**
151 * Clones this object polymorphically.
b331163b 152 * @stable ICU 53
57a6839d
A
153 */
154 virtual Format *clone() const;
155
156 /**
157 * Formats object to produce a string.
b331163b 158 * @stable ICU 53
57a6839d
A
159 */
160 virtual UnicodeString &format(
161 const Formattable &obj,
162 UnicodeString &appendTo,
163 FieldPosition &pos,
164 UErrorCode &status) const;
165
166 /**
167 * Parse a string to produce an object. This implementation sets
168 * status to U_UNSUPPORTED_ERROR.
169 *
170 * @draft ICU 53
171 */
172 virtual void parseObject(
173 const UnicodeString &source,
174 Formattable &reslt,
175 ParsePosition &pos) const;
176
57a6839d
A
177 /**
178 * Formats measure objects to produce a string. An example of such a
179 * formatted string is 3 meters, 3.5 centimeters. Measure objects appear
180 * in the formatted string in the same order they appear in the "measures"
181 * array. The NumberFormat of this object is used only to format the amount
182 * of the very last measure. The other amounts are formatted with zero
183 * decimal places while rounding toward zero.
184 * @param measures array of measure objects.
185 * @param measureCount the number of measure objects.
186 * @param appendTo formatted string appended here.
187 * @param pos the field position.
188 * @param status the error.
189 * @return appendTo reference
190 *
b331163b 191 * @stable ICU 53
57a6839d
A
192 */
193 UnicodeString &formatMeasures(
194 const Measure *measures,
195 int32_t measureCount,
196 UnicodeString &appendTo,
197 FieldPosition &pos,
198 UErrorCode &status) const;
b331163b
A
199
200#ifndef U_HIDE_INTERNAL_API
2ca993e8
A
201 /**
202 * Apple-specific for now.
203 * Like formatMeasures above, but with a
204 * FieldPositionIterator* instead of a FieldPosition&
205 *
206 * @param measures Array of measure objects.
207 * @param measureCount the number of measure objects.
208 * @param appendTo Formatted string appended here.
209 * @param posIter On return, can be used to iterate over positions
210 * of fields generated by this format call. Field
211 * values are defined in UAMeasureUnit.
212 * @param status The error.
213 * @return appendTo reference
214 *
215 * @internal.
216 */
217 UnicodeString &formatMeasures(
218 const Measure *measures,
219 int32_t measureCount,
220 UnicodeString &appendTo,
221 FieldPositionIterator* posIter,
222 UErrorCode &status) const;
223
b331163b
A
224 /**
225 * Apple-specific for now
226 * @internal.
227 */
228 UMeasureFormatWidth getWidth(void) const;
2ca993e8 229
b331163b 230#endif /* U_HIDE_INTERNAL_API */
57a6839d
A
231
232
b331163b
A
233 /**
234 * Formats a single measure per unit. An example of such a
235 * formatted string is 3.5 meters per second.
236 * @param measure The measure object. In above example, 3.5 meters.
237 * @param perUnit The per unit. In above example, it is
238 * *MeasureUnit::createSecond(status).
239 * @param appendTo formatted string appended here.
240 * @param pos the field position.
241 * @param status the error.
242 * @return appendTo reference
243 *
2ca993e8 244 * @stable ICU 55
b331163b
A
245 */
246 UnicodeString &formatMeasurePerUnit(
247 const Measure &measure,
248 const MeasureUnit &perUnit,
249 UnicodeString &appendTo,
250 FieldPosition &pos,
251 UErrorCode &status) const;
252
b331163b 253
374ca955
A
254 /**
255 * Return a formatter for CurrencyAmount objects in the given
256 * locale.
257 * @param locale desired locale
258 * @param ec input-output error code
259 * @return a formatter object, or NULL upon error
73c04bcf 260 * @stable ICU 3.0
374ca955
A
261 */
262 static MeasureFormat* U_EXPORT2 createCurrencyFormat(const Locale& locale,
263 UErrorCode& ec);
264
265 /**
266 * Return a formatter for CurrencyAmount objects in the default
267 * locale.
268 * @param ec input-output error code
269 * @return a formatter object, or NULL upon error
73c04bcf 270 * @stable ICU 3.0
374ca955
A
271 */
272 static MeasureFormat* U_EXPORT2 createCurrencyFormat(UErrorCode& ec);
273
57a6839d
A
274 /**
275 * Return the class ID for this class. This is useful only for comparing to
276 * a return value from getDynamicClassID(). For example:
277 * <pre>
278 * . Base* polymorphic_pointer = createPolymorphicObject();
279 * . if (polymorphic_pointer->getDynamicClassID() ==
280 * . erived::getStaticClassID()) ...
281 * </pre>
282 * @return The class ID for all objects of this class.
b331163b 283 * @stable ICU 53
57a6839d
A
284 */
285 static UClassID U_EXPORT2 getStaticClassID(void);
286
287 /**
288 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
289 * method is to implement a simple version of RTTI, since not all C++
290 * compilers support genuine RTTI. Polymorphic operator==() and clone()
291 * methods call this method.
292 *
293 * @return The class ID for this object. All objects of a
294 * given class have the same class ID. Objects of
295 * other classes have different class IDs.
b331163b 296 * @stable ICU 53
57a6839d
A
297 */
298 virtual UClassID getDynamicClassID(void) const;
374ca955 299
57a6839d 300 protected:
374ca955
A
301 /**
302 * Default constructor.
73c04bcf 303 * @stable ICU 3.0
374ca955
A
304 */
305 MeasureFormat();
57a6839d
A
306
307#ifndef U_HIDE_INTERNAL_API
308
57a6839d
A
309 /**
310 * ICU use only.
311 * Initialize or change MeasureFormat class from subclass.
312 * @internal.
313 */
314 void initMeasureFormat(
315 const Locale &locale,
316 UMeasureFormatWidth width,
317 NumberFormat *nfToAdopt,
318 UErrorCode &status);
57a6839d
A
319 /**
320 * ICU use only.
321 * Allows subclass to change locale. Note that this method also changes
322 * the NumberFormat object. Returns TRUE if locale changed; FALSE if no
323 * change was made.
324 * @internal.
325 */
326 UBool setMeasureFormatLocale(const Locale &locale, UErrorCode &status);
327
328 public:
329 // Apple-only, temporarily public for Apple use
330 /**
331 * ICU use only.
332 * Let subclass change NumberFormat.
2ca993e8 333 * @internal Apple
57a6839d
A
334 */
335 void adoptNumberFormat(NumberFormat *nfToAdopt, UErrorCode &status);
336
2ca993e8
A
337 /**
338 * Gets the display name for a unit.
339 * @param unit The unit whose display name to get.
340 * @param result Receives the name result, if any (if none,
341 * length will be 0)
342 * @return Reference to result
343 *
344 * @internal Apple
345 */
346 UnicodeString &getUnitName(
347 const MeasureUnit* unit,
348 UnicodeString &result ) const;
349
350 /**
351 * Gets the display name for a set of units.
352 * @param units Array of units whose display name to get.
353 * @param unitCount The count of units
354 * @param listStyle The list style used for combining the unit names.
355 * @param result Receives the name result, if any (if none,
356 * length will be 0)
357 * @return Reference to result
358 *
359 * @internal Apple
360 */
361 UnicodeString &getMultipleUnitNames(
362 const MeasureUnit** units,
363 int32_t unitCount,
364 UAMeasureNameListStyle listStyle,
365 UnicodeString &result ) const;
366
57a6839d
A
367 protected:
368 /**
369 * ICU use only.
370 * @internal.
371 */
372 const NumberFormat &getNumberFormat() const;
373
374 /**
375 * ICU use only.
376 * @internal.
377 */
378 const PluralRules &getPluralRules() const;
379
380 /**
381 * ICU use only.
382 * @internal.
383 */
384 Locale getLocale(UErrorCode &status) const;
385
386 /**
387 * ICU use only.
388 * @internal.
389 */
390 const char *getLocaleID(UErrorCode &status) const;
391
392#endif /* U_HIDE_INTERNAL_API */
393
394 private:
395 const MeasureFormatCacheData *cache;
396 const SharedNumberFormat *numberFormat;
397 const SharedPluralRules *pluralRules;
2ca993e8
A
398 UMeasureFormatWidth width;
399 UBool stripPatternSpaces;
57a6839d
A
400
401 // Declared outside of MeasureFormatSharedData because ListFormatter
402 // objects are relatively cheap to copy; therefore, they don't need to be
403 // shared across instances.
404 ListFormatter *listFormatter;
2ca993e8 405 ListFormatter *listFormatterStd; // standard list style, option for display names; Apple specific
57a6839d 406
2ca993e8
A
407 const SimpleFormatter *getFormatterOrNull(
408 const MeasureUnit &unit, UMeasureFormatWidth width, int32_t index) const;
409
410 const SimpleFormatter *getFormatter(
411 const MeasureUnit &unit, UMeasureFormatWidth width, int32_t index,
412 UErrorCode &errorCode) const;
57a6839d 413
2ca993e8
A
414 const SimpleFormatter *getPluralFormatter(
415 const MeasureUnit &unit, UMeasureFormatWidth width, int32_t index,
416 UErrorCode &errorCode) const;
b331163b 417
2ca993e8
A
418 const SimpleFormatter *getPerFormatter(
419 UMeasureFormatWidth width,
b331163b
A
420 UErrorCode &status) const;
421
422 int32_t withPerUnitAndAppend(
423 const UnicodeString &formatted,
424 const MeasureUnit &perUnit,
425 UnicodeString &appendTo,
426 UErrorCode &status) const;
427
57a6839d
A
428 UnicodeString &formatMeasure(
429 const Measure &measure,
430 const NumberFormat &nf,
431 UnicodeString &appendTo,
432 FieldPosition &pos,
433 UErrorCode &status) const;
434
435 UnicodeString &formatMeasuresSlowTrack(
436 const Measure *measures,
437 int32_t measureCount,
438 UnicodeString& appendTo,
439 FieldPosition& pos,
440 UErrorCode& status) const;
441
442 UnicodeString &formatNumeric(
443 const Formattable *hms, // always length 3: [0] is hour; [1] is
444 // minute; [2] is second.
445 int32_t bitMap, // 1=hour set, 2=minute set, 4=second set
446 UnicodeString &appendTo,
2ca993e8 447 FieldPositionHandler& handler,
57a6839d
A
448 UErrorCode &status) const;
449
450 UnicodeString &formatNumeric(
451 UDate date,
452 const DateFormat &dateFmt,
453 UDateFormatField smallestField,
454 const Formattable &smallestAmount,
455 UnicodeString &appendTo,
2ca993e8 456 FieldPositionHandler& handler,
57a6839d 457 UErrorCode &status) const;
374ca955
A
458};
459
460U_NAMESPACE_END
461
462#endif // #if !UCONFIG_NO_FORMATTING
463#endif // #ifndef MEASUREFORMAT_H