org.geotoolkit.measure
Class RangeFormat

Object
  extended by Format
      extended by RangeFormat
All Implemented Interfaces:
Serializable, Cloneable

public class RangeFormat
extends Format

Parses and formats ranges of the given type. The kind of ranges created by the parse method is determined by the class of range components:

Since:
3.06
Version:
3.16
Author:
Martin Desruisseaux (Geomatys)
See Also:
Range, DateRange, NumberRange, MeasurementRange, Serialized Form
Module:
utility/geotk-utility (download)    View source code for this class

Nested Class Summary
 
Nested classes/interfaces inherited from class Format
Format.Field
 
Field Summary
protected  Class<?> elementClass
          The type of the range components.
protected  Format elementFormat
          The format to use for parsing and formatting the range components.
static int MAX_VALUE_FIELD
          The constant value for FieldPosition which designate the maximal value.
static int MIN_VALUE_FIELD
          The constant value for FieldPosition which designate the minimal value.
static int UNIT_FIELD
          The constant value for FieldPosition which designate the units of measurement.
protected  UnitFormat unitFormat
          The format for units of measurement, or null if none.
 
Constructor Summary
RangeFormat()
          Creates a new format for parsing and formatting number ranges using the default locale.
RangeFormat(Locale locale)
          Creates a new format for parsing and formatting number ranges using the given locale.
RangeFormat(Locale locale, Class<?> elementClass)
          Creates a new format for parsing and formatting ranges of the given element class using the given locale.
RangeFormat(Locale locale, TimeZone timezone)
          Creates a new format for parsing and formatting date ranges using the given locale and timezone.
 
Method Summary
 StringBuffer format(Object range, StringBuffer toAppendTo, FieldPosition pos)
          Formats a Range and appends the resulting text to a given string buffer.
 String getElementPattern(boolean localized)
          Returns the pattern used by elementFormat for formatting the minimum and maximum values.
static RangeFormat getInstance()
          Constructs a new RangeFormat for the default locale.
static RangeFormat getInstance(Locale locale)
          Constructs a new RangeFormat for the specified locale.
 RangeSymbols getSymbols()
          Returns the symbols used for parsing and formatting ranges.
 Range<?> parse(String source)
          Parses text from the given string to produce a range.
 Range<?> parse(String source, ParsePosition pos)
          Parses text from a string to produce a range.
 Object parseObject(String source)
          Parses text from a string to produce a range.
 Object parseObject(String source, ParsePosition pos)
          Parses text from a string to produce a range.
 void setElementPattern(String pattern, boolean localized)
          Sets the pattern to be used by elementFormat for formatting the minimum and maximum values.
 void setSymbols(RangeSymbols symbols)
          Sets the symbols to use for parsing and formatting ranges.
 
Methods inherited from class Format
clone, format, formatToCharacterIterator
 
Methods inherited from class Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MIN_VALUE_FIELD

public static final int MIN_VALUE_FIELD
The constant value for FieldPosition which designate the minimal value. This constant can be combined with one of the *_FIELD constants defined in NumberFormat or DateFormat classes for fetching the position of a formatted field. For example in order to get the position where the fraction digits of the minimal value begin, use:
FieldPosition pos = new FieldPosition(NumberFormat.FRACTION_FIELD | RangeFormat.MIN_VALUE_FIELD);
rangeFormat.format(range, buffer, pos);
int beginIndex = pos.getBeginIndex();

See Also:
Constant Field Values

MAX_VALUE_FIELD

public static final int MAX_VALUE_FIELD
The constant value for FieldPosition which designate the maximal value. This constant can be combined with one of the *_FIELD constants defined in NumberFormat or DateFormat classes for fetching the position of a formatted field. For example in order to get the position where the fraction digits of the maximal value begin, use:
FieldPosition pos = new FieldPosition(NumberFormat.FRACTION_FIELD | RangeFormat.MAX_VALUE_FIELD);
rangeFormat.format(range, buffer, pos);
int beginIndex = pos.getBeginIndex();

See Also:
Constant Field Values

UNIT_FIELD

public static final int UNIT_FIELD
The constant value for FieldPosition which designate the units of measurement. This field can not be combined with other field masks.

See Also:
Constant Field Values

elementClass

protected final Class<?> elementClass
The type of the range components. Valid types are Number, Angle, Date or a subclass of those types. This value determines the kind of range to be created by the parse method:


elementFormat

protected final Format elementFormat
The format to use for parsing and formatting the range components. The format is determined from the element class:


unitFormat

protected final UnitFormat unitFormat
The format for units of measurement, or null if none. This is non-null if and only if elementClass is assignable to Number but not to Angle.

