org.apache.commons.math.fraction
Class FractionFormat

java.lang.Object
  extended by java.text.Format
      extended by org.apache.commons.math.fraction.FractionFormat
All Implemented Interfaces:
Serializable, Cloneable
Direct Known Subclasses:
ProperFractionFormat

public class FractionFormat
extends Format
implements Serializable

Formats a Fraction number in proper format or improper format. The number format for each of the whole number, numerator and, denominator can be configured.

Since:
1.1
Version:
$Revision: 348519 $ $Date: 2005-11-23 12:12:18 -0700 (Wed, 23 Nov 2005) $
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.text.Format
Format.Field
 
Constructor Summary
FractionFormat()
          Create an improper formatting instance with the default number format for the numerator and denominator.
FractionFormat(NumberFormat format)
          Create an improper formatting instance with a custom number format for both the numerator and denominator.
FractionFormat(NumberFormat numeratorFormat, NumberFormat denominatorFormat)
          Create an improper formatting instance with a custom number format for the numerator and a custom number format for the denominator.
 
Method Summary
 StringBuffer format(Fraction fraction, StringBuffer toAppendTo, FieldPosition pos)
          Formats a Fraction object to produce a string.
 StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos)
          Formats a object to produce a string.
static String formatFraction(Fraction f)
          This static method calls formatFraction() on a default instance of FractionFormat.
static Locale[] getAvailableLocales()
          Get the set of locales for which complex formats are available.
protected static NumberFormat getDefaultNumberFormat()
          Create a default number format.
 NumberFormat getDenominatorFormat()
          Access the denominator format.
static FractionFormat getImproperInstance()
          Returns the default complex format for the current locale.
static FractionFormat getImproperInstance(Locale locale)
          Returns the default complex format for the given locale.
 NumberFormat getNumeratorFormat()
          Access the numerator format.
static FractionFormat getProperInstance()
          Returns the default complex format for the current locale.
static FractionFormat getProperInstance(Locale locale)
          Returns the default complex format for the given locale.
 Fraction parse(String source)
          Parses a string to produce a Fraction object.
 Fraction parse(String source, ParsePosition pos)
          Parses a string to produce a Fraction object.
protected static void parseAndIgnoreWhitespace(String source, ParsePosition pos)
          Parses source until a non-whitespace character is found.
protected static char parseNextCharacter(String source, ParsePosition pos)
          Parses source until a non-whitespace character is found.
 Object parseObject(String source, ParsePosition pos)
          Parses a string to produce a object.
 void setDenominatorFormat(NumberFormat format)
          Modify the denominator format.
 void setNumeratorFormat(NumberFormat format)
          Modify the numerator format.
 
Methods inherited from class java.text.Format
clone, format, formatToCharacterIterator, parseObject
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FractionFormat

public FractionFormat()
Create an improper formatting instance with the default number format for the numerator and denominator.


FractionFormat

public FractionFormat(NumberFormat format)
Create an improper formatting instance with a custom number format for both the numerator and denominator.

Parameters:
format - the custom format for both the numerator and denominator.

FractionFormat

public FractionFormat(NumberFormat numeratorFormat,
                      NumberFormat denominatorFormat)
Create an improper formatting instance with a custom number format for the numerator and a custom number format for the denominator.

Parameters:
numeratorFormat - the custom format for the numerator.
denominatorFormat - the custom format for the denominator.
Method Detail

formatFraction

public static String formatFraction(Fraction f)
This static method calls formatFraction() on a default instance of FractionFormat.

Parameters:
f - Fraction object to format
Returns:
A formatted fraction in proper form.

getAvailableLocales

public static Locale[] getAvailableLocales()
Get the set of locales for which complex formats are available. This is the same set as the NumberFormat set.

Returns:
available complex format locales.

getImproperInstance

public static FractionFormat getImproperInstance()
Returns the default complex format for the current locale.

Returns:
the default complex format.

