Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Header Files   Compound Members   File Members  

ChoiceFormat Class Reference

A ChoiceFormat allows you to attach a format to a range of numbers. More...

#include <choicfmt.h>

Class diagram for ChoiceFormat:

NumberFormat Format

List of all members.


Public Members

 ChoiceFormat (const UnicodeString& newPattern, UErrorCode& status)
Construct a new ChoiceFormat with the limits and the corresponding formats based on the pattern. More...

 ChoiceFormat (const double* limits, const UnicodeString* formats, int32_t count )
Construct a new ChoiceFormat with the given limits and formats. More...

 ChoiceFormat (const ChoiceFormat&)
Copy constructor. More...

const ChoiceFormat& operator= (const ChoiceFormat&)
Assignment operator. More...

virtual ~ChoiceFormat ()
Destructor. More...

virtual Formatclone (void) const
Clone this Format object polymorphically. More...

virtual bool_t operator== (const Format& other) const
Return true if the given Format objects are semantically equal. More...

virtual void applyPattern (const UnicodeString& pattern, UErrorCode& status)
Sets the pattern. More...

virtual UnicodeStringtoPattern (UnicodeString &pattern) const
Gets the pattern. More...

virtual void adoptChoices (double* limitsToAdopt, UnicodeString* formatsToAdopt, int32_t count )
Set the choices to be used in formatting. More...

virtual void setChoices (const double* limitsToCopy, const UnicodeString* formatsToCopy, int32_t count )
Set the choices to be used in formatting. More...

virtual const double* getLimits (int32_t& count) const
Get the limits passed in the constructor. More...

virtual const UnicodeStringgetFormats (int32_t& count) const
Get the formats passed in the constructor. More...

virtual UnicodeStringformat (double number, UnicodeString& toAppendTo, FieldPosition& pos) const
Format a double or long number using this object's choices. More...

virtual UnicodeStringformat (int32_t number, UnicodeString& toAppendTo, FieldPosition& pos) const
Format a int_32t number using this object's choices. More...

virtual UnicodeStringformat (const Formattable* objs, int32_t cnt, UnicodeString& toAppendTo, FieldPosition& pos, UErrorCode& success) const
Format an array of objects using this object's choices. More...

virtual UnicodeStringformat (const Formattable& obj, UnicodeString& toAppendTo, FieldPosition& pos, UErrorCode& status) const
Format an object using this object's choices. More...

UnicodeStringformat (const Formattable& obj, UnicodeString& result, UErrorCode& status) const
Redeclared NumberFormat method. More...

UnicodeStringformat ( double number, UnicodeString& output) const
Redeclared NumberFormat method. More...

UnicodeStringformat ( int32_t number, UnicodeString& output) const
Redeclared NumberFormat method. More...

