Package org.apache.sis.measure
Class RangeFormat
-
- All Implemented Interfaces:
Serializable
,Cloneable
,Localized
public class RangeFormat extends Format implements Localized
Parses and formatsRange
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:- If the range is empty, then the range is represented by "
{}
". - Otherwise if the minimal value is equals to the
maximal value, then that single value is formatted
inside braces as in "
{value}
". - Otherwise the minimal and maximal values are formatted inside bracket or parenthesis,
depending on whether each endpoint is inclusive or exclusive:
- "
[min … max]
" if both endpoints are inclusive (closed interval); - "
(min … max)
" if both endpoints are exclusive (open interval); - or a mix of both styles if an endpoint is inclusive while the other is exclusive.
∞
" symbol is used in place ofmin
ormax
for unbounded ranges. - "
MeasurementRange
, then the unit of measurement is appended except for empty ranges.Lenient parsingAt parsing time, the above formatting rules are relaxed as below:- Empty ranges can be represented by "
[]
"or "()
" in addition to the standard "{}
". - The braces are optional for singleton values, i.e. "
value
" is accepted as well as "{value}
".
Range type and type of range elementsThe kind of ranges created by theparse(…)
methods is determined by the type of range elements:- If the elements type is assignable to
Date
, then theparse(…)
methods will createRange<Date>
objects. - If the elements type is assignable to
Number
, then:- If the text to parse contains a unit of measurement, then
the
parse(…)
methods will createMeasurementRange
objects. - Otherwise the
parse(…)
methods will createNumberRange
objects.
- If the text to parse contains a unit of measurement, then
the
- Since:
- 0.3
- See Also:
Range.toString()
, Wikipedia: ISO 31-11, Serialized Form
Defined in the
sis-utility
module
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
RangeFormat.Field
Constants that are used as attribute keys in the iterator returned fromformatToCharacterIterator(Object)
.
-
Field Summary
Fields Modifier and Type Field 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, ornull
if none.
-
Constructor Summary
Constructors Constructor 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 formattingRange<Date>
using the given locale and timezone.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description RangeFormat
clone()
Returns a clone of this range format.StringBuffer
format(Object range, StringBuffer toAppendTo, FieldPosition pos)
Formats aRange
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 byelementFormat
for formatting the minimum and maximum values.Locale
getLocale()
Returns this formatter locale.boolean
isAlternateForm()
Returnstrue
if thisRangeFormat
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 thisRangeFormat
shall use the alternate form at formatting time.void
setElementPattern(String pattern, boolean localized)
Sets the pattern to be used byelementFormat
for formatting the minimum and maximum values.
-
-
-
Field Detail
-
elementType
protected final Class<?> elementType
The type of the range components. Valid types areNumber
,Angle
,Date
or a subclass of those types. This value determines the kind of range to be created by the parse method:- See Also:
Range.getElementType()
-
elementFormat
protected final Format elementFormat
The format to use for parsing and formatting the range components. The format is determined from the element type:AngleFormat
if the element type is assignable toAngle
.NumberFormat
if the element type is assignable toNumber
.DateFormat
if the element type is assignable toDate
.
-
unitFormat
protected final UnitFormat unitFormat
The format for unit of measurement, ornull
if none. This is non-null if and only ifelementType
is assignable toNumber
but not toAngle
.
-
-
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 formattingRange<Date>
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<?> elementType) throws IllegalArgumentException
Creates a new format for parsing and formatting ranges of the given element type using the given locale. The element type is typicallyDate.class
or some subclass ofNumber.class
.- Parameters:
locale
- the locale for parsing and formatting range components.elementType
- the type of range components.- Throws:
IllegalArgumentException
- if the given type is not recognized by this constructor.
-
-
Method Detail
-
getLocale
public Locale getLocale()
Returns this formatter locale. This is the locale specified at construction time if any, or the default locale at construction time otherwise.- Specified by:
getLocale
in interfaceLocalized
- Returns:
- this formatter locale (never
null
). - Since:
- 1.0
-
getElementPattern
public String getElementPattern(boolean localized)
Returns the pattern used byelementFormat
for formatting the minimum and maximum values. If the element format does not use pattern, returnsnull
.- Parameters:
localized
-true
for returning the localized pattern, orfalse
for the unlocalized one.- Returns:
- the pattern, or
null
if theelementFormat
doesn't use pattern. - See Also:
DecimalFormat.toPattern()
,SimpleDateFormat.toPattern()
,AngleFormat.toPattern()
-
setElementPattern
public void setElementPattern(String pattern, boolean localized)
Sets the pattern to be used byelementFormat
for formatting the minimum and maximum values.- Parameters:
pattern
- the new pattern.localized
-true
if the given pattern is localized.- Throws:
IllegalStateException
- if theelementFormat
does not use pattern.- See Also:
DecimalFormat.applyPattern(String)
,SimpleDateFormat.applyPattern(String)
,AngleFormat.applyPattern(String)
-
isAlternateForm
public boolean isAlternateForm()
Returnstrue
if thisRangeFormat
shall use the alternate form at formatting time. The alternate form expresses open intervals like]a…b[
instead of(a…b)
.This flag has no effect on parsing, since the parser accepts both forms.
- Returns:
true
for using the alternate format instead of the default format.
-
setAlternateForm
public void setAlternateForm(boolean alternateForm)
Sets whether thisRangeFormat
shall use the alternate form at formatting time. The alternate form expresses open intervals like]a…b[
instead of(a…b)
.- Parameters:
alternateForm
-true
for using the alternate format, orfalse
for using the default format.
-
format
public StringBuffer format(Object range, StringBuffer toAppendTo, FieldPosition pos)
Formats aRange
and appends the resulting text to a given string buffer. See the class javadoc for a description of the format.- Specified by:
format
in classFormat
- Parameters:
range
- theRange
object to format.toAppendTo
- where the text is to be appended.pos
- identifies a field in the formatted text, ornull
if none.- Returns:
- the string buffer passed in as
toAppendTo
, with formatted text appended. - Throws:
IllegalArgumentException
- if this formatter can not format the given object.
-
formatToCharacterIterator
public AttributedCharacterIterator formatToCharacterIterator(Object range)
Formats a range as an attributed character iterator. Callers can iterate and queries the attribute values as in the following example:
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
orAngleFormat.Field
attributes in addition to theRangeFormat.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.- Overrides:
formatToCharacterIterator
in classFormat
- Parameters:
range
- theRange
object to format.- Returns:
- a character iterator together with the attributes describing the formatted value.
- Throws:
IllegalArgumentException
- ifvalue
if not an instance ofRange
.
-
parseObject
public Object parseObject(String source) throws ParseException
Parses text from a string to produce a range. The default implementation delegates toparse(String)
with no additional work.- Overrides:
parseObject
in classFormat
- Parameters:
source
- the text, part of which should be parsed.- Returns:
- a range parsed from the string.
- 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 toparse(String, ParsePosition)
with no additional work.- Specified by:
parseObject
in classFormat
- 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 bypos
. If parsing succeeds, then the index ofpos
is updated to the index after the last character used, and the parsed range is returned. If an error occurs, then the index ofpos
is not changed, the error index ofpos
is set to the index of the character where the error occurred, andnull
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.
-
clone
public RangeFormat clone()
Returns a clone of this range format.
-
-