org.apache.commons.math.complex
Class ComplexFormat

java.lang.Object
  extended by java.text.Format
      extended by org.apache.commons.math.complex.ComplexFormat
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable

public class ComplexFormat
extends java.text.Format
implements java.io.Serializable

Formats a Complex number in cartesian format "Re(c) + Im(c)i". 'i' can be replaced with 'j', and the number format for both real and imaginary parts can be configured.

Version:
$Revision: 480440 $ $Date: 2006-11-29 00:14:12 -0700 (Wed, 29 Nov 2006) $
Author:
Apache Software Foundation
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.text.Format
java.text.Format.Field
 
Field Summary
private static java.lang.String DEFAULT_IMAGINARY_CHARACTER
          The default imaginary character.
private  java.lang.String imaginaryCharacter
          The notation used to signify the imaginary part of the complex number.
private  java.text.NumberFormat imaginaryFormat
          The format used for the imaginary part.
private  java.text.NumberFormat realFormat
          The format used for the real part.
private static long serialVersionUID
          Serializable version identifier
 
Constructor Summary
ComplexFormat()
          Create an instance with the default imaginary character, 'i', and the default number format for both real and imaginary parts.
ComplexFormat(java.text.NumberFormat format)
          Create an instance with a custom number format for both real and imaginary parts.
ComplexFormat(java.text.NumberFormat realFormat, java.text.NumberFormat imaginaryFormat)
          Create an instance with a custom number format for the real part and a custom number format for the imaginary part.
ComplexFormat(java.lang.String imaginaryCharacter)
          Create an instance with a custom imaginary character, and the default number format for both real and imaginary parts.
ComplexFormat(java.lang.String imaginaryCharacter, java.text.NumberFormat format)
          Create an instance with a custom imaginary character, and a custom number format for both real and imaginary parts.
ComplexFormat(java.lang.String imaginaryCharacter, java.text.NumberFormat realFormat, java.text.NumberFormat imaginaryFormat)
          Create an instance with a custom imaginary character, a custom number format for the real part, and a custom number format for the imaginary part.
 
Method Summary
 java.lang.StringBuffer format(Complex complex, java.lang.StringBuffer toAppendTo, java.text.FieldPosition pos)
          Formats a Complex object to produce a string.
 java.lang.StringBuffer format(java.lang.Object obj, java.lang.StringBuffer toAppendTo, java.text.FieldPosition pos)
          Formats a object to produce a string.
static java.lang.String formatComplex(Complex c)
          This static method calls formatComplex() on a default instance of ComplexFormat.
private  java.lang.StringBuffer formatDouble(double value, java.text.NumberFormat format, java.lang.StringBuffer toAppendTo, java.text.FieldPosition pos)
          Formats a double value to produce a string.
static java.util.Locale[] getAvailableLocales()
          Get the set of locales for which complex formats are available.
private static java.text.NumberFormat getDefaultNumberFormat()
          Create a default number format.
private static java.text.NumberFormat getDefaultNumberFormat(java.util.Locale locale)
          Create a default number format.
 java.lang.String getImaginaryCharacter()
          Access the imaginaryCharacter.
 java.text.NumberFormat getImaginaryFormat()
          Access the imaginaryFormat.
static ComplexFormat getInstance()
          Returns the default complex format for the current locale.
static ComplexFormat getInstance(java.util.Locale locale)
          Returns the default complex format for the given locale.
 java.text.NumberFormat getRealFormat()
          Access the realFormat.
 Complex parse(java.lang.String source)
          Parses a string to produce a Complex object.
 Complex parse(java.lang.String source, java.text.ParsePosition pos)
          Parses a string to produce a Complex object.
private  void parseAndIgnoreWhitespace(java.lang.String source, java.text.ParsePosition pos)
          Parses source until a non-whitespace character is found.
private  char parseNextCharacter(java.lang.String source, java.text.ParsePosition pos)
          Parses source until a non-whitespace character is found.