getImproperInstance

public static FractionFormat getImproperInstance(Locale locale)
Returns the default complex format for the given locale.

Parameters:
locale - the specific locale used by the format.
Returns:
the complex format specific to the given locale.

getProperInstance

public static FractionFormat getProperInstance()
Returns the default complex format for the current locale.

Returns:
the default complex format.

getProperInstance

public static FractionFormat getProperInstance(Locale locale)
Returns the default complex format for the given locale.

Parameters:
locale - the specific locale used by the format.
Returns:
the complex format specific to the given locale.

getDefaultNumberFormat

protected static NumberFormat getDefaultNumberFormat()
Create a default number format. The default number format is based on NumberFormat.getNumberInstance(java.util.Locale) with the only customizing is the maximum number of fraction digits, which is set to 0.

Returns:
the default number format.

format

public StringBuffer format(Fraction fraction,
                           StringBuffer toAppendTo,
                           FieldPosition pos)
Formats a Fraction object to produce a string. The fraction is output in improper format.

Parameters:
fraction - the object to format.
toAppendTo - where the text is to be appended
pos - On input: an alignment field, if desired. On output: the offsets of the alignment field
Returns:
the value passed in as toAppendTo.

format

public StringBuffer format(Object obj,
                           StringBuffer toAppendTo,
                           FieldPosition pos)
Formats a object to produce a string. obj must be either a Fraction object or a Number object. Any other type of object will result in an IllegalArgumentException being thrown.

Specified by:
format in class Format
Parameters:
obj - the object to format.
toAppendTo - where the text is to be appended
pos - On input: an alignment field, if desired. On output: the offsets of the alignment field
Returns:
the value passed in as toAppendTo.
Throws:
IllegalArgumentException - is obj is not a valid type.
See Also:
Format.format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition)

getDenominatorFormat

public NumberFormat getDenominatorFormat()
Access the denominator format.

Returns:
the denominator format.

getNumeratorFormat

public NumberFormat getNumeratorFormat()
Access the numerator format.

Returns:
the numerator format.

parse

public Fraction parse(String source)
               throws ParseException
Parses a string to produce a Fraction object.

Parameters:
source - the string to parse
Returns:
the parsed Fraction object.
Throws:
ParseException - if the beginning of the specified string cannot be parsed.

parse

public Fraction parse(String source,
                      ParsePosition pos)
Parses a string to produce a Fraction object. This method expects the string to be formatted as an improper fraction.

Parameters:
source - the string to parse
pos - input/ouput parsing parameter.
Returns:
the parsed Fraction object.

parseObject

public Object parseObject(String source,
                          ParsePosition pos)
Parses a string to produce a object.

Specified by:
parseObject in class Format
Parameters:
source - the string to parse
pos - input/ouput parsing parameter.
Returns:
the parsed object.
See Also:
Format.parseObject(java.lang.String, java.text.ParsePosition)

setDenominatorFormat

public void setDenominatorFormat(NumberFormat format)
Modify the denominator format.

Parameters:
format - the new denominator format value.
Throws:
IllegalArgumentException - if format is null.

setNumeratorFormat

public void setNumeratorFormat(NumberFormat format)
Modify the numerator format.

Parameters:
format - the new numerator format value.
Throws:
IllegalArgumentException - if format is null.

parseAndIgnoreWhitespace

protected static void parseAndIgnoreWhitespace(String source,
                                               ParsePosition pos)
Parses source until a non-whitespace character is found.

Parameters:
source - the string to parse
pos - input/ouput parsing parameter. On output, pos holds the index of the next non-whitespace character.

parseNextCharacter

protected static char parseNextCharacter(String source,
                                         ParsePosition pos)
Parses source until a non-whitespace character is found.

Parameters:
source - the string to parse
pos - input/ouput parsing parameter.
Returns:
the first non-whitespace character.


Copyright © 2003-2009 The Apache Software Foundation. All Rights Reserved.