public class AngleFormat extends Format implements Localized
Symbol | Meaning |
---|---|
D | The integer part of degrees |
d | The fractional part of degrees |
M | The integer part of minutes |
m | The fractional part of minutes |
S | The integer part of seconds |
s | The fractional part of seconds |
# | Fraction digits shown only if non-zero |
. | The decimal separator |
? | Omit the preceding field if zero |
D
, M
and S
stand for the integer parts of degrees,
minutes and seconds respectively. If present, they shall appear in that order.
M′D
" is illegal because "M
" and "S
" are in reverse order.
"D°S
" is also illegal because "M
" is missing between "D
" and "S
".d
, m
and s
stand for fractional parts of degrees, minutes and
seconds respectively. Only one of those can appear in a pattern. If present, they must be in the last field.
D.dd°MM′
" is illegal because "d
" is followed by "M
".
"D.mm
" is also illegal because "m
" is not the fractional part of "D
".D
, M
, S
and their lower-case counterpart is the number
of digits to format.
DD.ddd
" will format angles with two digits for the integer part and three digits
for the fractional part (e.g. 4.4578
will be formatted as "04.458"
).°
, ′
and ″
are inserted "as-is" in the formatted string,
except the decimal separator dot (.
) which is replaced by the local-dependent decimal separator.
Separator characters may be completely omitted; AngleFormat
will still differentiate degrees,
minutes and seconds fields according the pattern.
0480439
" with the "DDDMMmm
" pattern will be parsed as 48°04.39′.?
modifier specifies that the preceding field can be omitted if its value is zero.
Any field can be omitted for Angle
object, but only trailing fields are omitted for
Longitude
and Latitude
.
DD°MM′?SS″?
" will format an angle of 12.01° as 12°36″
,
but a longitude of 12.01°N as 12°00′36″N
(not 12°36″N
).Pattern | 48.5 | -12.53125 |
---|---|---|
DD°MM′SS.#″ | 48°30′00″ | -12°31′52.5″ |
DD°MM′ | 48°30′ | -12°32′ |
DD.ddd | 48.500 | -12.531 |
DD.### | 48.5 | -12.531 |
DDMM | 4830 | -1232 |
DDMMSSs | 4830000 | -1231525 |
DD°MM′?SS.s″? | 48°30′ | -12°31′52.5″ |
Angle
,
Latitude
,
Longitude
,
Serialized FormDefined in the sis-utility
module
Modifier and Type | Class and Description |
---|---|
static class |
AngleFormat.Field
Constants that are used as attribute keys in the iterator returned from
formatToCharacterIterator(Object) . |
Constructor and Description |
---|
AngleFormat()
Constructs a new
AngleFormat for the default pattern and the current default locale. |
AngleFormat(Locale locale)
Constructs a new
AngleFormat for the default pattern and the specified locale. |
AngleFormat(String pattern)
Constructs a new
AngleFormat for the specified pattern and the current default locale. |
AngleFormat(String pattern,
Locale locale)
Constructs a new
AngleFormat using the specified pattern and locale. |
Modifier and Type | Method and Description |
---|---|
void |
applyPattern(String pattern)
Sets the pattern to use for parsing and formatting angles.
|
AngleFormat |
clone()
Returns a clone of this
AngleFormat . |
boolean |
equals(Object object)
Compares this format with the specified object for equality.
|
String |
format(double angle)
Formats an angle.
|
StringBuffer |
format(double angle,
StringBuffer toAppendTo,
FieldPosition pos)
Formats an angle in the given buffer.
|
StringBuffer |
format(Object value,
StringBuffer toAppendTo,
FieldPosition pos)
Formats an angle, latitude or longitude value in the given buffer.
|
AttributedCharacterIterator |
formatToCharacterIterator(Object value)
Formats an angle, latitude or longitude value as an attributed character iterator.
|
static AngleFormat |
getInstance()
Constructs a new
AngleFormat for the default pattern and the current default locale. |
static AngleFormat |
getInstance(Locale locale)
Constructs a new
AngleFormat for the default pattern and the specified locale. |
Locale |
getLocale()
Returns this formatter locale.
|
int |
getMaximumFractionDigits()
Returns the maximum number of digits allowed in the fraction portion of the last field.
|
int |
getMinimumFractionDigits()
Returns the minimum number of digits allowed in the fraction portion of the last field.
|
int |
hashCode()
Returns a "hash value" for this object.
|
boolean |
isFallbackAllowed()
Returns
true if the parse methods are allowed
to fallback on the build-in default symbols if the string to parse doesn't match the
applied pattern. |
Angle |
parse(String source)
Parses the given string as an angle.
|
Angle |
parse(String source,
ParsePosition pos)
Parses the given string as an angle.
|
Object |
parseObject(String source)
Parses the given string as an object.
|
Object |
parseObject(String source,
ParsePosition pos)
Parses a substring as an object.
|
void |
setFallbackAllowed(boolean allowed)
Sets whether the
parse methods are allowed to
fallback on the build-in default symbols if the string to parse doesn't match the
applied pattern. |
void |
setMaximumFractionDigits(int count)
Sets the maximum number of digits allowed in the fraction portion of the last field.
|
void |
setMaximumWidth(int width)
Modifies, if needed, the pattern in order to fit formatted angles in the given maximum
total width.
|
void |
setMinimumFractionDigits(int count)
Sets the minimum number of digits allowed in the fraction portion of the last field.
|
String |
toPattern()
Returns the pattern used for parsing and formatting angles.
|
String |
toString()
Returns a string representation of this object for debugging purpose.
|
public AngleFormat()
AngleFormat
for the default pattern and the current default locale.public AngleFormat(Locale locale)
AngleFormat
for the default pattern and the specified locale.locale
- The locale to use.public AngleFormat(String pattern) throws IllegalArgumentException
AngleFormat
for the specified pattern and the current default locale.pattern
- Pattern to use for parsing and formatting angles.
See class description for an explanation of pattern syntax.IllegalArgumentException
- If the specified pattern is illegal.public AngleFormat(String pattern, Locale locale) throws IllegalArgumentException
AngleFormat
using the specified pattern and locale.pattern
- Pattern to use for parsing and formatting angles.
See class description for an explanation of pattern syntax.locale
- Locale to use.IllegalArgumentException
- If the specified pattern is illegal.public static AngleFormat getInstance()
AngleFormat
for the default pattern and the current default locale.public static AngleFormat getInstance(Locale locale)
AngleFormat
for the default pattern and the specified locale.locale
- The locale to use.public void applyPattern(String pattern) throws IllegalArgumentException
pattern
- Pattern to use for parsing and formatting angle.IllegalArgumentException
- If the specified pattern is not legal.setMinimumFractionDigits(int)
,
setMaximumFractionDigits(int)
public String toPattern()
getMinimumFractionDigits()
,
getMaximumFractionDigits()
public int getMinimumFractionDigits()
'd'
, 'm'
or 's'
symbol
in the pattern.DecimalFormat.getMinimumFractionDigits()
public void setMinimumFractionDigits(int count)
count
- The minimum number of digits allowed in the fraction portion.DecimalFormat.setMinimumFractionDigits(int)
public int getMaximumFractionDigits()
'#'
symbol in the pattern.DecimalFormat.getMaximumFractionDigits()
public void setMaximumFractionDigits(int count)
count
- The maximum number of digits allowed in the fraction portion.DecimalFormat.setMaximumFractionDigits(int)
public void setMaximumWidth(int width)
This method does not take into account the space needed for the hemisphere symbol when
formatting Latitude
or Longitude
objects.
width
- The maximum total width of formatted angle.public final String format(double angle)
applyPattern(String)
.angle
- Angle to format, in decimal degrees.public StringBuffer format(double angle, StringBuffer toAppendTo, FieldPosition pos)
applyPattern(String)
.angle
- Angle to format, in decimal degrees.toAppendTo
- The buffer where to append the formatted angle.pos
- An optional object where to store the position of the field in the formatted
text, or null
if this information is not wanted. This field position
shall be created with one of the AngleFormat.Field
constants.toAppendTo
buffer, returned for method calls chaining.public StringBuffer format(Object value, StringBuffer toAppendTo, FieldPosition pos) throws IllegalArgumentException
applyPattern(String)
, with some variations that
depend on the value
class:
value
is a Latitude
instance, then the value is formatted as a
positive angle followed by the "N" (positive value) or "S" (negative value) symbol.value
is a Longitude
instance, then the value is formatted as a
positive angle followed by the "E" (positive value) or "W" (negative value) symbol.value
is any Angle
other than a Latitude
or Longitude
,
then it is formatted as by the format(double, StringBuffer, FieldPosition)
method.format
in class Format
value
- Angle
object to format.toAppendTo
- The buffer where to append the formatted angle.pos
- An optional object where to store the position of the field in the formatted
text, or null
if this information is not wanted. This field position
shall be created with one of the AngleFormat.Field
constants.toAppendTo
buffer, returned for method calls chaining.IllegalArgumentException
- if value
if not an instance of Angle
.public AttributedCharacterIterator formatToCharacterIterator(Object value)
Alternatively, if the current iterator index is before the start of the minutes field, then the starting position of that field can be obtained directly byAttributedCharacterIterator it = angleFormat.formatToCharacterIterator(myAngle); for (char c=it.first(); c!=AttributedCharacterIterator.DONE; c=c.next()) { // 'c' is a character from the formatted string. if (it.getAttribute(AngleFormat.Field.MINUTES) != null) { // If we enter this block, then the character 'c' is part of the minutes field, // This field extends from it.getRunStart(MINUTES) to it.getRunLimit(MINUTES). } }
it.getRunLimit(MINUTES)
. If the current iterator index
is inside the minutes 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
attributes in addition to the AngleFormat.Field
ones. Consequently the same character may
have more than one attribute. For example when formatting 45°30′15.0″N, then:
45°
part has the AngleFormat.Field.DEGREES
attribute.30′
part has the AngleFormat.Field.MINUTES
attribute.15.0″
part has the AngleFormat.Field.SECONDS
attribute.N
part has the AngleFormat.Field.HEMISPHERE
attribute.45
, 30
and 15
parts have the
NumberFormat.Field.INTEGER
attribute..
part has the NumberFormat.Field.DECIMAL_SEPARATOR
attribute.0
part has the NumberFormat.Field.FRACTION
attribute.CharSequence
interface for convenience.formatToCharacterIterator
in class Format
value
- Angle
object to format.IllegalArgumentException
- if value
if not an instance of Angle
.public Angle parse(String source, ParsePosition pos)
isFallbackAllowed()
is true
, then this method will parse "48°12.34'
" correctly even if the
expected pattern was "DDMM.mm
" (i.e. the string should have been "4812.34
").
If the given string ends with a "N" or "S" hemisphere symbol, then this method returns
an instance of Latitude
. Otherwise if the string ends with a "E" or "W" symbol,
then this method returns an instance of Longitude
. Otherwise this method returns
an instance of Angle
.
This method is stricter than the parse(String)
method regarding whitespaces
between the degrees, minutes and seconds fields. This is because whitespaces could be
used as a separator for other kinds of values. If the string is known to contain only
an angle value, use parse(String)
instead.
source
- The string to parse.pos
- On input, index of the first source
character to read.
On output, index after the last parsed character.Angle
, Latitude
or Longitude
object.isFallbackAllowed()
public Angle parse(String source) throws ParseException
parse(String)
to be more tolerant than
parse(String, ParsePosition)
regarding white spaces between degrees, minutes
and seconds fields.source
- The string to parse.Angle
, Latitude
or Longitude
object.ParseException
- If the string can not be fully parsed.isFallbackAllowed()
public Object parseObject(String source, ParsePosition pos)
parse(String, ParsePosition)
.parseObject
in class Format
source
- The string to parse.pos
- The position where to start parsing.Angle
, Latitude
or Longitude
object.public Object parseObject(String source) throws ParseException
parse(String)
.parseObject
in class Format
source
- The string to parse.Angle
, Latitude
or Longitude
object.ParseException
- If the string can not been fully parsed.public boolean isFallbackAllowed()
true
if the parse
methods are allowed
to fallback on the build-in default symbols if the string to parse doesn't match the
applied pattern.true
if the ASCII quote characters are allowed at parsing time.public void setFallbackAllowed(boolean allowed)
parse
methods are allowed to
fallback on the build-in default symbols if the string to parse doesn't match the
applied pattern. The build-in fallback is:
°
(an extended-ASCII character) or space (in parse(String)
method only) for degrees.'
(an ASCII character) or ′
(the default Unicode character) for minutes."
(an ASCII character) or ″
(the default Unicode character) for seconds.true
, because many end-users will not enter the Unicode
′
and ″
symbols. However developers may need to set this flag to
false
if those ASCII symbols are used in a wider context (for example the
"
character for quoting strings).allowed
- true
if the ASCII quote characters are allowed at parsing time.public Locale getLocale()
getLocale
in interface Localized
null
).CompoundFormat.getLocale()
public AngleFormat clone()
AngleFormat
.public int hashCode()
public boolean equals(Object object)
Copyright © 2010–2014 The Apache Software Foundation. All rights reserved.