private  java.lang.Number parseNumber(java.lang.String source, double value, java.text.ParsePosition pos)
          Parses source for a special double values.
private  java.lang.Number parseNumber(java.lang.String source, java.text.NumberFormat format, java.text.ParsePosition pos)
          Parses source for a number.
 java.lang.Object parseObject(java.lang.String source, java.text.ParsePosition pos)
          Parses a string to produce a object.
 void setImaginaryCharacter(java.lang.String imaginaryCharacter)
          Modify the imaginaryCharacter.
 void setImaginaryFormat(java.text.NumberFormat imaginaryFormat)
          Modify the imaginaryFormat.
 void setRealFormat(java.text.NumberFormat realFormat)
          Modify the realFormat.
 
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
 

Field Detail

serialVersionUID

private static final long serialVersionUID
Serializable version identifier

See Also:
Constant Field Values

DEFAULT_IMAGINARY_CHARACTER

private static final java.lang.String DEFAULT_IMAGINARY_CHARACTER
The default imaginary character.

See Also:
Constant Field Values

imaginaryCharacter

private java.lang.String imaginaryCharacter
The notation used to signify the imaginary part of the complex number.


imaginaryFormat

private java.text.NumberFormat imaginaryFormat
The format used for the imaginary part.


realFormat

private java.text.NumberFormat realFormat
The format used for the real part.

Constructor Detail

ComplexFormat

public ComplexFormat()
Create an instance with the default imaginary character, 'i', and the default number format for both real and imaginary parts.


ComplexFormat

public ComplexFormat(java.text.NumberFormat format)
Create an instance with a custom number format for both real and imaginary parts.

Parameters:
format - the custom format for both real and imaginary parts.

ComplexFormat

public ComplexFormat(java.text.NumberFormat realFormat,
                     java.text.NumberFormat imaginaryFormat)
Create an instance with a custom number format for the real part and a custom number format for the imaginary part.

Parameters:
realFormat - the custom format for the real part.
imaginaryFormat - the custom format for the imaginary part.

ComplexFormat

public ComplexFormat(java.lang.String imaginaryCharacter)
Create an instance with a custom imaginary character, and the default number format for both real and imaginary parts.

Parameters:
imaginaryCharacter - The custom imaginary character.

ComplexFormat

public ComplexFormat(java.lang.String imaginaryCharacter,
                     java.text.NumberFormat format)
Create an instance with a custom imaginary character, and a custom number format for both real and imaginary parts.

Parameters:
imaginaryCharacter - The custom imaginary character.
format - the custom format for both real and imaginary parts.

ComplexFormat

public ComplexFormat(java.lang.String imaginaryCharacter,
                     java.text.NumberFormat realFormat,
                     java.text.NumberFormat imaginaryFormat)
Create an instance with a custom imaginary character, a custom number format for the real part, and a custom number format for the imaginary part.

Parameters:
imaginaryCharacter - The custom imaginary character.
realFormat - the custom format for the real part.
imaginaryFormat - the custom format for the imaginary part.
Method Detail

formatComplex

public static java.lang.String formatComplex(Complex c)
This static method calls formatComplex() on a default instance of ComplexFormat.

Parameters:
c - Complex object to format
Returns:
A formatted number in the form "Re(c) + Im(c)i"

format

public java.lang.StringBuffer format(Complex complex,
                                     java.lang.StringBuffer toAppendTo,
                                     java.text.FieldPosition pos)
Formats a Complex object to produce a string.

Parameters:
complex - 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 java.lang.StringBuffer format(java.lang.Object obj,
                                     java.lang.StringBuffer toAppendTo,
                                     java.text.FieldPosition pos)
Formats a object to produce a string. obj must be either a Complex object or a Number object. Any other type of object will result in an IllegalArgumentException being thrown.

