public class LineFormat extends Format
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()
.
Format.Field
Constructor and Description |
---|
LineFormat()
Constructs 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.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clears this parser.
|
LineFormat |
clone()
Returns a clone of this parser.
|
StringBuffer |
format(Object values,
StringBuffer toAppendTo,
FieldPosition position)
Formats an object and appends the resulting text to a given string buffer.
|
Object |
getValue(int index)
Returns the value at the specified index.
|
int |
getValueCount()
Returns the number of elements found in the last line parsed by
setLine(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.
|
Object |
parseObject(String source)
Parses text from the beginning of the given string to produce an object.
|
Object |
parseObject(String source,
ParsePosition position)
Parses text from a string to produce an object.
|
int |
setLine(String line)
Parses the specified line.
|
int |
setLine(String line,
int lower,
int upper)
Parses a substring of the specified line.
|
void |
setValue(int index,
Object value)
Sets or adds a value to current line.
|
void |
setValues(Object values)
Sets all values in the current line.
|
String |
toString()
Returns a string representation of current line.
|
format, formatToCharacterIterator
public LineFormat()
public LineFormat(Locale locale)
Locale.US
may be
used for reading numbers using the dot as decimal separator.public LineFormat(Format format) throws IllegalArgumentException
format
- The format to use.IllegalArgumentException
- if format
is null.public LineFormat(Format[] formats) throws IllegalArgumentException
formats[0]
; the second column will be parsed using formats[1]
, etc. If there is more columns than formats, then the last format
object is reused for all remaining columns.formats
- The formats to use for parsing.IllegalArgumentException
- if formats
is null or an element of format
is null.public void clear()
getValueCount()
will returns 0.public int setLine(String line) throws ParseException
getValues(...)
method.line
- The line to parse.getValueCount()
.ParseException
- If at least one column can't be parsed.public int setLine(String line, int lower, int upper) throws ParseException
getValues(...)
method.line
- The line to parse.lower
- Index of the first character in line
to parse.upper
- Index after the last character in line
to parse.getValueCount()
.ParseException
- If at least one column can't be parsed.public int getValueCount()
setLine(String)
.public void setValues(Object values) throws IllegalArgumentException
values
argument must be an array, which may
be of primitive type.values
- The array to set as values.IllegalArgumentException
- if values
is not an array.public void setValue(int index, Object value) throws ArrayIndexOutOfBoundsException
getValueCount()
inclusively. If the index is equals to getValueCount()
, then value
will be appended as a new column after existing data.index
- Index of the value to add or modify.value
- The new value.ArrayIndexOutOfBoundsException
- If the index is outside the expected range.public Object getValue(int index) throws ArrayIndexOutOfBoundsException
getValueCount()
exclusively.index
- Index of the value to fetch.ArrayIndexOutOfBoundsException
- If the index is outside the expected range.public double[] getValues(double[] array) throws ParseException
setLine(String)
for fetching the values just parsed. If array
is null, this method
creates and returns a new array with a length equals to number of elements parsed. If array
is not null, then this method will thrown an exception if the array length is not
exactly equals to the number of elements parsed.array
- The array to copy values into.array
if it was not null, or a new array otherwise.ParseException
- If array
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.public float[] getValues(float[] array) throws ParseException
setLine(String)
for fetching the values just parsed. If array
is null, this method
creates and returns a new array with a length equals to number of elements parsed. If array
is not null, then this method will thrown an exception if the array length is not
exactly equals to the number of elements parsed.array
- The array to copy values into.array
if it was not null, or a new array otherwise.ParseException
- If array
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.public long[] getValues(long[] array) throws ParseException
setLine(String)
for fetching the values just parsed. If array
is null, this method
creates and returns a new array with a length equals to number of elements parsed. If array
is not null, then this method will thrown an exception if the array length is not
exactly equals to the number of elements parsed.array
- The array to copy values into.array
if it was not null, or a new array otherwise.ParseException
- If array
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.public int[] getValues(int[] array) throws ParseException
setLine(String)
for fetching the values just parsed. If array
is null, this method
creates and returns a new array with a length equals to number of elements parsed. If array
is not null, then this method will thrown an exception if the array length is not
exactly equals to the number of elements parsed.array
- The array to copy values into.array
if it was not null, or a new array otherwise.ParseException
- If array
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.public short[] getValues(short[] array) throws ParseException
setLine(String)
for fetching the values just parsed. If array
is null, this method
creates and returns a new array with a length equals to number of elements parsed. If array
is not null, then this method will thrown an exception if the array length is not
exactly equals to the number of elements parsed.array
- The array to copy values into.array
if it was not null, or a new array otherwise.ParseException
- If array
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.public byte[] getValues(byte[] array) throws ParseException
setLine(String)
for fetching the values just parsed. If array
is null, this method
creates and returns a new array with a length equals to number of elements parsed. If array
is not null, then this method will thrown an exception if the array length is not
exactly equals to the number of elements parsed.array
- The array to copy values into.array
if it was not null, or a new array otherwise.ParseException
- If array
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.public String toString()
Format
object specified at construction time. Columns are separated by tabulation.public StringBuffer format(Object values, StringBuffer toAppendTo, FieldPosition position)
public Object parseObject(String source, ParsePosition position)
parseObject
in class Format
public Object parseObject(String source) throws ParseException
parseObject
in class Format
ParseException
public LineFormat clone()
Copyright © 1996–2019 Geotools. All rights reserved.