]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/unicode/choicfmt.h
ICU-6.2.8.tar.gz
[apple/icu.git] / icuSources / i18n / unicode / choicfmt.h
1 /*
2 ********************************************************************************
3 * Copyright (C) 1997-2004, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ********************************************************************************
6 *
7 * File CHOICFMT.H
8 *
9 * Modification History:
10 *
11 * Date Name Description
12 * 02/19/97 aliu Converted from java.
13 * 03/20/97 helena Finished first cut of implementation and got rid
14 * of nextDouble/previousDouble and replaced with
15 * boolean array.
16 * 4/10/97 aliu Clean up. Modified to work on AIX.
17 * 8/6/97 nos Removed overloaded constructor, member var 'buffer'.
18 * 07/22/98 stephen Removed operator!= (implemented in Format)
19 ********************************************************************************
20 */
21
22 #ifndef CHOICFMT_H
23 #define CHOICFMT_H
24
25 #include "unicode/utypes.h"
26
27 #if !UCONFIG_NO_FORMATTING
28
29 #include "unicode/unistr.h"
30 #include "unicode/numfmt.h"
31 #include "unicode/fieldpos.h"
32 #include "unicode/format.h"
33
34 U_NAMESPACE_BEGIN
35
36 class MessageFormat;
37
38 /**
39 * ChoiceFormat converts between ranges of numeric values
40 * and string names for those ranges. A <code>ChoiceFormat</code> splits
41 * the real number line <code>-Inf</code> to <code>+Inf</code> into two
42 * or more contiguous ranges. Each range is mapped to a
43 * string. <code>ChoiceFormat</code> is generally used in a
44 * <code>MessageFormat</code> for displaying grammatically correct
45 * plurals such as &quot;There are 2 files.&quot;</p>
46 *
47 * <p>There are two methods of defining a <code>ChoiceFormat</code>; both
48 * are equivalent. The first is by using a string pattern. This is the
49 * preferred method in most cases. The second method is through direct
50 * specification of the arrays that make up the
51 * <code>ChoiceFormat</code>.</p>
52 *
53 * <p><strong>Patterns</strong></p>
54 *
55 * <p>In most cases, the preferred way to define a
56 * <code>ChoiceFormat</code> is with a pattern. Here is an example of a
57 * <code>ChoiceFormat</code> pattern:</p>
58 *
59 * \htmlonly<pre> 0&#x2264;are no files|1&#x2264;is one file|1&lt;are many files</pre>\endhtmlonly
60 *
61 * <p>or equivalently,</p>
62 *
63 * <pre> 0#are no files|1#is one file|1&lt;are many files</pre>
64 *
65 * <p>The pattern consists of a number or <em>range specifiers</em>
66 * separated by vertical bars '|' (U+007C). There is no
67 * vertical bar after the last range. Each range specifier is of the
68 * form:</p>
69 *
70 * \htmlonly<blockquote><em>Number Separator String</em></blockquote>\endhtmlonly
71 *
72 * <p><em>Number</em> is a floating point number that can be parsed by a
73 * default <code>NumberFormat</code> for the US locale. It gives the
74 * lower limit of this range. The lower limit is either inclusive or
75 * exclusive, depending on the <em>separator</em>. The upper limit is
76 * given by the lower limit of the next range. The Unicode infinity
77 * sign \htmlonly&#x221E \endhtmlonly (U+221E) is recognized for positive infinity. It may be preceded by
78 * '-' (U+002D) to indicate negative infinity.</p>
79 *
80 * <p><em>String</em> is the format string for this range, with special
81 * characters enclosed in single quotes (<code>'The #
82 * sign'</code>). Single quotes themselves are indicated by two single
83 * quotes in a row (<code>'o''clock'</code>).</p>
84 *
85 * <p><em>Separator</em> is one of the following single characters:
86 *
87 * <ul>
88 * <li>\htmlonly'&#x2264;' \endhtmlonly (U+2264) or '#' (U+0023)
89 * indicates that the lower limit given by <em>Number</em> is
90 * inclusive. (The two characters are equivalent to ChoiceFormat.)
91 * This means that the limit value <em>Number</em> belongs to this
92 * range. Another way of saying this is that the corresponding
93 * closure is <code>FALSE</code>.</li>
94 *
95 * <li>'<' (U+003C) indicates that the lower limit given by
96 * <em>Number</em> is exclusive. This means that the value
97 * <em>Number</em> belongs to the prior range.</li> Another way of
98 * saying this is that the corresponding closure is
99 * <code>TRUE</code>.
100 * </ul>
101 *
102 * <p>See below for more information about closures.</p>
103 *
104 * <p><strong>Arrays</strong></p>
105 *
106 * <p>A <code>ChoiceFormat</code> defining <code>n</code> intervals
107 * (<code>n</code> &gt;= 2) is specified by three arrays of
108 * <code>n</code> items:
109 *
110 * <ul>
111 * <li><code>double limits[]</code> gives the start of each
112 * interval. This must be a non-decreasing list of values, none of
113 * which may be <code>NaN</code>.</li>
114 * <li><code>UBool closures[]</code> determines whether each limit
115 * value is contained in the interval below it or in the interval
116 * above it. If <code>closures[i]</code> is <code>FALSE</code>, then
117 * <code>limits[i]</code> is a member of interval
118 * <code>i</code>. Otherwise it is a member of interval
119 * <code>i+1</code>. If no closures array is specified, this is
120 * equivalent to having all closures be <code>FALSE</code>. Closures
121 * allow one to specify half-open, open, or closed intervals.</li>
122 * <li><code>UnicodeString formats[]</code> gives the string label
123 * associated with each interval.</li>
124 * </ul>
125 *
126 * <p><strong>Formatting and Parsing</strong></p>
127 *
128 * <p>During formatting, a number is converted to a
129 * string. <code>ChoiceFormat</code> accomplishes this by mapping the
130 * number to an interval using the following rule. Given a number
131 * <code>X</code> and and index value <code>j</code> in the range
132 * <code>0..n-1</code>, where <code>n</code> is the number of ranges:</p>
133 *
134 * \htmlonly<blockquote>\endhtmlonly<code>X</code> matches <code>j</code> if and only if
135 * <code>limit[j] &lt;= X &lt; limit[j+1]</code>
136 * \htmlonly</blockquote>\endhtmlonly
137 *
138 * <p>(This assumes that all closures are <code>FALSE</code>. If some
139 * closures are <code>TRUE</code> then the relations must be changed to
140 * <code>&lt;=</code> or <code>&lt;</code> as appropriate.) If there is
141 * no match, then either the first or last index is used, depending on
142 * whether the number is too low or too high. Once a number is mapped to
143 * an interval <code>j</code>, the string <code>formats[j]</code> is
144 * output.</p>
145 *
146 * <p>During parsing, a string is converted to a
147 * number. <code>ChoiceFormat</code> finds the element
148 * <code>formats[j]</code> equal to the string, and returns
149 * <code>limits[j]</code> as the parsed value.</p>
150 *
151 * <p><strong>Notes</strong></p>
152 *
153 * <p>The first limit value does not define a range boundary. For
154 * example, in the pattern &quot;<code>1.0#a|2.0#b</code>&quot;, the
155 * intervals are [-Inf, 2.0) and [2.0, +Inf]. It appears that the first
156 * interval should be [1.0, 2.0). However, since all values that are too
157 * small are mapped to range zero, the first interval is effectively
158 * [-Inf, 2.0). However, the first limit value <em>is</em> used during
159 * formatting. In this example, <code>parse(&quot;a&quot;)</code> returns
160 * 1.0.</p>
161 *
162 * <p>There are no gaps between intervals and the entire number line is
163 * covered. A <code>ChoiceFormat</code> maps <em>all</em> possible
164 * double values to a finite set of intervals.</p>
165 *
166 * <p>The non-number <code>NaN</code> is mapped to interval zero during
167 * formatting.</p>
168 *
169 * <p><strong>Examples</strong></p>
170 *
171 * <p>Here is an example of two arrays that map the number
172 * <code>1..7</code> to the English day of the week abbreviations
173 * <code>Sun..Sat</code>. No closures array is given; this is the same as
174 * specifying all closures to be <code>FALSE</code>.</p>
175 *
176 * <pre> {1,2,3,4,5,6,7},
177 * {&quot;Sun&quot;,&quot;Mon&quot;,&quot;Tue&quot;,&quot;Wed&quot;,&quot;Thur&quot;,&quot;Fri&quot;,&quot;Sat&quot;}</pre>
178 *
179 * <p>Here is an example that maps the ranges [-Inf, 1), [1, 1], and (1,
180 * +Inf] to three strings. That is, the number line is split into three
181 * ranges: x &lt; 1.0, x = 1.0, and x &gt; 1.0.</p>
182 *
183 * <pre> {0, 1, 1},
184 * {FALSE, FALSE, TRUE},
185 * {&quot;no files&quot;, &quot;one file&quot;, &quot;many files&quot;}</pre>
186 *
187 * <p>Here is a simple example that shows formatting and parsing: </p>
188 *
189 * \code
190 * #include <unicode/choicfmt.h>
191 * #include <unicode/unistr.h>
192 * #include <iostream.h>
193 *
194 * int main(int argc, char *argv[]) {
195 * double limits[] = {1,2,3,4,5,6,7};
196 * UnicodeString monthNames[] = {
197 * "Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
198 * ChoiceFormat fmt(limits, monthNames, 7);
199 * UnicodeString str;
200 * char buf[256];
201 * for (double x = 1.0; x <= 8.0; x += 1.0) {
202 * fmt.format(x, str);
203 * str.extract(0, str.length(), buf, 256, "");
204 * str.truncate(0);
205 * cout << x << " -> "
206 * << buf << endl;
207 * }
208 * cout << endl;
209 * return 0;
210 * }
211 * \endcode
212 *
213 * <p>Here is a more complex example using a <code>ChoiceFormat</code>
214 * constructed from a pattern together with a
215 * <code>MessageFormat</code>.</p>
216 *
217 * \code
218 * #include <unicode/choicfmt.h>
219 * #include <unicode/msgfmt.h>
220 * #include <unicode/unistr.h>
221 * #include <iostream.h>
222 *
223 * int main(int argc, char *argv[]) {
224 * UErrorCode status = U_ZERO_ERROR;
225 * double filelimits[] = {0,1,2};
226 * UnicodeString filepart[] =
227 * {"are no files","is one file","are {0} files"};
228 * ChoiceFormat* fileform = new ChoiceFormat(filelimits, filepart, 3 );
229 * Format* testFormats[] =
230 * {fileform, NULL, NumberFormat::createInstance(status)};
231 * MessageFormat pattform("There {0} on {1}", status );
232 * pattform.adoptFormats(testFormats, 3);
233 * Formattable testArgs[] = {0L, "Disk A"};
234 * FieldPosition fp(0);
235 * UnicodeString str;
236 * char buf[256];
237 * for (int32_t i = 0; i < 4; ++i) {
238 * Formattable fInt(i);
239 * testArgs[0] = fInt;
240 * pattform.format(testArgs, 2, str, fp, status );
241 * str.extract(0, str.length(), buf, "");
242 * str.truncate(0);
243 * cout << "Output for i=" << i << " : " << buf << endl;
244 * }
245 * cout << endl;
246 * return 0;
247 * }
248 * \endcode
249 *
250 * <p><em>User subclasses are not supported.</em> While clients may write
251 * subclasses, such code will not necessarily work and will not be
252 * guaranteed to work stably from release to release.
253 */
254 class U_I18N_API ChoiceFormat: public NumberFormat {
255 public:
256 /**
257 * Construct a new ChoiceFormat with the limits and the corresponding formats
258 * based on the pattern.
259 *
260 * @param pattern Pattern used to construct object.
261 * @param status Output param to receive success code. If the
262 * pattern cannot be parsed, set to failure code.
263 * @stable ICU 2.0
264 */
265 ChoiceFormat(const UnicodeString& pattern,
266 UErrorCode& status);
267
268
269 /**
270 * Construct a new ChoiceFormat with the given limits and formats. Copy
271 * the limits and formats instead of adopting them.
272 *
273 * @param limits Array of limit values.
274 * @param formats Array of formats.
275 * @param count Size of 'limits' and 'formats' arrays.
276 * @stable ICU 2.0
277 */
278
279 ChoiceFormat(const double* limits,
280 const UnicodeString* formats,
281 int32_t count );
282
283 /**
284 * Construct a new ChoiceFormat with the given limits and formats.
285 * Copy the limits and formats (instead of adopting them). By
286 * default, each limit in the array specifies the inclusive lower
287 * bound of its range, and the exclusive upper bound of the previous
288 * range. However, if the isLimitOpen element corresponding to a
289 * limit is TRUE, then the limit is the exclusive lower bound of its
290 * range, and the inclusive upper bound of the previous range.
291 * @param limits Array of limit values
292 * @param closures Array of booleans specifying whether each
293 * element of 'limits' is open or closed. If FALSE, then the
294 * corresponding limit is a member of the range above it. If TRUE,
295 * then the limit belongs to the range below it.
296 * @param formats Array of formats
297 * @param count Size of 'limits', 'closures', and 'formats' arrays
298 * @stable ICU 2.4
299 */
300 ChoiceFormat(const double* limits,
301 const UBool* closures,
302 const UnicodeString* formats,
303 int32_t count);
304
305 /**
306 * Copy constructor.
307 *
308 * @param that ChoiceFormat object to be copied from
309 * @stable ICU 2.0
310 */
311 ChoiceFormat(const ChoiceFormat& that);
312
313 /**
314 * Assignment operator.
315 *
316 * @param that ChoiceFormat object to be copied
317 * @stable ICU 2.0
318 */
319 const ChoiceFormat& operator=(const ChoiceFormat& that);
320
321 /**
322 * Destructor.
323 * @stable ICU 2.0
324 */
325 virtual ~ChoiceFormat();
326
327 /**
328 * Clone this Format object polymorphically. The caller owns the
329 * result and should delete it when done.
330 *
331 * @return a copy of this object
332 * @stable ICU 2.0
333 */
334 virtual Format* clone(void) const;
335
336 /**
337 * Return true if the given Format objects are semantically equal.
338 * Objects of different subclasses are considered unequal.
339 *
340 * @param other ChoiceFormat object to be compared
341 * @return true if other is the same as this.
342 * @stable ICU 2.0
343 */
344 virtual UBool operator==(const Format& other) const;
345
346 /**
347 * Sets the pattern.
348 * @param pattern The pattern to be applied.
349 * @param status Output param set to success/failure code on
350 * exit. If the pattern is invalid, this will be
351 * set to a failure result.
352 * @stable ICU 2.0
353 */
354 virtual void applyPattern(const UnicodeString& pattern,
355 UErrorCode& status);
356
357 /**
358 * Sets the pattern.
359 * @param pattern The pattern to be applied.
360 * @param parseError Struct to recieve information on position
361 * of error if an error is encountered
362 * @param status Output param set to success/failure code on
363 * exit. If the pattern is invalid, this will be
364 * set to a failure result.
365 * @stable ICU 2.0
366 */
367 virtual void applyPattern(const UnicodeString& pattern,
368 UParseError& parseError,
369 UErrorCode& status);
370 /**
371 * Gets the pattern.
372 *
373 * @param pattern Output param which will recieve the pattern
374 * Previous contents are deleted.
375 * @return A reference to 'pattern'
376 * @stable ICU 2.0
377 */
378 virtual UnicodeString& toPattern(UnicodeString &pattern) const;
379
380 #ifdef U_USE_CHOICE_FORMAT_DEPRECATES
381 /**
382 * Set the choices to be used in formatting. The arrays are adopted and
383 * should not be deleted by the caller.
384 *
385 * @param limitsToAdopt Contains the top value that you want
386 * parsed with that format,and should be in
387 * ascending sorted order. When formatting X,
388 * the choice will be the i, where limit[i]
389 * &lt;= X &lt; limit[i+1].
390 * @param formatsToAdopt The format strings you want to use for each limit.
391 * @param count The size of the above arrays.
392 * @obsolete ICU 2.6. Use setChoices instead since this API will be removed in that release.
393 */
394 virtual void adoptChoices(double* limitsToAdopt,
395 UnicodeString* formatsToAdopt,
396 int32_t count );
397
398 /**
399 * Set the choices to be used in formatting. The arrays are adopted
400 * and should not be deleted by the caller. See class description
401 * for documenatation of the limits, closures, and formats arrays.
402 * @param limitsToAdopt Array of limits to adopt
403 * @param closuresToAdopt Array of limit booleans to adopt
404 * @param formatsToAdopt Array of format string to adopt
405 * @param count The size of the above arrays
406 * @obsolete ICU 2.6. Use setChoices instead since this API will be removed in that release.
407 */
408 virtual void adoptChoices(double* limitsToAdopt,
409 UBool* closuresToAdopt,
410 UnicodeString* formatsToAdopt,
411 int32_t count);
412 #endif
413
414 /**
415 * Set the choices to be used in formatting.
416 *
417 * @param limitsToCopy Contains the top value that you want
418 * parsed with that format,and should be in
419 * ascending sorted order. When formatting X,
420 * the choice will be the i, where limit[i]
421 * &lt;= X &lt; limit[i+1].
422 * @param formatsToCopy The format strings you want to use for each limit.
423 * @param count The size of the above arrays.
424 * @stable ICU 2.0
425 */
426 virtual void setChoices(const double* limitsToCopy,
427 const UnicodeString* formatsToCopy,
428 int32_t count );
429
430 /**
431 * Set the choices to be used in formatting. See class description
432 * for documenatation of the limits, closures, and formats arrays.
433 * @param limits Array of limits
434 * @param closures Array of limit booleans
435 * @param formats Array of format string
436 * @param count The size of the above arrays
437 * @stable ICU 2.4
438 */
439 virtual void setChoices(const double* limits,
440 const UBool* closures,
441 const UnicodeString* formats,
442 int32_t count);
443
444 /**
445 * Get the limits passed in the constructor.
446 *
447 * @param count The size of the limits arrays
448 * @return the limits.
449 * @stable ICU 2.0
450 */
451 virtual const double* getLimits(int32_t& count) const;
452
453 /**
454 * Get the limit booleans passed in the constructor. The caller
455 * must not delete the result.
456 *
457 * @param count The size of the arrays
458 * @return the closures
459 * @stable ICU 2.4
460 */
461 virtual const UBool* getClosures(int32_t& count) const;
462
463 /**
464 * Get the formats passed in the constructor.
465 *
466 * @param count The size of the arrays
467 * @return the formats.
468 * @stable ICU 2.0
469 */
470 virtual const UnicodeString* getFormats(int32_t& count) const;
471
472 /**
473 * Format a double or long number using this object's choices.
474 *
475 * @param number The value to be formatted.
476 * @param appendTo Output parameter to receive result.
477 * Result is appended to existing contents.
478 * @param pos On input: an alignment field, if desired.
479 * On output: the offsets of the alignment field.
480 * @return Reference to 'appendTo' parameter.
481 * @stable ICU 2.0
482 */
483 virtual UnicodeString& format(double number,
484 UnicodeString& appendTo,
485 FieldPosition& pos) const;
486 /**
487 * Format a int_32t number using this object's choices.
488 *
489 * @param number The value to be formatted.
490 * @param appendTo Output parameter to receive result.
491 * Result is appended to existing contents.
492 * @param pos On input: an alignment field, if desired.
493 * On output: the offsets of the alignment field.
494 * @return Reference to 'appendTo' parameter.
495 * @stable ICU 2.0
496 */
497 virtual UnicodeString& format(int32_t number,
498 UnicodeString& appendTo,
499 FieldPosition& pos) const;
500
501 /**
502 * Format an int64_t number using this object's choices.
503 *
504 * @param number The value to be formatted.
505 * @param appendTo Output parameter to receive result.
506 * Result is appended to existing contents.
507 * @param pos On input: an alignment field, if desired.
508 * On output: the offsets of the alignment field.
509 * @return Reference to 'appendTo' parameter.
510 * @draft ICU 2.8
511 */
512 virtual UnicodeString& format(int64_t number,
513 UnicodeString& appendTo,
514 FieldPosition& pos) const;
515
516 /**
517 * Format an array of objects using this object's choices.
518 *
519 * @param objs The array of objects to be formatted.
520 * @param cnt The size of objs.
521 * @param appendTo Output parameter to receive result.
522 * Result is appended to existing contents.
523 * @param pos On input: an alignment field, if desired.
524 * On output: the offsets of the alignment field.
525 * @param success Output param set to success/failure code on
526 * exit.
527 * @return Reference to 'appendTo' parameter.
528 * @stable ICU 2.0
529 */
530 virtual UnicodeString& format(const Formattable* objs,
531 int32_t cnt,
532 UnicodeString& appendTo,
533 FieldPosition& pos,
534 UErrorCode& success) const;
535 /**
536 * Format an object using this object's choices.
537 *
538 *
539 * @param obj The object to be formatted.
540 * @param appendTo Output parameter to receive result.
541 * Result is appended to existing contents.
542 * @param pos On input: an alignment field, if desired.
543 * On output: the offsets of the alignment field.
544 * @param status Output param set to success/failure code on
545 * exit.
546 * @return Reference to 'appendTo' parameter.
547 * @stable ICU 2.0
548 */
549 virtual UnicodeString& format(const Formattable& obj,
550 UnicodeString& appendTo,
551 FieldPosition& pos,
552 UErrorCode& status) const;
553
554 /**
555 * Redeclared NumberFormat method.
556 *
557 * @param obj The object to be formatted.
558 * @param appendTo Output parameter to receive result.
559 * Result is appended to existing contents.
560 * @param status Output param set to success/failure code on
561 * exit.
562 * @return Reference to 'appendTo' parameter.
563 * @stable ICU 2.0
564 */
565 UnicodeString& format(const Formattable& obj,
566 UnicodeString& appendTo,
567 UErrorCode& status) const;
568
569 /**
570 * Redeclared NumberFormat method.
571 * Format a double number. These methods call the NumberFormat
572 * pure virtual format() methods with the default FieldPosition.
573 *
574 * @param number The value to be formatted.
575 * @param appendTo Output parameter to receive result.
576 * Result is appended to existing contents.
577 * @return Reference to 'appendTo' parameter.
578 * @stable ICU 2.0
579 */
580 UnicodeString& format( double number,
581 UnicodeString& appendTo) const;
582
583 /**
584 * Redeclared NumberFormat method.
585 * Format a long number. These methods call the NumberFormat
586 * pure virtual format() methods with the default FieldPosition.
587 *
588 * @param number The value to be formatted.
589 * @param appendTo Output parameter to receive result.
590 * Result is appended to existing contents.
591 * @return Reference to 'appendTo' parameter.
592 * @stable ICU 2.0
593 */
594 UnicodeString& format( int32_t number,
595 UnicodeString& appendTo) const;
596
597 /**
598 * Return a long if possible (e.g. within range LONG_MAX,
599 * LONG_MAX], and with no decimals), otherwise a double. If
600 * IntegerOnly is set, will stop at a decimal point (or equivalent;
601 * e.g. for rational numbers "1 2/3", will stop after the 1).
602 * <P>
603 * If no object can be parsed, parsePosition is unchanged, and NULL is
604 * returned.
605 *
606 * @param text The text to be parsed.
607 * @param result Formattable to be set to the parse result.
608 * If parse fails, return contents are undefined.
609 * @param parsePosition The position to start parsing at on input.
610 * On output, moved to after the last successfully
611 * parse character. On parse failure, does not change.
612 * @see NumberFormat::isParseIntegerOnly
613 * @stable ICU 2.0
614 */
615 virtual void parse(const UnicodeString& text,
616 Formattable& result,
617 ParsePosition& parsePosition) const;
618
619 /**
620 * Return a long if possible (e.g. within range LONG_MAX,
621 * LONG_MAX], and with no decimals), otherwise a double. If
622 * IntegerOnly is set, will stop at a decimal point (or equivalent;
623 * e.g. for rational numbers "1 2/3", will stop after the 1).
624 * <P>
625 * If no object can be parsed, parsePosition is unchanged, and NULL is
626 * returned.
627 *
628 * @param text The text to be parsed.
629 * @param result Formattable to be set to the parse result.
630 * If parse fails, return contents are undefined.
631 * @param status Output param with the formatted string.
632 * @see NumberFormat::isParseIntegerOnly
633 * @stable ICU 2.0
634 */
635 virtual void parse(const UnicodeString& text,
636 Formattable& result,
637 UErrorCode& status) const;
638
639
640 public:
641 /**
642 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override.
643 * This method is to implement a simple version of RTTI, since not all
644 * C++ compilers support genuine RTTI. Polymorphic operator==() and
645 * clone() methods call this method.
646 *
647 * @return The class ID for this object. All objects of a
648 * given class have the same class ID. Objects of
649 * other classes have different class IDs.
650 * @stable ICU 2.0
651 */
652 virtual UClassID getDynamicClassID(void) const;
653
654 /**
655 * Return the class ID for this class. This is useful only for
656 * comparing to a return value from getDynamicClassID(). For example:
657 * <pre>
658 * . Base* polymorphic_pointer = createPolymorphicObject();
659 * . if (polymorphic_pointer->getDynamicClassID() ==
660 * . Derived::getStaticClassID()) ...
661 * </pre>
662 * @return The class ID for all objects of this class.
663 * @stable ICU 2.0
664 */
665 static UClassID U_EXPORT2 getStaticClassID(void);
666
667 private:
668 // static cache management (thread-safe)
669 // static NumberFormat* getNumberFormat(UErrorCode &status); // call this function to 'check out' a numberformat from the cache.
670 // static void releaseNumberFormat(NumberFormat *adopt); // call this function to 'return' the number format to the cache.
671
672 /**
673 * Converts a string to a double value using a default NumberFormat object
674 * which is static (shared by all ChoiceFormat instances).
675 * @param string the string to be converted with.
676 * @return the converted double number.
677 */
678 static double stod(const UnicodeString& string);
679
680 /**
681 * Converts a double value to a string using a default NumberFormat object
682 * which is static (shared by all ChoiceFormat instances).
683 * @param value the double number to be converted with.
684 * @param string the result string.
685 * @return the converted string.
686 */
687 static UnicodeString& dtos(double value, UnicodeString& string);
688
689 ChoiceFormat(); // default constructor not implemented
690
691 /**
692 * Construct a new ChoiceFormat with the limits and the corresponding formats
693 * based on the pattern.
694 *
695 * @param newPattern Pattern used to construct object.
696 * @param parseError Struct to recieve information on position
697 * of error if an error is encountered.
698 * @param status Output param to receive success code. If the
699 * pattern cannot be parsed, set to failure code.
700 * @stable ICU 2.0
701 */
702 ChoiceFormat(const UnicodeString& newPattern,
703 UParseError& parseError,
704 UErrorCode& status);
705
706 friend class MessageFormat;
707 /**
708 * Each ChoiceFormat divides the range -Inf..+Inf into fCount
709 * intervals. The intervals are:
710 *
711 * 0: fChoiceLimits[0]..fChoiceLimits[1]
712 * 1: fChoiceLimits[1]..fChoiceLimits[2]
713 * ...
714 * fCount-2: fChoiceLimits[fCount-2]..fChoiceLimits[fCount-1]
715 * fCount-1: fChoiceLimits[fCount-1]..+Inf
716 *
717 * Interval 0 is special; during formatting (mapping numbers to
718 * strings), it also contains all numbers less than
719 * fChoiceLimits[0], as well as NaN values.
720 *
721 * Interval i maps to and from string fChoiceFormats[i]. When
722 * parsing (mapping strings to numbers), then intervals map to
723 * their lower limit, that is, interval i maps to fChoiceLimit[i].
724 *
725 * The intervals may be closed, half open, or open. This affects
726 * formatting but does not affect parsing. Interval i is affected
727 * by fClosures[i] and fClosures[i+1]. If fClosures[i]
728 * is FALSE, then the value fChoiceLimits[i] is in interval i.
729 * That is, intervals i and i are:
730 *
731 * i-1: ... x < fChoiceLimits[i]
732 * i: fChoiceLimits[i] <= x ...
733 *
734 * If fClosures[i] is TRUE, then the value fChoiceLimits[i] is
735 * in interval i-1. That is, intervals i-1 and i are:
736 *
737 * i-1: ... x <= fChoiceLimits[i]
738 * i: fChoiceLimits[i] < x ...
739 *
740 * Because of the nature of interval 0, fClosures[0] has no
741 * effect.
742
743 */
744 double* fChoiceLimits;
745 UBool* fClosures;
746 UnicodeString* fChoiceFormats;
747 int32_t fCount;
748 };
749
750 inline UnicodeString&
751 ChoiceFormat::format(const Formattable& obj,
752 UnicodeString& appendTo,
753 UErrorCode& status) const {
754 // Don't use Format:: - use immediate base class only,
755 // in case immediate base modifies behavior later.
756 return NumberFormat::format(obj, appendTo, status);
757 }
758
759 inline UnicodeString&
760 ChoiceFormat::format(double number,
761 UnicodeString& appendTo) const {
762 return NumberFormat::format(number, appendTo);
763 }
764
765 inline UnicodeString&
766 ChoiceFormat::format(int32_t number,
767 UnicodeString& appendTo) const {
768 return NumberFormat::format(number, appendTo);
769 }
770 U_NAMESPACE_END
771
772 #endif /* #if !UCONFIG_NO_FORMATTING */
773
774 #endif // _CHOICFMT
775 //eof