Package org.geotools.util
Class LineFormat
-
- All Implemented Interfaces:
Serializable,Cloneable
public class LineFormat extends Format
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 someFormatimplementations. 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()});LineFormatmay 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 aParseExceptionwill be thrown. The check if performed by thegetValues(double[])method when thedataarray 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; final
BufferedReaderin = newBufferedReader(newFileReader("MATRIX.TXT")); for (Stringline; (line=in.readLine()) != null;) { parser.setLine(line); data = parser.getValues(data); // ... process 'data' here ... });A
ParseExceptionmay 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 usingParseException.getErrorOffset().- Since:
- 2.0
- Author:
- Martin Desruisseaux (IRD)
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class Format
Format.Field
-
-
Constructor Summary
Constructors Constructor 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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Clears this parser.LineFormatclone()Returns a clone of this parser.StringBufferformat(Object values, StringBuffer toAppendTo, FieldPosition position)Formats an object and appends the resulting text to a given string buffer.ObjectgetValue(int index)Returns the value at the specified index.intgetValueCount()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.ObjectparseObject(String source)Parses text from the beginning of the given string to produce an object.ObjectparseObject(String source, ParsePosition position)Parses text from a string to produce an object.intsetLine(String line)Parses the specified line.intsetLine(String line, int lower, int upper)Parses a substring of the specified line.voidsetValue(int index, Object value)Sets or adds a value to current line.voidsetValues(Object values)Sets all values in the current line.StringtoString()Returns a string representation of current line.-
Methods inherited from class Format
format, formatToCharacterIterator
-
-
-
-
Constructor Detail
-
LineFormat
public LineFormat()
Constructs a new line parser for the default locale.
-
LineFormat
public LineFormat(Locale locale)
Constructs a new line parser for the specified locale. For exampleLocale.USmay be used for reading numbers using the dot as decimal separator.
-
LineFormat
public LineFormat(Format format) throws IllegalArgumentException
Constructs a new line parser using the specified format for every columns.- Parameters:
format- The format to use.- Throws:
IllegalArgumentException- ifformatis null.
-
LineFormat
public LineFormat(Format... formats) throws IllegalArgumentException
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- ifformatsis null or an element offormatis null.
-
-
Method Detail
-
clear
public void clear()
Clears this parser. Next call togetValueCount()will returns 0.
-
setLine
public int setLine(String line) throws ParseException
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
public int setLine(String line, int lower, int upper) throws ParseException
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 inlineto parse.upper- Index after the last character inlineto 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
public void setValues(Object values) throws IllegalArgumentException
Sets all values in the current line. Thevaluesargument must be an array, which may be of primitive type.- Parameters:
values- The array to set as values.- Throws:
IllegalArgumentException- ifvaluesis not an array.- Since:
- 2.4
-
setValue
public void setValue(int index, Object value) throws ArrayIndexOutOfBoundsExceptionSets or adds a value to current line. The index should be in the range 0 togetValueCount()inclusively. If the index is equals togetValueCount(), thenvaluewill 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
public Object getValue(int index) throws ArrayIndexOutOfBoundsException
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
public double[] getValues(double[] array) throws ParseExceptionCopies all values to the specified array. This method is typically invoked aftersetLine(String)for fetching the values just parsed. Ifarrayis null, this method creates and returns a new array with a length equals to number of elements parsed. Ifarrayis 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:
arrayif it was not null, or a new array otherwise.- Throws:
ParseException- Ifarraywas 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
public float[] getValues(float[] array) throws ParseExceptionCopies all values to the specified array. This method is typically invoked aftersetLine(String)for fetching the values just parsed. Ifarrayis null, this method creates and returns a new array with a length equals to number of elements parsed. Ifarrayis 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:
arrayif it was not null, or a new array otherwise.- Throws:
ParseException- Ifarraywas 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
public long[] getValues(long[] array) throws ParseExceptionCopies all values to the specified array. This method is typically invoked aftersetLine(String)for fetching the values just parsed. Ifarrayis null, this method creates and returns a new array with a length equals to number of elements parsed. Ifarrayis 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:
arrayif it was not null, or a new array otherwise.- Throws:
ParseException- Ifarraywas 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
public int[] getValues(int[] array) throws ParseExceptionCopies all values to the specified array. This method is typically invoked aftersetLine(String)for fetching the values just parsed. Ifarrayis null, this method creates and returns a new array with a length equals to number of elements parsed. Ifarrayis 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:
arrayif it was not null, or a new array otherwise.- Throws:
ParseException- Ifarraywas 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
public short[] getValues(short[] array) throws ParseExceptionCopies all values to the specified array. This method is typically invoked aftersetLine(String)for fetching the values just parsed. Ifarrayis null, this method creates and returns a new array with a length equals to number of elements parsed. Ifarrayis 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:
arrayif it was not null, or a new array otherwise.- Throws:
ParseException- Ifarraywas 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
public byte[] getValues(byte[] array) throws ParseExceptionCopies all values to the specified array. This method is typically invoked aftersetLine(String)for fetching the values just parsed. Ifarrayis null, this method creates and returns a new array with a length equals to number of elements parsed. Ifarrayis 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:
arrayif it was not null, or a new array otherwise.- Throws:
ParseException- Ifarraywas 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
public String toString()
Returns a string representation of current line. All columns are formatted using theFormatobject specified at construction time. Columns are separated by tabulation.
-
format
public StringBuffer format(Object values, StringBuffer toAppendTo, FieldPosition position)
-
parseObject
public Object parseObject(String source, ParsePosition position)
Parses text from a string to produce an object.- Specified by:
parseObjectin classFormat- Since:
- 2.4
-
parseObject
public Object parseObject(String source) throws ParseException
Parses text from the beginning of the given string to produce an object.- Overrides:
parseObjectin classFormat- Throws:
ParseException- Since:
- 2.4
-
clone
public LineFormat clone()
Returns a clone of this parser. In current implementation, this clone is not for usage in concurrent thread.
-
-