Specified by:
format in class java.text.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:
java.lang.IllegalArgumentException - is obj is not a valid type.
See Also:
Format.format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition)

formatDouble

private java.lang.StringBuffer formatDouble(double value,
                                            java.text.NumberFormat format,
                                            java.lang.StringBuffer toAppendTo,
                                            java.text.FieldPosition pos)
Formats a double value to produce a string. In general, the value is formatted using the formatting rules of format. There are three exceptions to this:
  1. NaN is formatted as '(NaN)'
  2. Positive infinity is formatted as '(Infinity)'
  3. Negative infinity is formatted as '(-Infinity)'

Parameters:
value - the double to format.
format - the format used.
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.

getAvailableLocales

public static java.util.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.

getDefaultNumberFormat

private static java.text.NumberFormat getDefaultNumberFormat()
Create a default number format. The default number format is based on NumberFormat.getInstance() with the only customizing is the maximum number of fraction digits, which is set to 2.

Returns:
the default number format.

getDefaultNumberFormat

private static java.text.NumberFormat getDefaultNumberFormat(java.util.Locale locale)
Create a default number format. The default number format is based on NumberFormat.getInstance(java.util.Locale) with the only customizing is the maximum number of fraction digits, which is set to 2.

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

getImaginaryCharacter

public java.lang.String getImaginaryCharacter()
Access the imaginaryCharacter.

Returns:
the imaginaryCharacter.

getImaginaryFormat

public java.text.NumberFormat getImaginaryFormat()
Access the imaginaryFormat.

Returns:
the imaginaryFormat.

getInstance

public static ComplexFormat getInstance()
Returns the default complex format for the current locale.

Returns:
the default complex format.

getInstance

public static ComplexFormat getInstance(java.util.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.

getRealFormat

public java.text.NumberFormat getRealFormat()
Access the realFormat.

Returns:
the realFormat.

parse

public Complex parse(java.lang.String source)
              throws java.text.ParseException
Parses a string to produce a Complex object.

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

parse

public Complex parse(java.lang.String source,
                     java.text.ParsePosition pos)
Parses a string to produce a Complex object.

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

parseAndIgnoreWhitespace

private void parseAndIgnoreWhitespace(java.lang.String source,
                                      java.text.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

private char parseNextCharacter(java.lang.String source,
                                java.text.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.

parseNumber

private java.lang.Number parseNumber(java.lang.String source,
                                     double value,
                                     java.text.ParsePosition pos)
Parses source for a special double values. These values include Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY.

Parameters:
source - the string to parse
value - the special value to parse.
pos - input/ouput parsing parameter.
Returns:
the special number.

parseNumber

private java.lang.Number parseNumber(java.lang.String source,
                                     java.text.NumberFormat format,
                                     java.text.ParsePosition pos)
Parses source for a number. This method can parse normal, numeric values as well as special values. These special values include Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY.

Parameters:
source - the string to parse
format - the number format used to parse normal, numeric values.
pos - input/ouput parsing parameter.
Returns:
the parsed number.

parseObject

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

Specified by:
parseObject in class java.text.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)

setImaginaryCharacter

public void setImaginaryCharacter(java.lang.String imaginaryCharacter)
Modify the imaginaryCharacter.

Parameters:
imaginaryCharacter - The new imaginaryCharacter value.
Throws:
java.lang.IllegalArgumentException - if imaginaryCharacter is null or an empty string.

setImaginaryFormat

public void setImaginaryFormat(java.text.NumberFormat imaginaryFormat)
Modify the imaginaryFormat.

Parameters:
imaginaryFormat - The new imaginaryFormat value.
Throws:
java.lang.IllegalArgumentException - if imaginaryFormat is null.

setRealFormat

public void setRealFormat(java.text.NumberFormat realFormat)
Modify the realFormat.

Parameters:
realFormat - The new realFormat value.
Throws:
java.lang.IllegalArgumentException - if realFormat is null.