public class TLcdDistanceFormat extends Format implements Serializable, ILcdFormatter, ILcdParser
Parses and formats distances. More specifically:
- it parses a string containing distances in any unit (or without any unit) into doubles of the chosen 'program' unit
- it formats a double of the given 'program' unit into a String containing the chosen 'user' unit
Example for user unit 'TLcdDistanceUnit.METRE_UNIT
' and program unit 'TLcdDistanceUnit.KM_UNIT
'
- Parsing '1000 m' will result in 1 (km)
- Formatting '20' (km) will result in '20000 m'
By using the ParsePosition, a string containing more than one distance can be parsed too. For instance, this string contains four distances:" 12km 13 1mi3some text"It can be parsed by calling parse four times with the same parse position instance. The parse position will be just before "some text". A distance format configured with km as its program unit and mi as its user unit will parse the following speeds, all in km: 12, 20.92..., 1.60..., and 4.82... A subsequent parse will return
null
, because "some text" cannot be parsed.Format.Field
Modifier and Type | Field and Description |
---|---|
static int |
NUMBER_FIELD
Indicates the field containing the number of the distance.
|
static int |
UNIT_FIELD
Indicates the field containing the unit short name.
|
Constructor and Description |
---|
TLcdDistanceFormat()
Create a distance format with
user unit: TLcdDistanceUnit.METRE_UNIT program unit: TLcdDistanceUnit.METRE_UNIT |
TLcdDistanceFormat(TLcdDistanceUnit aUserUnit)
Create a distance format with
program unit: TLcdDistanceUnit.METRE_UNIT |
TLcdDistanceFormat(TLcdDistanceUnit aProgramUnit,
TLcdDistanceUnit aUserUnit)
Create a distance format.
|
Modifier and Type | Method and Description |
---|---|
Object |
clone() |
StringBuffer |
format(Object aObject,
StringBuffer aStringBufferToAppendTo,
FieldPosition aFieldPosition) |
String |
formatDistance(double aDistance)
Format the distance as a String given the distance as a double (in program unit).
|
StringBuffer |
formatDistance(double aDistance,
StringBuffer aToAppendToSFCT)
Format the distance (in program unit) as a String and append to the given StringBuffer.
|
StringBuffer |
formatDistance(double aDistance,
StringBuffer aToAppendToSFCT,
FieldPosition aFieldPosition)
Formats the given distance and appends that to the given string buffer.
|
int |
getFractionDigits()
Returns the number of fraction digits used to format the measurement.
|
int |
getMaximumFractionDigits()
Returns the maximum number of digits allowed in the fraction portion of a distance.
|
int |
getMinimumFractionDigits()
Returns the minimum number of digits allowed in the fraction portion of a distance.
|
String |
getNaNString()
Returns the string used for NaN values.
|
TLcdDistanceUnit |
getProgramUnit()
Returns the program unit.
|
TLcdDistanceUnit |
getUserUnit()
Returns the user unit.
|
boolean |
isAddUnitShortName()
Returns
true if format will add the unit short name, false otherwise. |
Object |
parse(String aString)
Parses a given
String and creates a new Object
with the corresponding type/properties. |
double |
parseDistance(String aDistanceString)
Returns the distance (in program unit) given a distance as a string (in user unit).
|
double |
parseDistance(String aDistanceString,
ParsePosition aParsePosition)
Returns the distance (in program unit) given a distance as a string (in user unit).
|
Object |
parseObject(String aString,
ParsePosition aParsePosition) |
void |
setAddUnitShortName(boolean aAddUnitShortName)
Sets if format should add the unit short name.
|
void |
setFractionDigits(int aFractionDigitCount)
Sets the number of fraction digits that should be used when formatting the measurement.
|
void |
setMaximumFractionDigits(int aMaximumFractionDigits)
Sets the maximum number of digits allowed in the fraction portion of a distance.
|
void |
setMinimumFractionDigits(int aMinimumFractionDigits)
Sets the minimum number of digits allowed in the fraction portion of a distance.
|
void |
setNaNString(String aNaNString)
Sets the string that should be used for NaN values.
|
void |
setProgramUnit(TLcdDistanceUnit aProgramUnit)
Sets the program unit.
|
void |
setUserUnit(TLcdDistanceUnit aUserUnit)
Sets the user unit.
|
String |
toString() |
format, formatToCharacterIterator, parseObject
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
format
public static final int NUMBER_FIELD
public static final int UNIT_FIELD
public TLcdDistanceFormat()
TLcdDistanceUnit.METRE_UNIT
TLcdDistanceUnit.METRE_UNIT
public TLcdDistanceFormat(TLcdDistanceUnit aUserUnit)
TLcdDistanceUnit.METRE_UNIT
public TLcdDistanceFormat(TLcdDistanceUnit aProgramUnit, TLcdDistanceUnit aUserUnit)
public TLcdDistanceUnit getProgramUnit()
setProgramUnit(com.luciad.util.TLcdDistanceUnit)
public void setProgramUnit(TLcdDistanceUnit aProgramUnit)
aProgramUnit
- The program unit.getProgramUnit()
public TLcdDistanceUnit getUserUnit()
setUserUnit(com.luciad.util.TLcdDistanceUnit)
public void setUserUnit(TLcdDistanceUnit aUserUnit)
Format.format(Object)
will convert the given measurement to this unit.
Format.parseObject(String)
will detect and parse the user unit as well as other units of the same measure type.
When no unit name is provided the format assumes the value is in the user unit.aUserUnit
- The user unit.getUserUnit()
public boolean isAddUnitShortName()
true
if format will add the unit short name, false
otherwise.true
if format will add the unit short name, false
otherwise.setAddUnitShortName(boolean)
public void setAddUnitShortName(boolean aAddUnitShortName)
aAddUnitShortName
- true
if unit short name should be added, false otherwise.isAddUnitShortName()
public int getFractionDigits()
getMinimumFractionDigits
returns.setFractionDigits(int)
,
getMinimumFractionDigits()
public void setFractionDigits(int aFractionDigitCount)
aFractionDigitCount
- The number of fraction digits.getFractionDigits()
,
setMinimumFractionDigits(int)
,
setMaximumFractionDigits(int)
public void setMinimumFractionDigits(int aMinimumFractionDigits)
aMinimumFractionDigits
- the minimum number of fraction digits to be shown; if less than
zero, then zero is used.getMinimumFractionDigits()
public int getMinimumFractionDigits()
setMinimumFractionDigits(int)
public void setMaximumFractionDigits(int aMaximumFractionDigits)
aMaximumFractionDigits
- the maximum number of fraction digits to be shown; if less than
zero, then zero is used.getMaximumFractionDigits()
public int getMaximumFractionDigits()
setMaximumFractionDigits(int)
public String getNaNString()
setNaNString(String)
public void setNaNString(String aNaNString)
aNaNString
- the string to use.getNaNString()
public StringBuffer format(Object aObject, StringBuffer aStringBufferToAppendTo, FieldPosition aFieldPosition)
public Object parse(String aString) throws IllegalArgumentException
ILcdParser
String
and creates a new Object
with the corresponding type/properties.parse
in interface ILcdParser
aString
- the String
to be parsed.Object
.IllegalArgumentException
- if the String
can't be parsed.public Object parseObject(String aString, ParsePosition aParsePosition)
parseObject
in class Format
public String formatDistance(double aDistance)
aDistance
- the distance as a double (in program unit).public StringBuffer formatDistance(double aDistance, StringBuffer aToAppendToSFCT)
aDistance
- the distance as a double (in program unit).aToAppendToSFCT
- The string buffer to append to.public StringBuffer formatDistance(double aDistance, StringBuffer aToAppendToSFCT, FieldPosition aFieldPosition)
aDistance
- Must be instance of Number.aToAppendToSFCT
- The string buffer where the formatted distance will be appended to.aFieldPosition
- NUMBER_FIELD or USER_FIELD. Can be used to retrieve the indices of the
unit or of the number.public double parseDistance(String aDistanceString) throws ParseException
aDistanceString
- The string to parse, assumed to be in the user unit.ParseException
- If the given string cannot be parsed.public double parseDistance(String aDistanceString, ParsePosition aParsePosition) throws ParseException
aDistanceString
- The string to parse, assumed to be in the user unit.aParsePosition
- The position to start the parsing at.ParseException
- If the given string cannot be parsed.