Package org.geotools.util
Class LineFormat
- All Implemented Interfaces:
Serializable
,Cloneable
Parses a line of text data. This class is mostly used for parsing lines in a matrix or a table. Each column may
contains numbers, dates, or other objects parseable by some
Format
implementations. The example below reads
dates in the first column and numbers in all remaining columns.
final LineParser parser = new LineFormat(new Format[] {DateFormat.getDateTimeInstance()
,NumberFormat.getNumberInstance()
});
LineFormat
may be used for reading a matrix with an unknow number of columns, while requiring that all lines
have the same number of columns. The example below gets the number of columns while reading the first line, and
ensure that all subsequent lines have the same number of columns. If one line violate this condition, then a
ParseException
will be thrown. The check if performed by the getValues(double[])
method when the
data
array is non-nul.
This code can work as well with dates instead of numbers. In this case, the values returned will be microseconds ellapsed since January 1st, 1970.double[] data=null; finalBufferedReader
in = newBufferedReader
(newFileReader
("MATRIX.TXT")); for (String
line; (line=in.readLine()) != null;) { parser.setLine(line); data = parser.getValues(data); // ... process 'data' here ... });
A ParseException
may be thrown because a string can't be parsed, because an object can't be converted into
a number or because a line don't have the expected number of columns. In all case, it is possible to gets the index
of the first problem found using ParseException.getErrorOffset()
.
- Since:
- 2.0
- Author:
- Martin Desruisseaux (IRD)
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class Format
Format.Field
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new line parser for the default locale.LineFormat
(Format format) Constructs a new line parser using the specified format for every columns.LineFormat
(Format... formats) Constructs a new line parser using the specified format objects.LineFormat
(Locale locale) Constructs a new line parser for the specified locale. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Clears this parser.clone()
Returns a clone of this parser.format
(Object values, StringBuffer toAppendTo, FieldPosition position) Formats an object and appends the resulting text to a given string buffer.getValue
(int index) Returns the value at the specified index.int
Returns the number of elements found in the last line parsed bysetLine(String)
.byte[]
getValues
(byte[] array) Copies all values to the specified array.double[]
getValues
(double[] array) Copies all values to the specified array.float[]
getValues
(float[] array) Copies all values to the specified array.int[]
getValues
(int[] array) Copies all values to the specified array.long[]
getValues
(long[] array) Copies all values to the specified array.short[]
getValues
(short[] array) Copies all values to the specified array.parseObject
(String source) Parses text from the beginning of the given string to produce an object.parseObject
(String source, ParsePosition position) Parses text from a string to produce an object.int
Parses the specified line.int
Parses a substring of the specified line.void
Sets or adds a value to current line.void
Sets all values in the current line.toString()
Returns a string representation of current line.Methods inherited from class Format
format, formatToCharacterIterator
-
Constructor Details
-
LineFormat
public LineFormat()Constructs a new line parser for the default locale. -
LineFormat
Constructs a new line parser for the specified locale. For exampleLocale.US
may be used for reading numbers using the dot as decimal separator. -
LineFormat
Constructs a new line parser using the specified format for every columns.- Parameters:
format
- The format to use.- Throws:
IllegalArgumentException
- ifformat
is null.
-
LineFormat
Constructs a new line parser using the specified format objects. For example the first column will be parsed usingformats[0]
; the second column will be parsed usingformats[1]
, etc. If there is more columns than formats, then the last format object is reused for all remaining columns.- Parameters:
formats
- The formats to use for parsing.- Throws:
IllegalArgumentException
- ifformats
is null or an element offormats
is null.
-
-
Method Details
-
clear
public void clear()Clears this parser. Next call togetValueCount()
will returns 0. -
setLine
Parses the specified line. The content is immediately parsed and values can be obtained using one of thegetValues(...)
method.- Parameters:
line
- The line to parse.- Returns:
- The number of elements parsed in the specified line. The same information can be obtained with
getValueCount()
. - Throws:
ParseException
- If at least one column can't be parsed.
-
setLine
Parses a substring of the specified line. The content is immediately parsed and values can be obtained using one of thegetValues(...)
method.- Parameters:
line
- The line to parse.lower
- Index of the first character inline
to parse.upper
- Index after the last character inline
to parse.- Returns:
- The number of elements parsed in the specified line. The same information can be obtained with
getValueCount()
. - Throws:
ParseException
- If at least one column can't be parsed.
-
getValueCount
public int getValueCount()Returns the number of elements found in the last line parsed bysetLine(String)
. -
setValues
Sets all values in the current line. Thevalues
argument must be an array, which may be of primitive type.- Parameters:
values
- The array to set as values.- Throws:
IllegalArgumentException
- ifvalues
is not an array.- Since:
- 2.4
-
setValue
Sets or adds a value to current line. The index should be in the range 0 togetValueCount()
inclusively. If the index is equals togetValueCount()
, thenvalue
will be appended as a new column after existing data.- Parameters:
index
- Index of the value to add or modify.value
- The new value.- Throws:
ArrayIndexOutOfBoundsException
- If the index is outside the expected range.
-
getValue
Returns the value at the specified index. The index should be in the range 0 inclusively togetValueCount()
exclusively.- Parameters:
index
- Index of the value to fetch.- Returns:
- The value at the specified index.
- Throws:
ArrayIndexOutOfBoundsException
- If the index is outside the expected range.
-
getValues
Copies all values to the specified array. This method is typically invoked aftersetLine(String)
for fetching the values just parsed. Ifarray
is null, this method creates and returns a new array with a length equals to number of elements parsed. Ifarray
is not null, then this method will thrown an exception if the array length is not exactly equals to the number of elements parsed.- Parameters:
array
- The array to copy values into.- Returns:
array
if it was not null, or a new array otherwise.- Throws:
ParseException
- Ifarray
was not null and its length is not equals to the number of elements parsed, or if at least one element can't be parsed.
-
getValues
Copies all values to the specified array. This method is typically invoked aftersetLine(String)
for fetching the values just parsed. Ifarray
is null, this method creates and returns a new array with a length equals to number of elements parsed. Ifarray
is not null, then this method will thrown an exception if the array length is not exactly equals to the number of elements parsed.- Parameters:
array
- The array to copy values into.- Returns:
array
if it was not null, or a new array otherwise.- Throws:
ParseException
- Ifarray
was not null and its length is not equals to the number of elements parsed, or if at least one element can't be parsed.
-
getValues
Copies all values to the specified array. This method is typically invoked aftersetLine(String)
for fetching the values just parsed. Ifarray
is null, this method creates and returns a new array with a length equals to number of elements parsed. Ifarray
is not null, then this method will thrown an exception if the array length is not exactly equals to the number of elements parsed.- Parameters:
array
- The array to copy values into.- Returns:
array
if it was not null, or a new array otherwise.- Throws:
ParseException
- Ifarray
was not null and its length is not equals to the number of elements parsed, or if at least one element can't be parsed.
-
getValues
Copies all values to the specified array. This method is typically invoked aftersetLine(String)
for fetching the values just parsed. Ifarray
is null, this method creates and returns a new array with a length equals to number of elements parsed. Ifarray
is not null, then this method will thrown an exception if the array length is not exactly equals to the number of elements parsed.- Parameters:
array
- The array to copy values into.- Returns:
array
if it was not null, or a new array otherwise.- Throws:
ParseException
- Ifarray
was not null and its length is not equals to the number of elements parsed, or if at least one element can't be parsed.
-
getValues
Copies all values to the specified array. This method is typically invoked aftersetLine(String)
for fetching the values just parsed. Ifarray
is null, this method creates and returns a new array with a length equals to number of elements parsed. Ifarray
is not null, then this method will thrown an exception if the array length is not exactly equals to the number of elements parsed.- Parameters:
array
- The array to copy values into.- Returns:
array
if it was not null, or a new array otherwise.- Throws:
ParseException
- Ifarray
was not null and its length is not equals to the number of elements parsed, or if at least one element can't be parsed.
-
getValues
Copies all values to the specified array. This method is typically invoked aftersetLine(String)
for fetching the values just parsed. Ifarray
is null, this method creates and returns a new array with a length equals to number of elements parsed. Ifarray
is not null, then this method will thrown an exception if the array length is not exactly equals to the number of elements parsed.- Parameters:
array
- The array to copy values into.- Returns:
array
if it was not null, or a new array otherwise.- Throws:
ParseException
- Ifarray
was not null and its length is not equals to the number of elements parsed, or if at least one element can't be parsed.
-
toString
Returns a string representation of current line. All columns are formatted using theFormat
object specified at construction time. Columns are separated by tabulation. -
format
-
parseObject
Parses text from a string to produce an object.- Specified by:
parseObject
in classFormat
- Since:
- 2.4
-
parseObject
Parses text from the beginning of the given string to produce an object.- Overrides:
parseObject
in classFormat
- Throws:
ParseException
- Since:
- 2.4
-
clone
Returns a clone of this parser. In current implementation, this clone is not for usage in concurrent thread.
-