Constructor Detail

RangeFormat

public RangeFormat()
Creates a new format for parsing and formatting number ranges using the default locale.


RangeFormat

public RangeFormat(Locale locale)
Creates a new format for parsing and formatting number ranges using the given locale.

Parameters:
locale - The locale for parsing and formatting range components.

RangeFormat

public RangeFormat(Locale locale,
                   TimeZone timezone)
Creates a new format for parsing and formatting date ranges using the given locale and timezone.

Parameters:
locale - The locale for parsing and formatting range components.
timezone - The timezone for the date to be formatted.

RangeFormat

public RangeFormat(Locale locale,
                   Class<?> elementClass)
            throws IllegalArgumentException
Creates a new format for parsing and formatting ranges of the given element class using the given locale. The element class is typically Date.class or some subclass of Number.class.

Parameters:
locale - The locale for parsing and formatting range components.
elementClass - The type of range components.
Throws:
IllegalArgumentException - If the given type is not recognized by this constructor.
Method Detail

getInstance

public static RangeFormat getInstance()
Constructs a new RangeFormat for the default locale.

Returns:
A range format in the default locale.
Since:
3.09

getInstance

public static RangeFormat getInstance(Locale locale)
Constructs a new RangeFormat for the specified locale.

Parameters:
locale - The locale.
Returns:
A range format in the given locale.
Since:
3.09

getSymbols

public RangeSymbols getSymbols()
Returns the symbols used for parsing and formatting ranges.

Returns:
The symbols used by this format.

setSymbols

public void setSymbols(RangeSymbols symbols)
Sets the symbols to use for parsing and formatting ranges.

Parameters:
symbols - The new symbols to use for this format.

getElementPattern

public String getElementPattern(boolean localized)
Returns the pattern used by elementFormat for formatting the minimum and maximum values. If the element format does not use pattern, returns null.

Parameters:
localized - true for returning the localized pattern, or false for the unlocalized one.
Returns:
The pattern, or null if the elementFormat doesn't use pattern.
Since:
3.11

setElementPattern

public void setElementPattern(String pattern,
                              boolean localized)
Sets the pattern to be used by elementFormat for formatting the minimum and maximum values.

Parameters:
pattern - The new pattern.
localized - true if the given pattern is localized.
Throws:
IllegalStateException - If the elementFormat does not use pattern.
Since:
3.11

format

public StringBuffer format(Object range,
                           StringBuffer toAppendTo,
                           FieldPosition pos)
Formats a Range and appends the resulting text to a given string buffer. The default implementation formats the range using the same rules than Range.toString(), except that the values (numbers, angles or dates) are formatted using the Format object appropriate for the locale given at construction time.

Specified by:
format in class Format
Parameters:
range - The Range object to format.
toAppendTo - Where the text is to be appended.
pos - Identifies a field in the formatted text.
Returns:
The string buffer passed in as toAppendTo, with formatted text appended.
Throws:
IllegalArgumentException - If this formatter can not format the given object.

parseObject

public Object parseObject(String source)
                   throws ParseException
Parses text from a string to produce a range. The default implementation delegates to parse(String) with no additional work.

Overrides:
parseObject in class Format
Parameters:
source - The text, part of which should be parsed.
Returns:
A range parsed from the string, or null in case of error.
Throws:
ParseException - If the given string can not be fully parsed.

parseObject

public Object parseObject(String source,
                          ParsePosition pos)
Parses text from a string to produce a range. The default implementation delegates to parse(String, ParsePosition) with no additional work.

Specified by:
parseObject in class Format
Parameters:
source - The text, part of which should be parsed.
pos - Index and error index information as described above.
Returns:
A range parsed from the string, or null in case of error.

parse

public Range<?> parse(String source)
               throws ParseException
Parses text from the given string to produce a range. This method use the full string. If there is some unparsed characters after the parsed range, then this method thrown an exception.

Parameters:
source - The text to parse.
Returns:
The parsed range (never null).
Throws:
ParseException - If the given string can not be fully parsed.

parse

public Range<?> parse(String source,
                      ParsePosition pos)
Parses text from a string to produce a range. The method attempts to parse text starting at the index given by pos. If parsing succeeds, then the index of pos is updated to the index after the last character used, and the parsed range is returned. If an error occurs, then the index of pos is not changed, the error index of pos is set to the index of the character where the error occurred, and null is returned.

Parameters:
source - The text, part of which should be parsed.
pos - Index and error index information as described above.
Returns:
A range parsed from the string, or null in case of error.


Copyright © 2009-2012 Geotoolkit.org. All Rights Reserved.