Class AngleFormat
-
- All Implemented Interfaces:
Serializable,Cloneable
public class AngleFormat extends Format
Parses and formats angles according a specified pattern. The pattern is a string containing any characters, with a special meaning for the following characters:DThe integer part of degrees dThe fractional part of degrees MThe integer part of minutes mThe fractional part of minutes SThe integer part of seconds sThe fractional part of seconds .The decimal separator Upper-case letters
D,MandSare for the integer parts of degrees, minutes and seconds respectively. They must appear in this order (e.g. "M'D" is illegal because "M" and "S" are inverted; "D°S" is illegal too because there is no "M" between "D" and "S"). Lower-case lettersd,mandsare for fractional parts of degrees, minutes and seconds respectively. Only one of those may appears in a pattern, and it must be the last special symbol (e.g. "D.dd°MM'" is illegal because "d" is followed by "M"; "D.mm" is illegal because "m" is not the fractional part of "D").The number of occurrence of
D,M,Sand their lower-case counterpart is the number of digits to format. For example, "DD.ddd" will format angle with two digits for the integer part and three digits for the fractional part (e.g. 4.4578 will be formatted as "04.458"). Separator characters like°,'and"and 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;AngleFormatwill still differentiate degrees, minutes and seconds fields according the pattern. For example, "0480439" with the pattern "DDDMMmm" will be parsed as 48°04.39'.The following table gives some examples of legal patterns.
Pattern Example DD°MM'SS"48°30'00" DD°MM'48°30' DD.ddd48.500 DDMM4830 DDMMSS483000 - Since:
- 2.0
- Author:
- Martin Desruisseaux (PMO, IRD)
- See Also:
Angle,Latitude,Longitude, Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classAngleFormat.RoundingMethodSet the rounding method to use when the last significant digit in a value is 5.-
Nested classes/interfaces inherited from class Format
Format.Field
-
-
Field Summary
Fields Modifier and Type Field Description static AngleFormat.RoundingMethodDEFAULT_ROUNDING_METHODThe default rounding method (AngleFormat.RoundingMethod.ROUND_HALF_EVEN).static intDEGREES_FIELDConstant for degrees field.static intHEMISPHERE_FIELDConstant for hemisphere field.static intMINUTES_FIELDConstant for minutes field.static intSECONDS_FIELDConstant for seconds field.
-
Constructor Summary
Constructors Constructor Description AngleFormat()Constructs a newAngleFormatusing the current default locale and a default pattern.AngleFormat(String pattern)Constructs a newAngleFormatusing the current default locale and the specified pattern.AngleFormat(String pattern, DecimalFormatSymbols symbols)Constructs a newAngleFormatusing the specified pattern and decimal symbols.AngleFormat(String pattern, Locale locale)Constructs a newAngleFormatusing the specified pattern and locale.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidapplyPattern(String pattern)Sets the pattern to use for parsing and formatting angle.booleanequals(Object obj)Compares this format with the specified object for equality.Stringformat(double angle)Format an angle.StringBufferformat(double angle, StringBuffer toAppendTo, FieldPosition pos)Formats an angle and appends the resulting text to a given string buffer.StringBufferformat(Object obj, StringBuffer toAppendTo, FieldPosition pos)Formats an angle, a latitude or a longitude and appends the resulting text to a given string buffer.static AngleFormat.RoundingMethodgetDefaultRoundingMethod()Get the default rounding method.static AngleFormatgetInstance(Locale locale)Constructs a newAngleFormatfor the specified locale.static AngleFormat.RoundingMethodgetRoundingMethod()Get the rounding method being used by this instance.inthashCode()Returns a "hash value" for this object.Angleparse(String source)Parses a string as an angle.Angleparse(String source, ParsePosition pos)Parses a string as an angle.AngleparseObject(String source)Parses a string as an object.AngleparseObject(String source, ParsePosition pos)Parses a substring as an angle.static voidsetDefaultRoundingMethod(AngleFormat.RoundingMethod method)Set the default rounding method for all instances of this class to use when the last significant digit of a value is 5.voidsetRoundingMethod(AngleFormat.RoundingMethod method)Set the rounding method for this instance to use when the last significant digit of a value is 5.StringtoPattern()Returns the pattern used for parsing and formatting angles.StringtoString()Returns a string representation of this object.-
Methods inherited from class Format
clone, format, formatToCharacterIterator
-
-
-
-
Field Detail
-
DEGREES_FIELD
public static final int DEGREES_FIELD
Constant for degrees field. When formatting a string, this value may be specified to theFieldPositionconstructor in order to get the bounding index where degrees have been written.- See Also:
- Constant Field Values
-
MINUTES_FIELD
public static final int MINUTES_FIELD
Constant for minutes field. When formatting a string, this value may be specified to theFieldPositionconstructor in order to get the bounding index where minutes have been written.- See Also:
- Constant Field Values
-
SECONDS_FIELD
public static final int SECONDS_FIELD
Constant for seconds field. When formatting a string, this value may be specified to theFieldPositionconstructor in order to get the bounding index where seconds have been written.- See Also:
- Constant Field Values
-
HEMISPHERE_FIELD
public static final int HEMISPHERE_FIELD
Constant for hemisphere field. When formatting a string, this value may be specified to theFieldPositionconstructor in order to get the bounding index where the hemisphere synbol has been written.- See Also:
- Constant Field Values
-
DEFAULT_ROUNDING_METHOD
public static final AngleFormat.RoundingMethod DEFAULT_ROUNDING_METHOD
The default rounding method (AngleFormat.RoundingMethod.ROUND_HALF_EVEN).
-
-
Constructor Detail
-
AngleFormat
public AngleFormat()
Constructs a newAngleFormatusing the current default locale and a default pattern.
-
AngleFormat
public AngleFormat(String pattern) throws IllegalArgumentException
Constructs a newAngleFormatusing the current default locale and the specified pattern.- Parameters:
pattern- Pattern to use for parsing and formatting angle. See class description for an explanation of how this pattern work.- Throws:
IllegalArgumentException- If the specified pattern is not legal.
-
AngleFormat
public AngleFormat(String pattern, Locale locale) throws IllegalArgumentException
Constructs a newAngleFormatusing the specified pattern and locale.- Parameters:
pattern- Pattern to use for parsing and formatting angle. See class description for an explanation of how this pattern work.locale- Locale to use.- Throws:
IllegalArgumentException- If the specified pattern is not legal.
-
AngleFormat
public AngleFormat(String pattern, DecimalFormatSymbols symbols)
Constructs a newAngleFormatusing the specified pattern and decimal symbols.- Parameters:
pattern- Pattern to use for parsing and formatting angle. See class description for an explanation of how this pattern work.symbols- The symbols to use for parsing and formatting numbers.- Throws:
IllegalArgumentException- If the specified pattern is not legal.
-
-
Method Detail
-
getInstance
public static AngleFormat getInstance(Locale locale)
Constructs a newAngleFormatfor the specified locale.- Parameters:
locale- The locale.- Returns:
- An angle format in the given locale.
-
setDefaultRoundingMethod
public static void setDefaultRoundingMethod(AngleFormat.RoundingMethod method)
Set the default rounding method for all instances of this class to use when the last significant digit of a value is 5.Note: this will not affect the rounding method being used by instances of this class created previously.
- Parameters:
method- one of AngleFormat.RoundingMethod constants- See Also:
setRoundingMethod(org.geotools.measure.AngleFormat.RoundingMethod),DEFAULT_ROUNDING_METHOD
-
getDefaultRoundingMethod
public static AngleFormat.RoundingMethod getDefaultRoundingMethod()
Get the default rounding method.- Returns:
- the default rounding method.
-
setRoundingMethod
public void setRoundingMethod(AngleFormat.RoundingMethod method)
Set the rounding method for this instance to use when the last significant digit of a value is 5. If the rounding method has previously been set globally with the static setDefaultRoundingMethod(org.geotools.measure.AngleFormat.RoundingMethod) method, setting a different method here will take precedence for this instance.- Parameters:
method- one of AngleFormat.RoundingMethod constants- See Also:
setDefaultRoundingMethod(org.geotools.measure.AngleFormat.RoundingMethod),DEFAULT_ROUNDING_METHOD
-
getRoundingMethod
public static AngleFormat.RoundingMethod getRoundingMethod()
Get the rounding method being used by this instance.- Returns:
- the rounding method for this instance
-
applyPattern
public void applyPattern(String pattern) throws IllegalArgumentException
Sets the pattern to use for parsing and formatting angle. See class description for an explanation of how patterns work.- Parameters:
pattern- Pattern to use for parsing and formatting angle.- Throws:
IllegalArgumentException- If the specified pattern is not legal.
-
toPattern
public String toPattern()
Returns the pattern used for parsing and formatting angles. See class description for an explanation of how patterns work.- Returns:
- The formatting pattern.
-
format
public final String format(double angle)
Format an angle. The string will be formatted according the pattern set in the last call toapplyPattern(java.lang.String).- Parameters:
angle- Angle to format, in degrees.- Returns:
- The formatted string.
-
format
public StringBuffer format(double angle, StringBuffer toAppendTo, FieldPosition pos)
Formats an angle and appends the resulting text to a given string buffer. The string will be formatted according the pattern set in the last call toapplyPattern(java.lang.String).- Parameters:
angle- Angle to format, in degrees.toAppendTo- Where the text is to be appended.pos- An optionalFieldPositionidentifying a field in the formatted text, ornullif this information is not wanted. This field position shall be constructed with one of the following constants:DEGREES_FIELD,MINUTES_FIELD,SECONDS_FIELDorHEMISPHERE_FIELD.- Returns:
- The string buffer passed in as
toAppendTo, with formatted text appended.
-
format
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) throws IllegalArgumentException
Formats an angle, a latitude or a longitude and appends the resulting text to a given string buffer. The string will be formatted according the pattern set in the last call toapplyPattern(java.lang.String). The argumentobjshall be anAngleobject or one of its derived class (Latitude,Longitude). Ifobjis aLatitudeobject, then a symbol "N" or "S" will be appended to the end of the string (the symbol will be choosen according the angle's sign). Otherwise, ifobjis aLongitudeobject, then a symbol "E" or "W" will be appended to the end of the string. Otherwise, no hemisphere symbol will be appended.
Strictly speaking, formatting ordinary numbers is not theAngleFormat's job. Nevertheless, this method acceptNumberobjects. This capability is provided only as a convenient way to format altitude numbers together with longitude and latitude angles.- Specified by:
formatin classFormat- Parameters:
obj-AngleorNumberobject to format.toAppendTo- Where the text is to be appended.pos- An optionalFieldPositionidentifying a field in the formatted text, ornullif this information is not wanted. This field position shall be constructed with one of the following constants:DEGREES_FIELD,MINUTES_FIELD,SECONDS_FIELDorHEMISPHERE_FIELD.- Returns:
- The string buffer passed in as
toAppendTo, with formatted text appended. - Throws:
IllegalArgumentException- ifobjif not an object of classAngleorNumber.
-
parse
public Angle parse(String source, ParsePosition pos)
Parses a string as an angle. This method can parse an angle even if it doesn't comply exactly to the expected pattern. For example, this method will parse correctly string "48°12.34'" even if the expected pattern was "DDMM.mm" (i.e. the string should have been "4812.34"). Spaces between degrees, minutes and secondes are ignored. If the string ends with an hemisphere symbol "N" or "S", then this method returns an object of classLatitude. Otherwise, if the string ends with an hemisphere symbol "E" or "W", then this method returns an object of classLongitude. Otherwise, this method returns an object of classAngle.
-
parse
public Angle parse(String source) throws ParseException
Parses a string as an angle.- Parameters:
source- The string to parse.- Returns:
- The parsed string as an
Angle,LatitudeorLongitudeobject. - Throws:
ParseException- if the string has not been fully parsed.
-
parseObject
public Angle parseObject(String source, ParsePosition pos)
Parses a substring as an angle. Default implementation invokesparse(String, ParsePosition).- Specified by:
parseObjectin classFormat- Parameters:
source- A String whose beginning should be parsed.pos- Position where to start parsing.- Returns:
- The parsed string as an
Angle,LatitudeorLongitudeobject.
-
parseObject
public Angle parseObject(String source) throws ParseException
Parses a string as an object. Default implementation invokesparse(String).- Overrides:
parseObjectin classFormat- Parameters:
source- The string to parse.- Returns:
- The parsed string as an
Angle,LatitudeorLongitudeobject. - Throws:
ParseException- if the string has not been fully parsed.
-
hashCode
public int hashCode()
Returns a "hash value" for this object.
-
equals
public boolean equals(Object obj)
Compares this format with the specified object for equality.
-
-