virtual void parse (const UnicodeString& text, Formattable& result, ParsePosition& parsePosition) const
Return a long if possible (e.g. More...

virtual void parse (const UnicodeString& text, Formattable& result, UErrorCode& status) const
Parse a string as a numeric value, and return a Formattable numeric object. More...

virtual UClassID getDynamicClassID (void) const
Returns a unique class ID POLYMORPHICALLY. More...


Static Public Members

UClassID getStaticClassID (void)
Return the class ID for this class. More...

double nextDouble (double d, bool_t positive)
double nextDouble (double d )
Finds the least double greater than d. More...

double previousDouble (double d )
Finds the greatest double less than d. More...


Detailed Description

A ChoiceFormat allows you to attach a format to a range of numbers.

It is generally used in a MessageFormat for doing things like plurals. The choice is specified with an ascending list of doubles, where each item specifies a half-open interval up to the next item:

 .    X matches j if and only if limit[j] &lt;= X &lt; limit[j+1]
 
If there is no match, then either the first or last index is used, depending on whether the number is too low or too high. The length of the array of formats must be the same as the length of the array of limits. For example,
 .     {1,2,3,4,5,6,7},
 .          {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"}
 .     {0, 1, ChoiceFormat::nextDouble(1)},
 .          {"no files", "one file", "many files"}
 
(nextDouble can be used to get the next higher double, to make the half-open interval.)

Here is a simple example that shows formatting and parsing:

 .  void SimpleChoiceExample( void )
 .  {
 .      double limits[] = {1,2,3,4,5,6,7};
 .      UnicodeString monthNames[] = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"};
 .      ChoiceFormat* form = new ChoiceFormat(limits, monthNames, 7 );
 .      ParsePosition* status = new ParsePosition(0);
 .      UnicodeString str;
 .      FieldPosition f1(0), f2(0);
 .      for (double i = 0.0; i &lt;= 8.0; ++i) {
 .          status->setIndex(0);
 .          Formattable parseResult;
 .          str.remove();
 .          cout &lt;&lt; i &lt;&lt; " -> " &lt;&lt; form->format(i,str, f1) 
 .                    &lt;&lt; " -> " &lt;&lt; parseResult &lt;&lt; endl;
 .      }
 .      delete form;
 .      delete status;
 .      cout &lt;&lt; endl;
 .  }
 
Here is a more complex example, with a pattern format.
 .  void ComplexChoiceExample( void )
 .  {
 .      double filelimits[] = {0,1,2};
 .      UnicodeString filepart[] = {"are no files","is one file","are {2} files"};
 .      ChoiceFormat* fileform = new ChoiceFormat(filelimits, filepart, 3 );
 .      UErrorCode success = U_ZERO_ERROR;
 .      const Format* testFormats[] = { fileform, NULL, NumberFormat::createInstance(success) };
 .      MessageFormat* pattform = new MessageFormat("There {0} on {1}", success );
 .      pattform->setFormats( testFormats, 3 );
 .      Formattable testArgs[] = {0L, "Disk_A", 0L};
 .      FieldPosition fp(0);
 .      UnicodeString str;
 .      for (int32_t i = 0; i &lt; 4; ++i) {
 .          Formattable fInt(i);
 .          testArgs[0] = fInt;
 .          testArgs[2] = testArgs[0];
 .          str.remove();
 .          pattform->format(testArgs, 3, str, fp, success );
 .          cout &lt;&lt; "Output for i=" &lt;&lt; i &lt;&lt; " : " &lt;&lt; str &lt;&lt; endl;
 .      }
 .      delete pattform;
 .      cout &lt;&lt; endl;
 .  }
 
ChoiceFormat objects may be converted to and from patterns. The syntax of these patterns is [TODO fill in this section with detail]. Here is an example of a ChoiceFormat pattern:

You can either do this programmatically, as in the above example, or by using a pattern (see ChoiceFormat for more information) as in:

 .       "0#are no files|1#is one file|1&lt;are many files"
 
Here the notation is:
 .       &lt;number> "#"  Specifies a limit value.
 .       &lt;number> "&lt;"  Specifies a limit of nextDouble(&lt;number>).
 .       &lt;number> ">"  Specifies a limit of previousDouble(&lt;number>).
 
Each limit value is followed by a string, which is terminated by a vertical bar character ("|"), except for the last string, which is terminated by the end of the string.

Member Function Documentation

ChoiceFormat::ChoiceFormat (const UnicodeString & newPattern, UErrorCode & status)

Construct a new ChoiceFormat with the limits and the corresponding formats based on the pattern.

Parameters:
pattern   Pattern used to construct object.
status   Output param to receive success code. If the pattern cannot be parsed, set to failure code.
Stable:

ChoiceFormat::ChoiceFormat (const double * limits, const UnicodeString * formats, int32_t count)

Construct a new ChoiceFormat with the given limits and formats.

Copy the limits and formats instead of adopting them.

Parameters:
limits   Array of limit values.
formats   Array of formats.
count   Size of 'limits' and 'formats' arrays.
Stable:

ChoiceFormat::ChoiceFormat (const ChoiceFormat &)

Copy constructor.

Stable:

const ChoiceFormat & ChoiceFormat::operator= (const ChoiceFormat &)

Assignment operator.

Stable:

virtual ChoiceFormat::~ChoiceFormat () [virtual]

Destructor.

Stable:

virtual Format * ChoiceFormat::clone (void) const [virtual]

Clone this Format object polymorphically.

The caller owns the result and should delete it when done.

Stable:

Reimplemented from Format.

virtual bool_t ChoiceFormat::operator== (const Format & other) const [virtual]

Return true if the given Format objects are semantically equal.

Objects of different subclasses are considered unequal.

Stable:

Reimplemented from Format.

virtual void ChoiceFormat::applyPattern (const UnicodeString & pattern, UErrorCode & status) [virtual]

Sets the pattern.

Parameters:
pattern   The pattern to be applied.
status   Output param set to success/failure code on exit. If the pattern is invalid, this will be set to a failure result.
Stable:

virtual UnicodeString & ChoiceFormat::toPattern (UnicodeString & pattern) const [virtual]

Gets the pattern.

Stable:

virtual void ChoiceFormat::adoptChoices (double * limitsToAdopt, UnicodeString * formatsToAdopt, int32_t count) [virtual]

Set the choices to be used in formatting.

The arrays are adopted and should not be deleted by the caller.

Parameters:
limitsToAdopt   Contains the top value that you want parsed with that format,and should be in ascending sorted order. When formatting X, the choice will be the i, where limit[i] <= X < limit[i+1].
formatsToAdopt   The format strings you want to use for each limit.
count   The size of the above arrays.
Stable:

virtual void ChoiceFormat::setChoices (const double * limitsToCopy, const UnicodeString * formatsToCopy, int32_t count) [virtual]

Set the choices to be used in formatting.

Parameters:
limitsToCopy   Contains the top value that you want parsed with that format,and should be in ascending sorted order. When formatting X, the choice will be the i, where limit[i] <= X < limit[i+1].
formatsToCopy   The format strings you want to use for each limit.
count   The size of the above arrays.
Stable:

virtual const double * ChoiceFormat::getLimits (int32_t & count) const [virtual]

Get the limits passed in the constructor.

Returns:
the limits.
Stable:

virtual const UnicodeString * ChoiceFormat::getFormats (int32_t & count) const [virtual]

Get the formats passed in the constructor.

Returns:
the formats.
Stable:

virtual UnicodeString & ChoiceFormat::format (double number, UnicodeString & toAppendTo, FieldPosition & pos) const [virtual]

Format a double or long number using this object's choices.

Parameters:
number   The value to be formatted.
toAppendTo   The string to append the formatted string to. This is an output parameter.
pos   On input: an alignment field, if desired. On output: the offsets of the alignment field.
Returns:
A reference to 'toAppendTo'.
Stable:

Reimplemented from NumberFormat.

virtual UnicodeString & ChoiceFormat::format (int32_t number, UnicodeString & toAppendTo, FieldPosition & pos) const [virtual]

Format a int_32t number using this object's choices.

Stable:

Reimplemented from NumberFormat.

virtual UnicodeString & ChoiceFormat::format (const Formattable * objs, int32_t cnt, UnicodeString & toAppendTo, FieldPosition & pos, UErrorCode & success) const [virtual]

Format an array of objects using this object's choices.

Stable:

virtual UnicodeString & ChoiceFormat::format (const Formattable & obj, UnicodeString & toAppendTo, FieldPosition & pos, UErrorCode & status) const [virtual]

Format an object using this object's choices.

Stable:

Reimplemented from Format.

UnicodeString & ChoiceFormat::format (const Formattable & obj, UnicodeString & result, UErrorCode & status) const [inline]

Redeclared NumberFormat method.

Stable:

Reimplemented from NumberFormat.

UnicodeString & ChoiceFormat::format (double number, UnicodeString & output) const [inline]

Redeclared NumberFormat method.

Stable:

Reimplemented from NumberFormat.

UnicodeString & ChoiceFormat::format (int32_t number, UnicodeString & output) const [inline]

Redeclared NumberFormat method.

Stable:

Reimplemented from NumberFormat.

virtual void ChoiceFormat::parse (const UnicodeString & text, Formattable & result, ParsePosition & parsePosition) const [virtual]

Return a long if possible (e.g.

within range LONG_MAX, LONG_MAX], and with no decimals), otherwise a double. If IntegerOnly is set, will stop at a decimal point (or equivalent; e.g. for rational numbers "1 2/3", will stop after the 1).

If no object can be parsed, parsePosition is unchanged, and NULL is returned.

Parameters:
text   The text to be parsed.
result   Formattable to be set to the parse result. If parse fails, return contents are undefined.
parsePosition   The position to start parsing at on input. On output, moved to after the last successfully parse character. On parse failure, does not change.
Returns:
A Formattable object of numeric type. The caller owns this an must delete it. NULL on failure.
See also:
NumberFormat::isParseIntegerOnly()
Stable:

Reimplemented from NumberFormat.

virtual void ChoiceFormat::parse (const UnicodeString & text, Formattable & result, UErrorCode & status) const [virtual]

Parse a string as a numeric value, and return a Formattable numeric object.

This method parses integers only if IntegerOnly is set.

Parameters:
text   The text to be parsed.
result   Formattable to be set to the parse result. If parse fails, return contents are undefined.
status   Success or failure output parameter.
Returns:
A Formattable object of numeric type. The caller owns this an must delete it. NULL on failure.
See also:
NumberFormat::isParseIntegerOnly()
Stable:

Reimplemented from NumberFormat.

UClassID ChoiceFormat::getDynamicClassID (void) const [inline, virtual]

Returns a unique class ID POLYMORPHICALLY.

Pure virtual override. This method is to implement a simple version of RTTI, since not all C++ compilers support genuine RTTI. Polymorphic operator==() and clone() methods call this method.

Returns:
The class ID for this object. All objects of a given class have the same class ID. Objects of other classes have different class IDs.
Stable:

Reimplemented from Format.

UClassID ChoiceFormat::getStaticClassID (void) [inline, static]

Return the class ID for this class.

This is useful only for comparing to a return value from getDynamicClassID(). For example:

 .       Base* polymorphic_pointer = createPolymorphicObject();
 .       if (polymorphic_pointer->getDynamicClassID() ==
 .           Derived::getStaticClassID()) ...
 
Returns:
The class ID for all objects of this class.
Stable:

Reimplemented from NumberFormat.

double ChoiceFormat::nextDouble (double d, bool_t positive) [static]

double ChoiceFormat::nextDouble (double d) [inline, static]

Finds the least double greater than d.

If NaN, returns same value. Used to make half-open intervals.

See also:
ChoiceFormat::previousDouble()
Stable:

double ChoiceFormat::previousDouble (double d) [inline, static]

Finds the greatest double less than d.

If NaN, returns same value.

See also:
ChoiceFormat::nextDouble()
Stable:

The documentation for this class was generated from the following file:
Generated at Thu Feb 10 15:30:29 2000 for icu by doxygen 1.0.0 written by Dimitri van Heesch, © 1997-1999