public class RangeFormat extends Format
Range
instances according the given locale.
This class complies to the format described in the ISO 31-11
standard, except that the minimal and maximal values are separated by the "…
" character
instead than coma. More specifically, the format is defined as below:
{}
".{value}
".[min … max]
" if both endpoints are inclusive (closed interval);(min … max)
" if both endpoints are exclusive (open interval);∞
" symbol is used in place of min
or max
for unbounded ranges.MeasurementRange
, then the
unit of measurement is appended except for empty ranges.
[]
"or "()
" in addition to the
standard "{}
".value
" is accepted
as well as "{value}
".parse(…)
methods is determined
by the type of range elements:
Date
, then the parse(…)
methods
will create Range<Date>
objects.Number
, then:
parse(…)
methods will create MeasurementRange
objects.parse(…)
methods will create NumberRange
objects.Range.toString()
,
Wikipedia: ISO 31-11,
Serialized FormDefined in the sis-utility module
Modifier and Type | Class and Description |
---|---|
static class |
RangeFormat.Field
Defines constants that are used as attribute keys in the iterator returned from
formatToCharacterIterator(Object) . |
Modifier and Type | Field and Description |
---|---|
protected Format |
elementFormat
The format to use for parsing and formatting the range components.
|
protected Class<?> |
elementType
The type of the range components.
|
protected UnitFormat |
unitFormat
The format for unit of measurement, or
null if none. |
Constructor and Description |
---|
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<?> elementType)
Creates a new format for parsing and formatting ranges of
the given element type using the given locale.
|
RangeFormat(Locale locale,
TimeZone timezone)
Creates a new format for parsing and formatting
Range<Date>
using the given locale and timezone. |
Modifier and Type | Method and Description |
---|---|
StringBuffer |
format(Object range,
StringBuffer toAppendTo,
FieldPosition pos)
Formats a
Range and appends the resulting text to a given string buffer. |
AttributedCharacterIterator |
formatToCharacterIterator(Object range)
Formats a range as an attributed character iterator.
|
String |
getElementPattern(boolean localized)
Returns the pattern used by
elementFormat for formatting the minimum and
maximum values. |
boolean |
isAlternateForm()
Returns
true if this RangeFormat shall use the alternate form at
formatting time. |
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 |
setAlternateForm(boolean alternateForm)
Sets whether this
RangeFormat shall use the alternate form at formatting time. |
void |
setElementPattern(String pattern,
boolean localized)
Sets the pattern to be used by
elementFormat for formatting the minimum and
maximum values. |
protected final Class<?> elementType
Number
, Angle
,
Date
or a subclass of those types. This value determines the kind of range
to be created by the parse method:
Range.getElementType()
protected final Format elementFormat
AngleFormat
if the element type is assignable to Angle
.NumberFormat
if the element type is assignable to Number
.DateFormat
if the element type is assignable to Date
.protected final UnitFormat unitFormat
null
if none. This is non-null if and
only if elementType
is assignable to Number
but not to Angle
.public RangeFormat()
public RangeFormat(Locale locale)
locale
- The locale for parsing and formatting range components.public RangeFormat(Locale locale, TimeZone timezone)
Range<Date>
using the given locale and timezone.locale
- The locale for parsing and formatting range components.timezone
- The timezone for the date to be formatted.public RangeFormat(Locale locale, Class<?> elementType) throws IllegalArgumentException
Date.class
or some subclass of Number.class
.locale
- The locale for parsing and formatting range components.elementType
- The type of range components.IllegalArgumentException
- If the given type is not recognized by this constructor.public String getElementPattern(boolean localized)
elementFormat
for formatting the minimum and
maximum values. If the element format does not use pattern, returns null
.localized
- true
for returning the localized pattern, or false
for the unlocalized one.null
if the elementFormat
doesn't use pattern.DecimalFormat.toPattern()
,
SimpleDateFormat.toPattern()
,
AngleFormat.toPattern()
public void setElementPattern(String pattern, boolean localized)
elementFormat
for formatting the minimum and
maximum values.pattern
- The new pattern.localized
- true
if the given pattern is localized.IllegalStateException
- If the elementFormat
does not use pattern.DecimalFormat.applyPattern(String)
,
SimpleDateFormat.applyPattern(String)
,
AngleFormat.applyPattern(String)
public boolean isAlternateForm()
true
if this RangeFormat
shall use the alternate form at
formatting time. The alternate form expresses open intervals like ]a…b[
instead of (a…b)
.
This flag as no effect on parsing, since the parser accepts both forms.
true
for using the alternate format instead of the default format.public void setAlternateForm(boolean alternateForm)
RangeFormat
shall use the alternate form at formatting time.alternateForm
- true
for using the alternate format, or false
for
using the default format.public StringBuffer format(Object range, StringBuffer toAppendTo, FieldPosition pos)
Range
and appends the resulting text to a given string buffer.
See the class javadoc for a description of the format.format
in class Format
range
- The Range
object to format.toAppendTo
- Where the text is to be appended.pos
- Identifies a field in the formatted text, or null
if none.toAppendTo
, with formatted text appended.IllegalArgumentException
- If this formatter can not format the given object.public AttributedCharacterIterator formatToCharacterIterator(Object range)
Alternatively, if the current iterator index is before the start of the minimum value field, then the starting position of that field can be obtained directly byAttributedCharacterIterator it = rangeFormat.formatToCharacterIterator(myRange); for (char c=it.first(); c!=AttributedCharacterIterator.DONE; c=c.next()) { // 'c' is a character from the formatted string. if (it.getAttribute(RangeFormat.Field.MIN_VALUE) != null) { // If we enter this block, then the character 'c' is part of the minimal value, // This field extends from it.getRunStart(MIN_VALUE) to it.getRunLimit(MIN_VALUE). } }
it.getRunLimit(MIN_VALUE)
. If the current iterator
index is inside the minimum value field, then the above method call will rather returns the
end of that field. The same strategy works for other all fields too.
The returned character iterator contains all NumberFormat.Field
,
DateFormat.Field
or AngleFormat.Field
attributes in addition to the RangeFormat.Field
ones. Consequently the same character may
have more than one attribute.
In Apache SIS implementation, the returned character iterator also implements the
CharSequence
interface for convenience.
formatToCharacterIterator
in class Format
range
- Range
object to format.IllegalArgumentException
- if value
if not an instance of Range
.public Object parseObject(String source) throws ParseException
parse(String)
with no additional work.parseObject
in class Format
source
- The text, part of which should be parsed.null
in case of error.ParseException
- If the given string can not be fully parsed.public Object parseObject(String source, ParsePosition pos)
parse(String, ParsePosition)
with no additional work.parseObject
in class Format
source
- The text, part of which should be parsed.pos
- Index and error index information as described above.null
in case of error.public Range<?> parse(String source) throws ParseException
source
- The text to parse.null
).ParseException
- If the given string can not be fully parsed.public Range<?> parse(String source, ParsePosition pos)
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.source
- The text, part of which should be parsed.pos
- Index and error index information as described above.null
in case of error.Copyright © 2010–2013 The Apache Software Foundation. All rights reserved.